数据类型--运算符和表达式
python语言支持下列类型的运算符
1、算术运算符
2、比较(关系)运算符
3、赋值运算符
4、位运算符
5、逻辑运算符
6、成员运算符
7、身份运算符
8、运算符优先级
算术运算符
以下假设变量a为10,变量b为20
运算符 | 描述 | 实例 |
---|---|---|
+ | 加 - 两个对象相加 | a + b 输出结果 30 |
- | 减 - 得到负数或是一个数减去另一个数 | a - b 输出结果 -10 |
* | 乘 - 两个数相乘或是返回一个被重复若干次的字符串 | a * b 输出结果 200 |
/ | 除 - x除以y | b / a 输出结果 2 |
% | 取模 - 返回除法的余数 | b % a 输出结果 0 |
** | 幂 - 返回x的y次幂 | a**b 为10的20次方, 输出结果 100000000000000000000 |
// | 取整除 - 返回商的整数部分 | 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0 |
1 #!/usr/bin/python 2 a = 21 3 b = 10 4 c = 0 5 6 c=a + b 7 print("Line 1 - value of c is ",c) 8 9 c=a - b 10 print("Line 2 - value of c is ",c) 11 12 c=a * b 13 print("Line 3 - values of c is ",c) 14 15 16 c=a / b 17 print("Line 4 - value of c is ",c) 18 19 20 c= a % b 21 print("Line 5 - value of c is ",c) 22 23 24 a = 2 25 b = 3 26 c = a**b 27 print("Line 6 - value if c is ",c) 28 29 a = 10 30 b = 5 31 c = a//b 32 print("Line 7 - value of c is ",c) 33 34 35 36 输出结果: 37 Line 1 - value of c is 31 38 Line 2 - value of c is 11 39 Line 3 - values of c is 210 40 Line 4 - value of c is 2.1 41 Line 5 - value of c is 1 42 Line 6 - value if c is 8 43 Line 7 - value of c is 2
比较运算符
以下假设变量a为10,变量b为20
运算符 | 描述 | 实例 |
---|---|---|
== | 等于 - 比较对象是否相等 | (a == b) 返回 False。 |
!= | 不等于 - 比较两个对象是否不相等 | (a != b) 返回 true. |
<> | 不等于 - 比较两个对象是否不相等(3.0已经取消) | (a <> b) 返回 true。这个运算符类似 != 。 |
> | 大于 - 返回x是否大于y | (a > b) 返回 False。 |
< | 小于 - 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。 | (a < b) 返回 true。 |
>= | 大于等于 - 返回x是否大于等于y。 | (a >= b) 返回 False。 |
<= | 小于等于 - 返回x是否小于等于y。 | (a <= b) 返回 true。 |
1 a = 21 2 b = 10 3 c = 0 4 5 if ( a == b ): 6 print("Line 1 - a is equal to b") 7 else: 8 print("Line 1 - a is not equal to b") 9 10 if ( a != b ): 11 print("Line 2 - a is not equal to b") 12 else: 13 print("Line 2 - a is equal to b") 14 15 # if ( a <> b ): 16 # print("Line 3 - a is not equal to b") 17 # else: 18 # print("Line 3 - a is equal to b") 19 20 21 if ( a < b ): 22 print("Line 4 - a is less than b") 23 else: 24 print("Line 4 - a is not less than b") 25 26 27 if ( a > b ): 28 print("Line 5 - a is greater than b") 29 else: 30 print("Line 5 - a is not greater than b") 31 32 33 a = 5 34 b = 20 35 if ( a <= b ): 36 print("Line 6 - is either less than or equal to b") 37 else: 38 print("Line 6 - is neither less than nor equal to b") 39 40 41 if ( b >= a ): 42 print("Line 7 - is either greater than or equal to b") 43 else: 44 print("Line 7 - is neither greater than nor equal to b") 45 46 47 48 输出显示: 49 Line 1 - a is not equal to b 50 Line 2 - a is not equal to b 51 Line 4 - a is not less than b 52 Line 5 - a is greater than b 53 Line 6 - is either less than or equal to b 54 Line 7 - is either greater than or equal to b 55 56 57 58 注意: 59 在3.0以上的python代码中,去掉了不等于"<>" 这个比较运算符。 60 61 如下: 62 63 if ( a <> b ): 64 ^ 65 SyntaxError: invalid syntax
赋值运算符
以下假设变量a为10,变量b为20:
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |
+= | 加法赋值运算符 | c += a 等效于 c = c + a |
-= | 减法赋值运算符 | c -= a 等效于 c = c - a |
*= | 乘法赋值运算符 | c *= a 等效于 c = c * a |
/= | 除法赋值运算符 | c /= a 等效于 c = c / a |
%= | 取模赋值运算符 | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c **= a 等效于 c = c ** a |
//= | 取整除赋值运算符 | c //= a 等效于 c = c // a |
1 #!/usr/bin/python 2 a = 21 3 b = 10 4 c = 0 5 6 c = a + b 7 print("Line 1 - value of c is", c) 8 9 c += a 10 print("Line 2 - value of c is", c) 11 12 c *= a 13 print("Line 3 - value of c is", c) 14 15 c /= a 16 print("Line 4 - value of c is", c) 17 18 c = 2 19 c %= a 20 print("Line 5 - value of c is", c) 21 22 c **= a 23 print("Line 6 - value of c is", c) 24 25 c //= a 26 print("Line 7 - value of c is", c) 27 28 29 30 输出结果: 31 Line 1 - value of c is 31 32 Line 2 - value of c is 52 33 Line 3 - value of c is 1092 34 Line 4 - value of c is 52.0 35 Line 5 - value of c is 2 36 Line 6 - value of c is 2097152 37 Line 7 - value of c is 99864
位运算符
按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:
运算符 | 描述 | 实例 |
---|---|---|
& | 按位与运算符 | (a & b) 输出结果 12 ,二进制解释: 0000 1100 |
| | 按位或运算符 | (a | b) 输出结果 61 ,二进制解释: 0011 1101 |
^ | 按位异或运算符 | (a ^ b) 输出结果 49 ,二进制解释: 0011 0001 |
~ | 按位取反运算符 | (~a ) 输出结果 -61 ,二进制解释: 1100 0011, 在一个有符号二进制数的补码形式。 |
<< | 左移动运算符 | a << 2 输出结果 240 ,二进制解释: 1111 0000 |
>> | 右移动运算符 | a >> 2 输出结果 15 ,二进制解释: 0000 1111 |
1 #!/usr/bin/python 2 a = 60 #60 = 0011 1100 3 b = 13 #60 = 0000 1101 4 c = 0 5 6 c = a & b; # 12 = 0000 1100 7 print("Line 1 - Value of c is", c) 8 9 c = a | b; # 61 = 0011 1101 10 print("Line 2 - value of c is", c) 11 12 c = a ^ b; # 49 = 0011 0001 13 print("Line 3 - value of c is", c) 14 15 c = ~a; # -61 = 1100 0011 16 print("Line 4 - value of c is", c) 17 18 c = a << 2; # 240 = 1111 0000 19 print("Line 5 - value of c is", c) 20 21 c = a >> 2; # 15 = 0000 1111 22 print=("Line 6 - value of c is", c) 23 24 输出结果: 25 Line 1 - Value of c is 12 26 Line 2 - value of c is 61 27 Line 3 - value of c is 49 28 Line 4 - value of c is -61 29 Line 5 - value of c is 240
逻辑运算符
以下假设变量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。 |
1 #!/usr/bin/python 2 a = 10 3 b = 20 4 c = 0 5 6 if ( a and b ): 7 print("Line 1 - a and b are true") 8 else: 9 print("Line 1 - Either a is not true or b is not true") 10 11 if( a or b ): 12 print("Line 2 - Either a is true or b is true or both are true") 13 else: 14 print("Line 2 - Neither a is true nor b is true") 15 16 17 a = 0 18 if( a and b ): 19 print("Line 3 -a and b are true") 20 else: 21 print("Line 3 - Either a is not true or b is not true") 22 23 if ( a or b ): 24 print("Line 4 - Either a is true or b is true or both are true") 25 else: 26 print("Line 4 - Neither a is true nor b is true") 27 28 if ( a and b ): 29 print("Line 5 - Either a is not true or b is not true or both are not true") 30 else: 31 print("Line 5 - a and b are true") 32 33 34 35 36 输出结果: 37 Line 1 - a and b are true 38 Line 2 - Either a is true or b is true or both are true 39 Line 3 - Either a is not true or b is not true 40 Line 4 - Either a is true or b is true or both are true 41 Line 5 - a and b are true
成员运算符
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组
运算符 | 描述 | 实例 |
---|---|---|
in | 如果在指定的序列中找到值返回True,否则返回False。 | x 在 y序列中 , 如果x在y序列中返回True。 |
not in | 如果在指定的序列中没有找到值返回True,否则返回False。 | x 不在 y序列中 , 如果x不在y序列中返回True。 |
1 #!/usr/bin/python 2 3 a = 10 4 5 b = 20 6 7 list = [1,2,3,4,5]; 8 9 10 if ( a in list ): 11 12 print("Line 1 - a is available in the given list") 13 14 else: 15 16 print("Line 1 - a is not available in the given list") 17 18 19 20 if ( b not in list ): 21 22 print("Line 2 - b is not available in the given list") 23 24 else: 25 26 print("Line 2 - b is available in the given list") 27 28 29 30 a = 2 31 32 if ( a in list ): 33 34 print("Line 3 - a is available in the given list") 35 36 else: 37 38 print("Line 3 - a is not available in the given list") 39 40 41 输出结果: 42 Line 1 - a is not available in the given list 43 Line 2 - b is not available in the given list 44 Line 3 - a is available in the given list
身份运算符
身份运算符用于比较两个对象的存储单元
运算符 | 描述 | 实例 |
---|---|---|
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 |
1 #!/usr/bin/python 2 a = 20 3 b = 20 4 5 if ( a is b ): 6 print("Line 1 - a and b have same identity") 7 else: 8 print("Line 1 - a and b do not have same identify") 9 10 if ( id(a) == id(b) ): 11 print("Line 2 - a and b have same identity") 12 else: 13 print("Line 2 - a and b do not have same identity") 14 15 b = 30 16 if ( a is b ): 17 print("Line 3 - a and b have same identity") 18 else: 19 print("Line 3 - a and b do not have same identity") 20 21 if ( a is not b ): 22 print("Line 4 - a and b do not have same identity") 23 else: 24 print("Line 4 - a and b have same identity") 25 26 27 显示结果: 28 Line 1 - a and b have same identity 29 Line 2 - a and b have same identity 30 Line 3 - a and b do not have same identity 31 Line 4 - a and b do not have same identity
运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
运算符 | 描述 |
---|---|
** | 指数 (最高优先级) |
~ + - | 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) |
* / % // | 乘,除,取模和取整除 |
+ - | 加法减法 |
>> << | 右移,左移运算符 |
& | 位 'AND' |
^ | | 位运算符 |
<= < > >= | 比较运算符 |
<> == != | 等于运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
is is not | 身份运算符 |
in not in | 成员运算符 |
not or and | 逻辑运算符 |
1 #!/usr/bin/python 2 3 a = 20 4 b = 10 5 c = 15 6 d = 5 7 e = 0 8 9 e = ( a + b )* c / d #(20+10)*15/5 10 print("value of (a+b)*c/d is ", e) 11 12 e = (( a + b ) * c) / d #((20+10)*15)/5 13 print("value of (a+b)*c/d is ", e) 14 15 e = ( a + b ) * ( c / d ) #(20+10)*(15/5) 16 print("value of (a+b)*(c/d) is ", e) 17 18 e= a + ( b * c ) / d #20 + ( 10 * 15 ) / 5 19 print("value of a+(b*c)/d os ", e) 20 21 22 输出结果: 23 value of (a+b)*c/d is 90.0 24 value of (a+b)*c/d is 90.0 25 value of (a+b)*(c/d) is 90.0 26 value of a+(b*c)/d os 50.0