详解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判断、获取一张图片主色调的2个实例
Apr 10 Python
Python脚本处理空格的方法
Aug 08 Python
基于Python的文件类型和字符串详解
Dec 21 Python
对python_discover方法遍历所有执行的用例详解
Feb 13 Python
详解python爬虫系列之初识爬虫
Apr 06 Python
浅谈Django中view对数据库的调用方法
Jul 18 Python
浅析PEP570新语法: 只接受位置参数
Oct 15 Python
通过实例解析Python return运行原理
Mar 04 Python
keras实现图像预处理并生成一个generator的案例
Jun 17 Python
python3.5的包存放的具体路径
Aug 16 Python
Django静态文件加载失败解决方案
Aug 26 Python
python游戏开发之pygame实现接球小游戏
Apr 22 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
域名查询代码公布
2006/10/09 PHP
分享php邮件管理器源码
2016/01/06 PHP
PHP API接口必备之输出json格式数据示例代码
2017/06/27 PHP
PHP命名空间namespace及use的简单用法分析
2018/08/03 PHP
laravel 解决多库下的DB::transaction()事务失效问题
2019/10/21 PHP
使Ext的Template可以解析二层的json数据的方法
2007/12/22 Javascript
js实现兼容IE6与IE7的DIV高度
2010/05/13 Javascript
js网页侧边随页面滚动广告效果实现
2011/04/14 Javascript
js判断生效时间不得大于失效时间的思路及代码
2013/04/23 Javascript
SwfUpload在IE10上不出现上传按钮的解决方法
2013/06/25 Javascript
jquery ajax应用中iframe自适应高度问题解决方法
2014/04/12 Javascript
使用JavaScript获取电池状态的方法
2014/05/03 Javascript
基于Bootstrap仿淘宝分页控件实现代码
2016/11/07 Javascript
Node.js  REPL (交互式解释器)实例详解
2017/08/06 Javascript
微信小程序自定义多列选择器使用详解
2019/06/21 Javascript
ECharts地图绘制和钻取简易接口详解
2019/07/12 Javascript
小程序实现列表展开收起效果
2020/07/29 Javascript
使用JavaScript实现贪吃蛇游戏
2020/09/29 Javascript
vue实现顶部菜单栏
2020/11/08 Javascript
在RedHat系Linux上部署Python的Celery框架的教程
2015/04/07 Python
python简单实现刷新智联简历
2016/03/30 Python
安装Python和pygame及相应的环境变量配置(图文教程)
2017/06/04 Python
python中获得当前目录和上级目录的实现方法
2017/10/12 Python
Python3实现汉语转换为汉语拼音
2019/07/08 Python
python虚拟环境的安装和配置(virtualenv,virtualenvwrapper)
2019/08/09 Python
Python实现随机取一个矩阵数组的某几行
2019/11/26 Python
python实现双色球随机选号
2020/01/01 Python
TensorFlow tf.nn.max_pool实现池化操作方式
2020/01/04 Python
利用pytorch实现对CIFAR-10数据集的分类
2020/01/14 Python
Pytorch高阶OP操作where,gather原理
2020/04/30 Python
Python使用jpype模块调用jar包过程解析
2020/07/29 Python
详解pandas映射与数据转换
2021/01/22 Python
全球立体声:World Wide Stereo
2018/09/29 全球购物
2014客服代表实习自我鉴定
2014/09/18 职场文书
教师批评与自我批评心得体会
2014/10/16 职场文书
2014年村党支部工作总结
2014/12/04 职场文书