Python学习笔记(五)----Python 运算符
来源:互联网 发布:淘宝客服兼职是真的吗 编辑:程序博客网 时间:2024/04/27 13:26
一、什么是运算符?
本章节主要说明Python的运算符。举个简单的例子 4 +5 = 9 。 例子中,4和5被称为操作数,"+"号为运算符。
那么,在Python中有哪些类型的运算符呢?
Python语言支持以下类型的运算符:
1、算术运算符
2、比较(关系)运算符
3、赋值运算符
4、逻辑运算符
5、位运算符
6、成员运算符
7、身份运算符
8、运算符优先级
下面我们分别对上述的运算符类型进行讲解:
二、Python算术运算符
为了更好说明这个例子,以下假设变量a为10,变量b为20:
以下实例演示了Python所有算术运算符的操作:
#!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "a + b = ", cc = a - bprint "a - b = ", c c = a * bprint "a *b = ", c c = a / bprint "a / b = ", c c = a % bprint "a % b = ", ca = 2b = 3c = a**b print "a ** b = ", ca = 10b = 5c = a//b print "a // b = ", c
以上实例输出结果:
a + b = 31a - b = 11a *b = 210a / b = 2a % b = 1a ** b = 8a // b = 2
三、Python比较运算符
以下假设变量a为10,变量b为20:
以下实例演示了Python所有比较运算符的操作:
#!/usr/bin/pythona = 21b = 10c = 0if ( a == b ): print "Line 1 - a is equal to b"else: print "Line 1 - a is not equal to b"if ( a != b ): print "Line 2 - a is not equal to b"else: print "Line 2 - a is equal to b"if ( a <> b ): print "Line 3 - a is not equal to b"else: print "Line 3 - a is equal to b"if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b"if ( a > b ): print "Line 5 - a is greater than b"else: print "Line 5 - a is not greater than b"a = 5;b = 20;if ( a <= b ): print "Line 6 - a is either less than or equal to b"else: print "Line 6 - a is neither less than nor equal to b"if ( b >= a ): print "Line 7 - b is either greater than or equal to b"else: print "Line 7 - b is neither greater than nor equal to b"
以上实例输出结果:
Line 1 - a is not equal to bLine 2 - a is not equal to bLine 3 - a is not equal to bLine 4 - a is not less than bLine 5 - a is greater than bLine 6 - a is either less than or equal to bLine 7 - b is either greater than or equal to b
四、Python赋值运算符
以下假设变量a为10,变量b为20:
以下实例演示了Python所有赋值运算符的操作:
#!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", cc += aprint "Line 2 - Value of c is ", c c *= aprint "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2c %= aprint "Line 5 - Value of c is ", cc **= aprint "Line 6 - Value of c is ", cc //= aprint "Line 7 - Value of c is ", c
以上实例输出结果:
Line 1 - Value of c is 31Line 2 - Value of c is 52Line 3 - Value of c is 1092Line 4 - Value of c is 52Line 5 - Value of c is 2Line 6 - Value of c is 2097152Line 7 - Value of c is 99864
五、Python位运算符
按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:
以下实例演示了Python所有位运算符的操作:
#!/usr/bin/pythona = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0c = a & b; # 12 = 0000 1100print "Line 1 - Value of c is ", cc = a | b; # 61 = 0011 1101 print "Line 2 - Value of c is ", cc = a ^ b; # 49 = 0011 0001print "Line 3 - Value of c is ", cc = ~a; # -61 = 1100 0011print "Line 4 - Value of c is ", cc = a << 2; # 240 = 1111 0000print "Line 5 - Value of c is ", cc = a >> 2; # 15 = 0000 1111print "Line 6 - Value of c is ", c以上实例输出结果:
Line 1 - Value of c is 12Line 2 - Value of c is 61Line 3 - Value of c is 49Line 4 - Value of c is -61Line 5 - Value of c is 240Line 6 - Value of c is 15
六、Python逻辑运算符
Python语言支持逻辑运算符,以下假设变量a为10,变量b为20:
以下实例演示了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 - a and b are true"else: print "Line 5 - Either a is not true or b is not 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 - a and b are true
七、Python成员运算符
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
以下实例演示了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身份运算符
身份运算符用于比较两个对象的存储单元
以下实例演示了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
九、Python运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
以下实例演示了Python所有运算符优先级的操作:
#!/usr/bin/pythona = 20b = 10c = 15d = 5e = 0e = (a + b) * c / d #( 30 * 15 ) / 5print "Value of (a + b) * c / d is ", ee = ((a + b) * c) / d # (30 * 15 ) / 5print "Value of ((a + b) * c) / d is ", ee = (a + b) * (c / d); # (30) * (15/5)print "Value of (a + b) * (c / d) is ", ee = a + (b * c) / d; # 20 + (150/5)print "Value of a + (b * c) / d is ", e
以上实例输出结果:
Value of (a + b) * c / d is 90Value of ((a + b) * c) / d is 90Value of (a + b) * (c / d) is 90Value of a + (b * c) / d is 50
- Python学习笔记(五)----Python 运算符
- python tensorflow学习笔记(五)矩阵乘法运算
- Python学习笔记(运算符)
- Python学习笔记-运算符
- Python学习笔记(五)
- python学习笔记(五)
- python 学习笔记 (五)
- python学习笔记(五)
- Python学习笔记(五)
- Python学习笔记(五)
- Python学习笔记(五)
- python学习笔记(五)
- Python学习笔记(五)
- Python学习笔记(五)
- python学习笔记(五)
- Python学习笔记(五)
- Python学习笔记(五)
- [python]python学习笔记(五)
- NYOJ 96 n-1位数
- SharedPreferences 保存多个用户信息 并默认显示第一条用户信息
- 内核驱动常用头文件之--linux/module.h
- SOA 的基本概念及设计原则浅议
- 暗时间
- Python学习笔记(五)----Python 运算符
- POJ 3253 Fence Repair (哈夫曼树)Haffman-tree
- logstash日志平台搭建
- jQuery简单图表peity.js
- sharp lm8v302液晶屏替代屏
- WINCE 加载JPG 旋转缩放
- "invalid device symbol"问题解决
- HDU1907:John(Nim)
- DB2增差量备份恢复详尽演练