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


Posted in Python onOctober 13, 2015

Python元组
元组是另一个数据类型,类似于List(列表)。
元组用"()"标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。

#!/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 # 打印组合的元组

以上实例输出结果:

('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中的元组与逻辑运算符
以下实例演示了Python所有逻辑运算符的操作:

#!/usr/bin/python

a = 10
b = 20
c = 0

if ( 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 = 0
if ( 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 true
Line 2 - Either a is true or b is true or both are true
Line 3 - Either a is not true or b is not true
Line 4 - Either a is true or b is true or both are true
Line 5 - Either a is not true or b is not true or both are not true
Python 相关文章推荐
python模块之StringIO使用示例
Apr 08 Python
Python数据类型详解(二)列表
May 08 Python
python和pygame实现简单俄罗斯方块游戏
Feb 19 Python
Python常见排序操作示例【字典、列表、指定元素等】
Aug 15 Python
python write无法写入文件的解决方法
Jan 23 Python
Python使用统计函数绘制简单图形实例代码
May 15 Python
python输入多行字符串的方法总结
Jul 02 Python
对python3中的RE(正则表达式)-详细总结
Jul 23 Python
python爬虫 urllib模块url编码处理详解
Aug 20 Python
Python 字典中的所有方法及用法
Jun 10 Python
Python装饰器结合递归原理解析
Jul 02 Python
python 下载文件的几种方式分享
Apr 07 Python
如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求
Oct 13 #Python
Python语法快速入门指南
Oct 12 #Python
初步认识Python中的列表与位运算符
Oct 12 #Python
Python入门学习之字符串与比较运算符
Oct 12 #Python
各个系统下的Python解释器相关安装方法
Oct 12 #Python
Python中数字以及算数运算符的相关使用
Oct 12 #Python
深入解析Python中的变量和赋值运算符
Oct 12 #Python
You might like
PHP源码之explode使用说明
2011/08/05 PHP
php防止伪造数据从地址栏URL提交的方法
2014/08/24 PHP
php5.4以下版本json不支持不转义内容中文的解决方法
2015/01/13 PHP
PHP简单处理表单输入的特殊字符的方法
2016/02/03 PHP
php接口技术实例详解
2016/12/07 PHP
关于Laravel Route重定向的一个注意点
2017/01/16 PHP
PHP的自定义模板引擎
2017/03/24 PHP
php和html的区别点详细总结
2019/09/24 PHP
Yii框架模拟组件调用注入示例
2019/11/11 PHP
javascript中获取选中对象的类型
2007/04/02 Javascript
jQuery解析Json实例详解
2015/11/24 Javascript
jQuery学习笔记——jqGrid的使用记录(实现分页、搜索功能)
2016/11/09 Javascript
angularJS 指令封装回到顶部示例详解
2017/01/22 Javascript
搭建简单的nodejs http服务器详解
2017/03/09 NodeJs
详解webpack分离css单独打包
2017/06/21 Javascript
微信小程序实现倒计时60s获取验证码
2020/04/17 Javascript
详解使用Vue Router导航钩子与Vuex来实现后退状态保存
2017/09/11 Javascript
React Native验证码倒计时工具类分享
2017/10/24 Javascript
react中Suspense的使用详解
2019/09/01 Javascript
webpack 如何解析代码模块路径的实现
2019/09/04 Javascript
JS+Canvas实现五子棋游戏
2020/08/26 Javascript
Vue SPA 首屏优化方案
2021/02/26 Vue.js
对python内置map和six.moves.map的区别详解
2018/12/19 Python
tensorflow2.0保存和恢复模型3种方法
2020/02/03 Python
使用 django orm 写 exists 条件过滤实例
2020/05/20 Python
利用Python如何画一颗心、小人发射爱心
2021/02/21 Python
美国知名的女性服饰品牌:LOFT(洛芙特)
2016/08/05 全球购物
施华洛世奇英国官网:SWAROVSKI英国
2017/03/13 全球购物
环境工程毕业生自荐信
2013/11/17 职场文书
党员自我评价分享
2013/12/13 职场文书
顶岗实习接收函
2014/01/09 职场文书
创先争优活动方案
2014/02/12 职场文书
2014单位领导班子四风对照检查材料思想汇报
2014/09/25 职场文书
教师聘用意向书
2015/05/11 职场文书
MySQL EXPLAIN输出列的详细解释
2021/05/12 MySQL
SpringBoot中HttpSessionListener的简单使用方式
2022/03/17 Java/Android