什么是运算符?
本章节主要说明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 输出结果 20
// 取整除 – 返回商的整数部分 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0
以下实例演示了python所有算术运算符的操作:
#!/usr/bin/python
a = 21
b = 10
c = 0
c = a + b
print “line 1 – value of c is “, c
c = a – b
print “line 2 – value of c is “, c
c = a * b
print “line 3 – value of c is “, c
c = a / b
print “line 4 – value of c is “, c
c = a % b
print “line 5 – value of c is “, c
a = 2
b = 3
c = a**b
print “line 6 – value of c is “, c
a = 10
b = 5
c = a//b
print “line 7 – value of c is “, c
以上实例输出结果:
line 1 – value of c is 31
line 2 – value of c is 11
line 3 – value of c is 210
line 4 – value of c is 2
line 5 – value of c is 1
line 6 – value of c is 8
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。
> 2 输出结果 15 ,二进制解释: 0000 1111
以下实例演示了python所有位运算符的操作:
#!/usr/bin/python
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print “line 1 – value of c is “, c
c = a | b; # 61 = 0011 1101
print “line 2 – value of c is “, c
c = a ^ b; # 49 = 0011 0001
print “line 3 – value of c is “, c
c = ~a; # -61 = 1100 0011
print “line 4 – value of c is “, c
c = a > 2; # 15 = 0000 1111
print “line 6 – value of c is “, c
以上实例输出结果:
line 1 – value of c is 12
line 2 – value of c is 61
line 3 – value of c is 49
line 4 – value of c is -61
line 5 – value of c is 240
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所有逻辑运算符的操作:
#!/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 – 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 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 – a and b are true
python成员运算符
除了以上的一些运算符之外,python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
运算符
描述
实例
in 如果在指定的序列中找到值返回true,否则返回false。 x 在 y序列中 , 如果x在y序列中返回true。
not in 如果在指定的序列中没有找到值返回true,否则返回false。 x 不在 y序列中 , 如果x不在y序列中返回true。
以下实例演示了python所有成员运算符的操作:
#!/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
以下实例演示了python所有身份运算符的操作:
#!/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运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
运算符
描述
** 指数 (最高优先级)
~ + – 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)
* / % // 乘,除,取模和取整除
+ – 加法减法
>> = 比较运算符
== != 等于运算符
= %= /= //= -= += *= **= 赋值运算符
is is not 身份运算符
in not in 成员运算符
not or and 逻辑运算符
以下实例演示了python所有运算符优先级的操作:
#!/usr/bin/python
a = 20
b = 10
c = 15
d = 5
e = 0
e = (a + b) * c / d #( 30 * 15 ) / 5
print “value of (a + b) * c / d is “, e
e = ((a + b) * c) / d # (30 * 15 ) / 5
print “value of ((a + b) * c) / d is “, e
e = (a + b) * (c / d); # (30) * (15/5)
print “value of (a + b) * (c / d) is “, e
e = a + (b * c) / d; # 20 + (150/5)
print “value of a + (b * c) / d is “, e
以上实例输出结果:
value of (a + b) * c / d is 90
value of ((a + b) * c) / d is 90
value of (a + b) * (c / d) is 90
value of a + (b * c) / d is 50