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

python教程评论929 views阅读模式

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 元组 逻辑运算符
  • 本文原创发布php教程 ,转载请注明出处,感谢您的尊重!
    • 上一篇:使用Python解析JSON数据的基本方法
    • 下一篇:分享一下Python 开发者节省时间的10个方法

    相关文章

    相关视频

    • 在Django框架中运行Python应用全攻略
    • 在Python的Django框架中创建和使用模版
    • python获取元素在数组中索引号的方法
    • 浅谈python中截取字符函数strip,lstr...
    • 详解Python中的元组与逻辑运算符
    • Python 简介
    • Python 环境搭建
    • Python 中文编码
    • Python 基础语法
    • Python 变量类型

    网友评论

    文明上网理性发言,请遵守 新闻评论服务协议

    我要评论

  • 立即提交

    专题推荐

    • 独孤九贱-php全栈开发教程

      全栈 100W+

      主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

    • 玉女心经-web前端开发教程

      入门 50W+

      主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

    • 天龙八部-实战开发教程

      实战 80W+

      主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

    作者信息

    php教程

    认证0级讲师

    最近文章

    发布技术文章

    • 最新文章
    • 热门排行

    • python之禅怎么打出来
    • python怎么学
    • boosting和bootstrap区别
    • python库是什么意思
    • python卸载后怎么也安装不上
    • python安装后怎么不见了
    • python怎么卸载模块
    • python能做什么?是什么?

    • pickle库的使用详解
    • Anaconda的新手使用大全
    • python爬虫是什么?为什么把python叫做爬虫?
    • Python微信库:itchat的用法详解
    • 关于python3学习基础知识总结
    • python爬虫是什么
    • 使用Python可以做什么
    • python如何实现可视化热力图

    推荐视频教程

  • javascript初级视频教程
  • jquery 基础视频教程
  • 视频教程分类

    • php视频教程
    • html视频教程
    • css视频教程
    • JS视频教程
    • jQuery视频教程
    • mysql视频教程
    • Linux视频教程
    • Python视频教程
    • 网站首页
    • PHP视频
    • PHP实战

    PHP中文网:独家原创,永久免费的在线php视频教程,php技术学习阵地!

    Copyright 2014-2019 http://www.php.cn/ All Rights Reserved | 皖B2-20150071-9 皖公网安备 34010402701654号 免责申明赞助与捐赠

    企鹅博客
    • 本文由 发表于 2020年9月7日 16:37:41
    • 转载请务必保留本文链接:https://www.qieseo.com/334451.html

    发表评论