Python 运算符
2015-05-20 17:26
155 查看
原文地址:http://www.w3cschool.cc/python/python-operators.html
Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:
以下实例演示了Python所有逻辑运算符的操作:
以上实例输出结果:
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
以下实例演示了Python所有成员运算符的操作:
以上实例输出结果:
身份运算符用于比较两个对象的存储单元
以下实例演示了Python所有身份运算符的操作:
以上实例输出结果:
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。 |
#!/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还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。运算符 | 描述 | 实例 |
---|---|---|
in | 如果在指定的序列中找到值返回True,否则返回False。 | x 在 y序列中 , 如果x在y序列中返回True。 |
not in | 如果在指定的序列中没有找到值返回True,否则返回False。 | x 不在 y序列中 , 如果x不在y序列中返回True。 |
#!/usr/bin/python a = 10 b = 20 list = [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 = 2 if ( 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 list Line 2 - b is not available in the given list Line 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 |
#!/usr/bin/python a = 20 b = 20 if ( 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 = 30 if ( 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 identity Line 2 - a and b have same identity Line 3 - a and b do not have same identity Line 4 - a and b do not have same identity
相关文章推荐
- python运算符优先级问题
- python基础---运算符的使用
- Python 运算符
- Python基础运算符
- 中谷教育04 Python运算符与表达式
- 暴走Python之运算符与条件语句
- Python 运算符
- 讲解Python中运算符使用时的优先级
- python基本运算符(文摘)
- [python学习笔记] 运算符
- Python 数据类型、变量、运算符、注释
- Python入门教程之运算符与控制流
- Python运算符
- Python学习之运算符
- Python中3元运算符的实现
- Python 运算符优先级 运算符
- 跟老齐学Python之眼花缭乱的运算符
- Python学习之三【对象和类型&&运算符】
- Python学习(三) -- 运算符与表达式
- Python新手学习基础之运算符——位运算