详解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之眼花缭乱的运算符
Sep 14 Python
python3+PyQt5使用数据库窗口视图
Apr 24 Python
python实现比较文件内容异同
Jun 22 Python
Appium+python自动化之连接模拟器并启动淘宝APP(超详解)
Jun 17 Python
Pandas库之DataFrame使用的学习笔记
Jun 21 Python
python 计算数据偏差和峰度的方法
Jun 29 Python
对python中 math模块下 atan 和 atan2的区别详解
Jan 17 Python
django中related_name的用法说明
May 20 Python
在Pytorch中使用Mask R-CNN进行实例分割操作
Jun 24 Python
python中Django文件上传方法详解
Aug 05 Python
Pycharm plot独立窗口显示的操作
Dec 11 Python
Python3 使用pip安装git并获取Yahoo金融数据的操作
Apr 08 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开发者常犯的10个MySQL错误更正剖析
2012/01/30 PHP
PHP使用Memcache时模拟命名空间及缓存失效问题的解决
2016/02/27 PHP
PHP编程获取各个时间段具体时间的方法
2017/05/26 PHP
php+croppic.js实现剪切上传图片功能
2018/08/14 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
2020/10/28 PHP
Prototype 学习 工具函数学习($w,$F方法)
2009/07/12 Javascript
百度Popup.js弹出框进化版 拖拽小框架发布 兼容IE6/7/8,Firefox,Chrome
2010/04/13 Javascript
JavaScript NaN和Infinity特殊值 [译]
2012/09/20 Javascript
在NodeJS中启用ECMAScript 6小结(windos以及Linux)
2014/07/15 NodeJs
JavaScript实现瀑布流布局
2020/06/28 Javascript
浅析Bootstrap组件之面板组件
2016/05/04 Javascript
AngularJS基础 ng-hide 指令用法及示例代码
2016/08/01 Javascript
AngularJS基础 ng-srcset 指令简单示例
2016/08/03 Javascript
利用JavaScript阻止表单提交的两种方法
2016/08/11 Javascript
bootstrap响应式导航条模板使用详解(含下拉菜单,弹出框)
2017/11/17 Javascript
利用JS动态生成隔行换色HTML表格的两种方法
2018/10/09 Javascript
vue swipe自定义组件实现轮播效果
2019/07/03 Javascript
对layui数据表格动态cols(字段)动态变化详解
2019/10/25 Javascript
快速解决vue2+vue-cli3项目ie兼容的问题
2020/11/17 Vue.js
[05:04]完美世界携手游戏风云打造 卡尔工作室地图界面篇
2013/04/23 DOTA
[01:18:33]Secret vs VGJ.S Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
在cmd命令行里进入和退出Python程序的方法
2018/05/12 Python
Python中调用其他程序的方式详解
2019/08/06 Python
Java文件与类动手动脑实例详解
2019/11/10 Python
numpy.ndarray 实现对特定行或列取值
2019/12/05 Python
Python3 利用face_recognition实现人脸识别的方法
2020/03/13 Python
Python %r和%s区别代码实例解析
2020/04/03 Python
如何用python处理excel表格
2020/06/09 Python
高分子材料个人求职信范文
2013/09/25 职场文书
农业大学毕业生的个人自我评价
2013/10/11 职场文书
销售经理岗位职责
2014/03/16 职场文书
青年志愿者先进事迹
2014/05/06 职场文书
2015年度绩效考核工作总结
2015/05/27 职场文书
暑期家教宣传单
2015/07/14 职场文书
高中班主任心得体会
2016/01/07 职场文书
Mysql分析设计表主键为何不用uuid
2022/03/31 MySQL