博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 运算符
阅读量:4216 次
发布时间:2019-05-26

本文共 2937 字,大约阅读时间需要 9 分钟。

原文地址:

Python逻辑运算符

Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:

运算符 描述 实例
and 布尔"与" - 如果x为False,x and y返回False,否则它返回y的计算值。 (a and b) 返回 true。
or 布尔"或" - 如果x是True,它返回True,否则它返回y的计算值。 (a or b) 返回 true。
not 布尔"非" - 如果x为True,返回False。如果x为False,它返回True。 not(a and b) 返回 false。

以下实例演示了Python所有逻辑运算符的操作:

#!/usr/bin/pythona = 10b = 20c = 0if ( 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 = 0if ( 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 trueLine 2 - Either a is true or b is true or both are trueLine 3 - Either a is not true or b is not trueLine 4 - Either a is true or b is true or both are trueLine 5 - Either a is not true or b is  not true or both are not true

Python成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

运算符 描述 实例
in 如果在指定的序列中找到值返回True,否则返回False。 x 在 y序列中 , 如果x在y序列中返回True。
not in 如果在指定的序列中没有找到值返回True,否则返回False。 x 不在 y序列中 , 如果x不在y序列中返回True。

以下实例演示了Python所有成员运算符的操作:

#!/usr/bin/pythona = 10b = 20list = [1, 2, 3, 4, 5 ];if ( a in list ):   print "Line 1 - a is available in the given list"else:   print "Line 1 - a is not available in the given list"if ( b not in list ):   print "Line 2 - b is not available in the given list"else:   print "Line 2 - b is available in the given list"a = 2if ( a in list ):   print "Line 3 - a is available in the given list"else:   print "Line 3 - a is not available in the given list"

以上实例输出结果:

Line 1 - a is not available in the given listLine 2 - b is not available in the given listLine 3 - a is available in the given list

Python身份运算符

身份运算符用于比较两个对象的存储单元

运算符 描述 实例
is is是判断两个标识符是不是引用自一个对象 x is y, 如果 id(x) 等于 id(y) , is 返回结果 1
is not is not是判断两个标识符是不是引用自不同对象 x is not y, 如果 id(x) 不等于 id(y). is not 返回结果 1

以下实例演示了Python所有身份运算符的操作:

#!/usr/bin/pythona = 20b = 20if ( a is b ):   print "Line 1 - a and b have same identity"else:   print "Line 1 - a and b do not have same identity"if ( id(a) == id(b) ):   print "Line 2 - a and b have same identity"else:   print "Line 2 - a and b do not have same identity"b = 30if ( a is b ):   print "Line 3 - a and b have same identity"else:   print "Line 3 - a and b do not have same identity"if ( a is not b ):   print "Line 4 - a and b do not have same identity"else:   print "Line 4 - a and b have same identity"

以上实例输出结果:

Line 1 - a and b have same identityLine 2 - a and b have same identityLine 3 - a and b do not have same identityLine 4 - a and b do not have same identity

转载地址:http://dmsmi.baihongyu.com/

你可能感兴趣的文章
一种应用于GPS反欺骗的基于MLE的RAIM改进方法
查看>>
揭秘汽车演化与变革,上海控安在华东师大举办普陀区科普学术论坛
查看>>
筑牢网络安全基座,安全护航经济数字化转型大会成功举办
查看>>
单元测试工具:单元测试的测试前置驱动条件
查看>>
汽车智不智能?“智能座舱”有话说
查看>>
自动驾驶汽车CAN总线数字孪生建模(一)
查看>>
自动驾驶汽车CAN总线数字孪生建模(二)
查看>>
自动驾驶汽车GPS系统数字孪生建模(一)
查看>>
自动驾驶汽车GPS系统数字孪生建模(二)
查看>>
上海控安入选首批工控安全防护能力贯标咨询机构名单
查看>>
自动驾驶汽车传感器数字孪生建模(一)
查看>>
自动驾驶汽车传感器数字孪生建模(二)
查看>>
车载数字孪生预期功能安全未知危害分析技术
查看>>
自动驾驶汽车以太网数字孪生建模(一)
查看>>
自动驾驶汽车以太网数字孪生建模(二)
查看>>
初识软件定义汽车
查看>>
科普 | 自动驾驶预期功能安全(二)
查看>>
轩辕实验室丨SAE J3061汽车信息安全标准解读
查看>>
轩辕实验室丨欧盟EVITA项目预研 第一章(一)
查看>>
轩辕实验室丨欧盟EVITA项目预研 第一章(二)
查看>>