【2015.08.29】6.Python运算符

1250阅读 0评论2015-08-29 Joker_ora
分类:Python/Ruby

什么是运算符?

本章节主要说明Python的运算符。举个简单的例子 4 +5 = 9 。 例子中,4和5被称为操作数,"+"号为运算符。

Python语言支持以下类型的运算符:

接下来让我们一个个来学习Python的运算符。

Python算术运算符

以下假设变量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

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

点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. a = 21
  3. b = 10
  4. c = 0

  5. c = a + b
  6. print "Line 1 - Value of c is ", c

  7. c = a - b
  8. print "Line 2 - Value of c is ", c

  9. c = a * b
  10. print "Line 3 - Value of c is ", c

  11. c = a / b
  12. print "Line 4 - Value of c is ", c

  13. c = a % b
  14. print "Line 5 - Value of c is ", c

  15. a = 2
  16. b = 3
  17. c = a**b
  18. print "Line 6 - Value of c is ", c

  19. a = 10
  20. b = 5
  21. c = a//b
  22. print "Line 7 - Value of c is ", c
以上实例输出结果:

点击(此处)折叠或打开

  1. Line 1 - Value of c is 31
  2. Line 2 - Value of c is 11
  3. Line 3 - Value of c is 210
  4. Line 4 - Value of c is 2
  5. Line 5 - Value of c is 1
  6. Line 6 - Value of c is 8
  7. Line 7 - Value of c is 2

Python比较运算符

以下假设变量a为10,变量b为20:

运算符 描述 实例
== 等于 - 比较对象是否相等 (a == b) 返回 False。
!= 不等于 - 比较两个对象是否不相等 (a != b) 返回 true.
<> 不等于 - 比较两个对象是否不相等 (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。
以下实例演示了Python所有比较运算符的操作:

点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. a = 21
  3. b = 10
  4. c = 0

  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. if ( a != b ):
  10.    print "Line 2 - a is not equal to b"
  11. else:
  12.    print "Line 2 - a is equal to b"

  13. if ( a <> b ):
  14.    print "Line 3 - a is not equal to b"
  15. else:
  16.    print "Line 3 - a is equal to b"

  17. if ( a < b ):
  18.    print "Line 4 - a is less than b"
  19. else:
  20.    print "Line 4 - a is not less than b"

  21. if ( a > b ):
  22.    print "Line 5 - a is greater than b"
  23. else:
  24.    print "Line 5 - a is not greater than b"

  25. a = 5;
  26. b = 20;
  27. if ( a <= b ):
  28.    print "Line 6 - a is either less than or equal to b"
  29. else:
  30.    print "Line 6 - a is neither less than nor equal to b"

  31. if ( b >= a ):
  32.    print "Line 7 - b is either greater than or equal to b"
  33. else:
  34.    print "Line 7 - b is neither greater than nor equal to b"

  35. 以上实例输出结果:

  36. Line 1 - a is not equal to b
  37. Line 2 - a is not equal to b
  38. Line 3 - a is not equal to b
  39. Line 4 - a is not less than b
  40. Line 5 - a is greater than b
  41. Line 6 - a is either less than or equal to b
  42. Line 7 - b is either greater than or equal to b

Python赋值运算符

以下假设变量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
以下实例演示了Python所有赋值运算符的操作:

点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. a = 21
  3. b = 10
  4. c = 0

  5. c = a + b
  6. print "Line 1 - Value of c is ", c

  7. c += a
  8. print "Line 2 - Value of c is ", c

  9. c *= a
  10. print "Line 3 - Value of c is ", c

  11. c /= a
  12. print "Line 4 - Value of c is ", c

  13. c = 2
  14. c %= a
  15. print "Line 5 - Value of c is ", c

  16. c **= a
  17. print "Line 6 - Value of c is ", c

  18. c //= a
  19. print "Line 7 - Value of c is ", c

以上实例输出结果:

点击(此处)折叠或打开

  1. Line 1 - Value of c is 31
  2. Line 2 - Value of c is 52
  3. Line 3 - Value of c is 1092
  4. Line 4 - Value of c is 52
  5. Line 5 - Value of c is 2
  6. Line 6 - Value of c is 2097152
  7. Line 7 - Value of c is 99864

Python位运算符

按位运算符是把数字看作二进制来进行计算的。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

Python位运算符


按位运算符是把数字看作二进制来进行计算的。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
以下实例演示了Python所有位运算符的操作:

点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. a = 60 # 60 = 0011 1100
  3. b = 13 # 13 = 0000 1101
  4. c = 0

  5. c = a & b; # 12 = 0000 1100
  6. print "Line 1 - Value of c is ", c

  7. c = a | b; # 61 = 0011 1101
  8. print "Line 2 - Value of c is ", c

  9. c = a ^ b; # 49 = 0011 0001
  10. print "Line 3 - Value of c is ", c

  11. c = ~a; # -61 = 1100 0011
  12. print "Line 4 - Value of c is ", c

  13. c = a << 2; # 240 = 1111 0000
  14. print "Line 5 - Value of c is ", c

  15. c = a >> 2; # 15 = 0000 1111
  16. print "Line 6 - Value of c is ", c

以上实例输出结果:

点击(此处)折叠或打开

  1. Line 1 - Value of c is 12
  2. Line 2 - Value of c is 61
  3. Line 3 - Value of c is 49
  4. Line 4 - Value of c is -61
  5. Line 5 - Value of c is 240
  6. Line 6 - Value of c is 15

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所有逻辑运算符的操作:

点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. a = 10
  3. b = 20
  4. c = 0

  5. if ( a and b ):
  6.    print "Line 1 - a and b are true"
  7. else:
  8.    print "Line 1 - Either a is not true or b is not true"

  9. if ( a or b ):
  10.    print "Line 2 - Either a is true or b is true or both are true"
  11. else:
  12.    print "Line 2 - Neither a is true nor b is true"


  13. a = 0
  14. if ( a and b ):
  15.    print "Line 3 - a and b are true"
  16. else:
  17.    print "Line 3 - Either a is not true or b is not true"

  18. if ( a or b ):
  19.    print "Line 4 - Either a is true or b is true or both are true"
  20. else:
  21.    print "Line 4 - Neither a is true nor b is true"

  22. if not( a and b ):
  23.    print "Line 5 - Either a is not true or b is not true or both are not true"
  24. else:
  25.    print "Line 5 - a and b are true"
  26. 以上实例输出结果:

  27. Line 1 - a and b are true
  28. Line 2 - Either a is true or b is true or both are true
  29. Line 3 - Either a is not true or b is not true
  30. Line 4 - Either a is true or b is true or both are true
  31. Line 5 - Either a is not true or b is not true or both are not true

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所有身份运算符的操作:


点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. a = 20
  3. b = 20

  4. if ( a is b ):
  5.    print "Line 1 - a and b have same identity"
  6. else:
  7.    print "Line 1 - a and b do not have same identity"

  8. if ( id(a) == id(b) ):
  9.    print "Line 2 - a and b have same identity"
  10. else:
  11.    print "Line 2 - a and b do not have same identity"

  12. b = 30
  13. if ( a is b ):
  14.    print "Line 3 - a and b have same identity"
  15. else:
  16.    print "Line 3 - a and b do not have same identity"

  17. if ( a is not b ):
  18.    print "Line 4 - a and b do not have same identity"
  19. else:
  20.    print "Line 4 - a and b have same identity"
  21. 以上实例输出结果:

  22. Line 1 - a and b have same identity
  23. Line 2 - a and b have same identity
  24. Line 3 - a and b do not have same identity
  25. Line 4 - a and b do not have same identity

Python运算符优先级

以下表格列出了从最高到最低优先级的所有运算符:

运算符 描述
** 指数 (最高优先级)
~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ - 加法减法
>> << 右移,左移运算符
& 位 'AND'
^ | 位运算符
<= < > >= 比较运算符
<> == != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not or and 逻辑运算符

以下实例演示了Python所有运算符优先级的操作:


点击(此处)折叠或打开

  1. #!/usr/bin/python

  2. a = 20
  3. b = 10
  4. c = 15
  5. d = 5
  6. e = 0

  7. e = (a + b) * c / d #( 30 * 15 ) / 5
  8. print "Value of (a + b) * c / d is ", e

  9. e = ((a + b) * c) / d # (30 * 15 ) / 5
  10. print "Value of ((a + b) * c) / d is ", e

  11. e = (a + b) * (c / d); # (30) * (15/5)
  12. print "Value of (a + b) * (c / d) is ", e

  13. e = a + (b * c) / d; # 20 + (150/5)
  14. print "Value of a + (b * c) / d is ", e
  15. 以上实例输出结果:

  16. Value of (a + b) * c / d is 90
  17. Value of ((a + b) * c) / d is 90
  18. Value of (a + b) * (c / d) is 90
  19. Value of a + (b * c) / d is 50











上一篇:【2015.08.29】5.Python变量类型
下一篇:【2015.08.29】7.Python条件语句