基础永远值得花费90%的精力去学习加强。认识实践的重要性。
运算符相对于语法里的动词,加减乘除。
Ax Python Basic Operators | 运算符
运算符可以操作数据,比如4+5=9,加号和等于号就相对于运算符。
类型
- Arithmetic Operators | 算术
- Comparison (Relational) Operators | 关系或比较
- Assignment Operators | 赋值
- Logical Operators | 逻辑
- Bitwise Operators | 按位
- Membership Operators | 成员
- Identity Operators | 身份
算术
假设变量a为10,变量b为20,则−
Operator | Description | Example |
---|---|---|
+ Addition | 在操作符的任意一边添加值。 | a + b = 30 |
- Subtraction | 从左操作数减去右操作数。 | a – b = -10 |
* Multiplication | 将运算符两边的值相乘 | a * b = 200 |
/ Division | 将左操作数除以右操作数 | b / a = 2 |
% Modulus | 左操作数除以右操作数,返回余数 | b % a = 0 |
** Exponent | 对运算符执行指数(幂)计算 | a**b =10 to the power 20 |
// | 上下除法——对操作数进行除法,其结果是去掉小数点后数字的商。但如果其中一个操作数为负,则结果将向下取整,即舍入0(趋近负无穷)− | 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0 |
例
假设变量 a 持有 21,变量 b 持有 10
#!/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
比较
这些运算符比较它们两边的值,并决定它们之间的关系。它们也被称为关系操作符。
假设变量a为10,变量b为20,则−
Operator | Description | Example |
---|---|---|
== | 如果两个操作数的值相等,则条件为真。 | (a == b) is not true. |
!= | 如果两个操作数的值不相等,则条件为真 | (a != b) is true. |
<> | 如果两个操作数的值不相等,则条件为真 | (a <> b) is true. This is similar to != operator. |
> | 如果左操作数的值大于右操作数的值,则条件为真 | (a > b) is not true. |
< | 如果左操作数的值小于右操作数的值,则条件为真。 | (a < b) is true. |
>= | 如果左操作数的值大于或等于右操作数的值,则条件为真。 | (a >= b) is not true. |
<= | 如果左操作数的值小于或等于右操作数的值,则条件为真。 | (a <= b) is true. |
例
假设变量 a 为 10,变量 b 为 20
#!/usr/bin/python
a = 21
b = 10
c = 0
if ( 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
赋值
假设变量 a 为 10,变量 b 为 20
Operator | Description | Example |
---|---|---|
= |
将右操作数赋值给左操作数 | c = a + b assigns value of a + b into c |
+= Add AND |
它将右操作数添加到左操作数,并将结果赋给左操作数 | c += a is equivalent to c = c + a |
-= Subtract AND |
它从左操作数减去右操作数,并将结果赋值给左操作数 | c -= a is equivalent to c = c - a |
*= Multiply AND |
它将右操作数与左操作数相乘,并将结果赋给左操作数 | c *= a is equivalent to c = c * a |
/= Divide AND |
它将左操作数与右操作数除法,并将结果赋值给左操作数 | c /= a is equivalent to c = c / a |
%= Modulus AND |
它使用两个操作数取模,并将结果赋值给左操作数 | c %= a is equivalent to c = c % a |
**= Exponent AND |
对运算符执行指数(幂)计算,并将值赋给左操作数 | c **= a is equivalent to c = c ** a |
//= Floor Division |
它对操作符执行下除法,并将值赋给左操作数 | c //= a is equivalent to c = c // a |
例
假设变量 a 为 10,变量 b 为 20
#!/usr/bin/python
a = 21
b = 10
c = 0
c = a + b
print "Line 1 - Value of c is ", c
c += a
print "Line 2 - Value of c is ", c
c *= a
print "Line 3 - Value of c is ", c
c /= a
print "Line 4 - Value of c is ", c
c = 2
c %= a
print "Line 5 - Value of c is ", c
c **= a
print "Line 6 - Value of c is ", c
c //= a
print "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 语言支持以下按位运算符
Operator | Description | Example |
---|---|---|
& Binary AND | 如果两个操作数中都存在一个位,操作符就向结果复制一个位 | (a & b) (means 0000 1100) |
| Binary OR | 如果它存在于任意一个操作数中,它就复制一个位 | (a | b) = 61 (means 0011 1101) |
^ Binary XOR | 如果位是在一个操作数中设置的,而不是在两个操作数中设置的,则复制该位。 | (a ^ b) = 49 (means 0011 0001) |
~ Binary Ones Complement | 它是一元的,具有“翻转”比特的效果。 | (~a ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number. |
<< Binary Left Shift | 左操作数值向左移动由右操作数指定的位数。 | a << 2 = 240 (means 1111 0000) |
>> Binary Right Shift | 左操作数的值会向右移动由右操作数指定的位数 | a >> 2 = 15 (means 0000 1111) |
例
#!/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; # 240 = 1111 0000
print "Line 5 - 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 语言支持以下逻辑运算符。假设变量 a 持有 10,变量 b 持有 20
Operator | Description | Example |
---|---|---|
and Logical AND | 如果两个操作数都为真,则条件为真。 | (a and b) is true. |
or Logical OR | 如果两个操作数有一个非零,则条件为真. | (a or b) is true. |
not Logical NOT | 用于反转其操作数的逻辑状态。 | Not(a and b) is false. |
成员
Python 的成员资格运算符测试序列中的成员资格,例如字符串、列表或元组。有两个会员运营商,如下所述 -
Operator | Description | Example |
---|---|---|
in | 如果在指定序列中找到变量,则计算为true,否则为false。 | x in y, here in results in a 1 if x is a member of sequence y. |
not in | 如果在指定序列中没有找到变量,则计算为true,否则为false。 | x not in y, here not in results in a 1 if x is not a member of sequence y. |
例
#!/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
身份
恒等运算符比较两个对象的内存位置。有两个身份运算符
Operator | Description | Example |
---|---|---|
is | 如果操作符两边的变量指向同一个对象,则计算为true,否则为false。 | x is y, here is results in 1 if id(x) equals id(y). |
is not | 如果操作符两边的变量指向同一个对象,则计算为false,否则为true。 | x is not y, here is not results in 1 if id(x) is not equal to id(y). |
例
#!/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
优先级
下表列出了从最高优先级到最低优先级的所有运算符
Operator | Description |
---|---|
** | Exponentiation (raise to the power) |
~ + - | Complement, unary plus and minus (method names for the last two are +@ and -@) |
* / % // | Multiply, divide, modulo and floor division |
+ - | Addition and subtraction |
>> << | Right and left bitwise shift |
& | Bitwise ‘AND’td> |
^ | | Bitwise exclusive OR' and regular OR’ |
<= < > >= | Comparison operators |
<> == != | Equality operators |
= %= /= //= -= += *= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
not or and | Logical operators |
运算符优先级影响表达式的计算方式。例如,x = 7 + 3 * 2;在这里,x 被赋值为 13,而不是 20,因为运算符的优先级高于 +,所以它首先将 32 相乘,然后加到 7。这里,优先级最高的运算符出现在表的顶部,最低的出现在表的顶部底部。
例
#!/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