详解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中列表和元组的相关语句和方法讲解
Aug 20 Python
python安装mysql-python简明笔记(ubuntu环境)
Jun 25 Python
python3实现公众号每日定时发送日报和图片
Feb 24 Python
在PyCharm中三步完成PyPy解释器的配置的方法
Oct 29 Python
Python基础教程之异常详解
Jan 10 Python
python统计函数库scipy.stats的用法解析
Feb 25 Python
Django框架获取form表单数据方式总结
Apr 22 Python
Python基于smtplib协议实现发送邮件
Jun 03 Python
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
Jun 09 Python
python 8种必备的gui库
Aug 27 Python
python将下载到本地m3u8视频合成MP4的代码详解
Nov 24 Python
弄清Pytorch显存的分配机制
Dec 10 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
星际争霸教主Flash的ID由来:你永远不会知道他之前的ID是www!
2019/01/18 星际争霸
PHP+Ajax无刷新带进度条图片上传示例
2017/02/08 PHP
php实现留言板功能(会话控制)
2017/05/23 PHP
php实现与python进行socket通信的方法示例
2017/08/30 PHP
Laravel实现ORM带条件搜索分页
2019/10/24 PHP
JavaScript实现禁止后退的方法
2006/12/27 Javascript
一个页面放2段图片滚动代码出现冲突的问题如何解决
2012/12/21 Javascript
JavaScript 盒模型 尺寸深入理解
2012/12/31 Javascript
js时间日期和毫秒的相互转换
2013/02/22 Javascript
jQuery编辑器KindEditor4.1.4代码高亮显示设置教程
2013/03/01 Javascript
JS获取复选框的值,并传递到后台的实现方法
2016/05/30 Javascript
JS与HTML结合实现流程进度展示条思路详解
2017/09/03 Javascript
前端MVVM框架解析之双向绑定
2018/01/24 Javascript
vue实现简单的MVVM框架
2018/08/05 Javascript
vue中选项卡点击切换且能滑动切换功能的实现代码
2018/11/25 Javascript
单线程JavaScript实现异步过程详解
2020/05/19 Javascript
js轮播图之旋转木马效果
2020/10/13 Javascript
Vue使用路由钩子拦截器beforeEach和afterEach监听路由
2020/11/16 Javascript
[01:38]【DOTA2亚洲邀请赛】Sumail——梦开始的地方
2017/03/03 DOTA
Python算法之图的遍历
2017/11/16 Python
Python命名空间的本质和加载顺序
2018/12/17 Python
Python操作Elasticsearch处理timeout超时
2020/07/17 Python
苹果中国官方网站:Apple中国
2016/07/22 全球购物
植村秀美国官网:Shu Uemura美国
2019/03/19 全球购物
电气自动化大学生求职信
2013/10/16 职场文书
结婚典礼证婚词
2014/01/11 职场文书
环境建设实施方案
2014/03/14 职场文书
颐和园导游词400字
2015/01/30 职场文书
民主评议党员个人总结
2015/02/13 职场文书
工作总结之小学教师体育工作范文(3篇)
2019/10/07 职场文书
Vue接口封装的完整步骤记录
2021/05/14 Vue.js
pytorch--之halfTensor的使用详解
2021/05/24 Python
SpringMVC 整合SSM框架详解
2021/08/30 Java/Android
Ruby序列化和持久化存储 Marshal和Pstore介绍
2022/04/18 Ruby
纯CSS实现一个简单步骤条的示例代码
2022/07/15 HTML / CSS
Win11 22H2 2022怎么更新? 获得Win1122H22022版本升级技巧
2022/09/23 数码科技