300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 详解Python中的元组与逻辑运算符

详解Python中的元组与逻辑运算符

时间:2023-06-30 06:41:47

相关推荐

详解Python中的元组与逻辑运算符

后端开发|Python教程

Python,元组,逻辑运算符

后端开发-Python教程

Python元组

元组是另一个数据类型,类似于List(列表)。

元组用”()”标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。

在线抓娃娃源码,vscode文本类型,ubuntu 选个硬盘,tomcat自己的日志,乐清爬虫,php 过滤危险,天津seo哪家服务好,网站简单源码制作,discuz7.2风格模板lzw

#!/usr/bin/python# -*- coding: UTF-8 -*-tuple = ( abcd, 786 , 2.23, john, 70.2 )tinytuple = (123, john)print tuple # 输出完整元组print tuple[0] # 输出元组的第一个元素print tuple[1:3] # 输出第二个至第三个的元素 print tuple[2:] # 输出从第三个开始至列表末尾的所有元素print tinytuple * 2 # 输出元组两次print tuple + tinytuple # 打印组合的元组

下载单页源码,vscode怎么彻底删除,ubuntu 安装test,tomcat设置运行重启,sqlite两个表同步数据,爬虫pu,php 数组套数组,清远seo网络推广专业,随机词 网站,帝国cms7.2模板lzw

以上实例输出结果:

blue引擎源码,ubuntu没有应用商店,python爬虫医院信息,php中完成对二叉树节点的求和,seo主管 表格lzw

(abcd, 786, 2.23, john, 70.2)abcd(786, 2.23)(2.23, john, 70.2)(123, john, 123, john)(abcd, 786, 2.23, john, 70.2, 123, john)

以下是元组无效的,因为元组是不允许更新的。而列表是允许更新的:

#!/usr/bin/python# -*- coding: UTF-8 -*-tuple = ( abcd, 786 , 2.23, john, 70.2 )list = [ abcd, 786 , 2.23, john, 70.2 ]tuple[2] = 1000 # 元组中是非法应用list[2] = 1000 # 列表中是合法应用

Python逻辑运算符

Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:

以下实例演示了Python所有逻辑运算符的操作:

#!/usr/bin/pythona = 10b = 20c = 0if ( a and b ): print "Line 1 - a and b are true"else: print "Line 1 - Either a is not true or b is not true"if ( a or b ): print "Line 2 - Either a is true or b is true or both are true"else: print "Line 2 - Neither a is true nor b is true"a = 0if ( a and b ): print "Line 3 - a and b are true"else: print "Line 3 - Either a is not true or b is not true"if ( a or b ): print "Line 4 - Either a is true or b is true or both are true"else: print "Line 4 - Neither a is true nor b is true"if not( a and b ): print "Line 5 - Either a is not true or b is not true or both are not true"else: print "Line 5 - a and b are true"

以上实例输出结果:

Line 1 - a and b are trueLine 2 - Either a is true or b is true or both are trueLine 3 - Either a is not true or b is not trueLine 4 - Either a is true or b is true or both are trueLine 5 - Either a is not true or b is not true or both are not true

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。