概述
什么是操作符?
简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符。 Python语言支持操作者有以下几种类型。
算术运算符
比较(即关系)运算符
赋值运算符
逻辑运算符
位运算符
会员操作符
标识操作符
让我们逐一看看所有的运算符。
Python算术运算符:
假设变量a持有10和变量b持有20,则:
操作符 | 描述符 | 例子 |
---|---|---|
+ | 加法 - 对操作符的两侧增加值 | a + b = 30 |
- | 减法 - 减去从左侧操作数右侧操作数 | a - b = -10 |
* | 乘法 - 相乘的运算符两侧的值 | a * b = 200 |
/ | 除 - 由右侧操作数除以左侧操作数 | b / a = 2 |
% | 模 - 由右侧操作数和余返回除以左侧操作数 | b % a = 0 |
** | 指数- 执行对操作指数(幂)的计算 | a**b = 10 的幂 20 |
// | 地板除 - 操作数的除法,其中结果是将小数点后的位数被除去的商。 | 9//2 = 4 而 9.0//2.0 = 4.0 |
#!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", c c = a - bprint "Line 2 - Value of c is ", c c = a * bprint "Line 3 - Value of c is ", c c = a / bprint "Line 4 - Value of c is ", c c = a % bprint "Line 5 - Value of c is ", c a = 2b = 3c = a**b print "Line 6 - Value of c is ", c a = 10b = 5c = 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) 不为 true. |
!= | 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 | (a != b) 为 true. |
<> | 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 | (a <> b) 为 true。这个类似于 != 运算符 |
> | 检查左操作数的值是否大于右操作数的值,如果是,则条件成立。 | (a > b) 不为 true. |
< | 检查左操作数的值是否小于右操作数的值,如果是,则条件成立。 | (a < b) 为 true. |
>= | 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。 | (a >= b) 不为 true. |
<= | 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。 | (a <= b) 为 true. |
#!/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 b Line 2 - a is not equal to b Line 3 - a is not equal to b Line 4 - a is not less than b Line 5 - a is greater than b Line 6 - a is either less than or equal to b Line 7 - b is either greater than or equal to b
Python赋值运算符:
假设变量持有10和变量b持有20,则:
运算符 | 描述 | 示例 |
---|---|---|
= | 简单的赋值运算符,赋值从右侧操作数左侧操作数 | c = a + b将指定的值 a + b 到 c |
+= | 加法AND赋值操作符,它增加了右操作数左操作数和结果赋给左操作数 | c += a 相当于 c = c + a |
-= | 减AND赋值操作符,它减去右边的操作数从左边操作数,并将结果赋给左操作数 | c -= a 相当于 c = c - a |
*= | 乘法AND赋值操作符,它乘以右边的操作数与左操作数,并将结果赋给左操作数 | c *= a 相当于 c = c * a |
/= | 除法AND赋值操作符,它把左操作数与正确的操作数,并将结果赋给左操作数 | c /= a 相当于= c / a |
%= | 模量AND赋值操作符,它需要使用两个操作数的模量和分配结果左操作数 | c %= a is equivalent to c = c % a |
**= | 指数AND赋值运算符,执行指数(功率)计算操作符和赋值给左操作数 | c **= a 相当于 c = c ** a |
//= | 地板除,并分配一个值,执行地板除对操作和赋值给左操作数 | c //= a 相当于 c = c // a |
#!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", c c += 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 ", c c **= aprint "Line 6 - Value of c is ", c c //= aprint "Line 7 - Value of c is ", c
当执行上面的程序,它会产生以下结果:
Line 1 - Value of c is 31 Line 2 - Value of c is 52 Line 3 - Value of c is 1092 Line 4 - Value of c is 52 Line 5 - Value of c is 2 Line 6 - Value of c is 2097152 Line 7 - Value of c is 99864
Python位运算符:
位运算符作用于位和位操作执行位。假设,如果a =60;且b =13;现在以二进制格式它们将如下:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Python语言支持下位运算符
操作符 | 描述 | 示例 |
---|---|---|
& | 二进制和复制操作了一下,结果,如果它存在于两个操作数。 | (a & b) = 12 即 0000 1100 |
| | 二进制或复制操作了一个比特,如果它存在一个操作数中。 | (a | b) = 61 即 0011 1101 |
^ | 二进制异或运算符的副本,如果它被设置在一个操作数而不是两个比特。 | (a ^ b) = 49 即 0011 0001 |
~ | 二进制的补运算符是一元的,并有“翻转”位的效果。 | (~a ) = -61 即 1100 0011以2的补码形式由于带符号二进制数。 |
<< | 二进位向左移位运算符。左操作数的值左移由右操作数指定的位数。 | a << 2 = 240 即 1111 0000 |
>> | 二进位向右移位运算符。左操作数的值是由右操作数指定的位数向右移动。 | a >> 2 = 15 即 0000 1111 |
#!/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 ", c c = a | b; # 61 = 0011 1101 print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111print "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 | 所谓逻辑与运算符。如果两个操作数都是真的,那么则条件成立。 | (a and b) 为 true. |
or | 所谓逻辑OR运算符。如果有两个操作数都是非零然后再条件变为真。 | (a or b) 为 true. |
not | 所谓逻辑非运算符。用于反转操作数的逻辑状态。如果一个条件为真,则逻辑非运算符将返回false。 | not(a and b) 为 false. |
#!/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 - Either a is not true or b is 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
Python成员运算符:
除了前面讨论的运算符,Python成员运算符,在一个序列中成员资格的测试,如字符串,列表或元组。有两个成员运算符解释如下:
操作符 | 描述 | 示例 |
---|---|---|
in | 计算结果为true,如果它在指定找到变量的顺序,否则false。 | x在y中,在这里产生一个1,如果x是序列y的成员。 |
not in | 计算结果为true,如果它不找到在指定的变量顺序,否则为false。 | x不在y中,这里产生结果不为1,如果x不是序列y的成员。 |
#!/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 list Line 2 - b is not available in the given list Line 3 - a is available in the given list
Python标识运算符:
标识符比较两个对象的内存位置。两个运算符标识解释如下:
运算符 | 描述 | 例子 |
---|---|---|
is | 计算结果为true,如果操作符两侧的变量指向相同的对象,否则为false。 | x是y,这里结果是1,如果id(x)的值为id(y)。 |
is not | 计算结果为false,如果两侧的变量操作符指向相同的对象,否则为true。 | x不为y,这里结果不是1,当id(x)不等于id(y)。 |
#!/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 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运算符优先级
下表列出了所有运算符从最高优先级到最低。
运算符 | 描述 |
---|---|
** | 幂(提高到指数) |
~ + - | 补码,一元加号和减号(方法名的最后两个+@和 - @) |
* / % // | 乘,除,取模和地板除 |
+ - | 加法和减法 |
>> << | 左,右按位转移 |
& | 位'AND' |
^ | | 按位异'或`'和定期`或' |
<= < > >= | 比较运算符 |
<> == != | 等式运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
is is not | 标识运算符 |
in not in | 成员运算符 |
not or and | 逻辑运算符 |
#!/usr/bin/pythona = 20b = 10c = 15d = 5e = 0e = (a + b) * c / d #( 30 * 15 ) / 5print "Value of (a + b) * c / d is ", e e = ((a + b) * c) / d # (30 * 15 ) / 5print "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
转载于:https://blog.51cto.com/liangml/1855742
最后
以上就是无辜翅膀为你收集整理的运算的全部内容,希望文章能够帮你解决运算所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复