Python 简明教程

Python - Operators

Python Operators

Python operators 是用来对一个或多个操作数执行特定操作的特殊符号。 variables 、值或表达式可以用作操作数。例如,Python 的加法运算符 ( + ) 用于对两个变量、值或表达式执行加法操作。

以下是与 Python operators 相关的一些术语:

  1. Unary operators :需要一个操作数来执行特定操作的 Python 运算符称为一元运算符。

  2. Binary operators :需要两个操作数来执行特定操作的 Python 运算符称为二元运算符。

  3. Operands :与运算符一起使用以执行特定操作的变量、值或表达式。

Types of Python Operators

Python 运算符被归类在以下类别中 −

让我们逐个查看所有运算符。

Python Arithmetic Operators

Python Arithmetic operators 用于执行基本数学运算,例如加法、减法、乘法等。

下表包含所有算术运算符,以及它们的符号、名称和示例(假设 a 和 b 的值分别为 10 和 20):

Operator

Name

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*20

//

Floor Division

9//2 = 4

Example of Python Arithmetic Operators

a = 21
b = 10
c = 0

c = a + b
print ("a: {} b: {} a+b: {}".format(a,b,c))

c = a - b
print ("a: {} b: {} a-b: {}".format(a,b,c) )

c = a * b
print ("a: {} b: {} a*b: {}".format(a,b,c))

c = a / b
print ("a: {} b: {} a/b: {}".format(a,b,c))

c = a % b
print ("a: {} b: {} a%b: {}".format(a,b,c))

a = 2
b = 3
c = a**b
print ("a: {} b: {} a**b: {}".format(a,b,c))

a = 10
b = 5
c = a//b
print ("a: {} b: {} a//b: {}".format(a,b,c))

Output

a: 21 b: 10 a+b: 31
a: 21 b: 10 a-b: 11
a: 21 b: 10 a*b: 210
a: 21 b: 10 a/b: 2.1
a: 21 b: 10 a%b: 1
a: 2 b: 3 a**b: 8
a: 10 b: 5 a//b: 2

Python Comparison Operators

Python Comparison operators 比较两边值并判断它们之间的关系。它们也被称为关系运算符。

下表包含所有比较运算符及其符号、名称和示例(假设 a 和 b 的值分别为 10 和 20)−

Operator

Name

Example

==

Equal

(a == b)为 false。

!=

Not equal

(a != b) 为 true。

>

Greater than

(a > b) 为 false。

<

Less than

(a < b) 为 true。

>=

大于或等于

(a >= b) 为 false。

小于或等于

(a ⇐ b) 为 true。

Example of Python Comparison Operators

a = 21
b = 10
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 less than b" )
else:
   print ("Line 3 - a is not less than b")

if ( a > b ):
   print ("Line 4 - a is greater than b")
else:
   print ("Line 4 - a is not greater than b")

a,b=b,a #values of a and b swapped. a becomes 10, b becomes 21

if ( a <= b ):
   print ("Line 5 - a is either less than or equal to  b")
else:
   print ("Line 5 - a is neither less than nor equal to  b")

if ( b >= a ):
   print ("Line 6 - b is either greater than  or equal to b")
else:
   print ("Line 6 - b is neither greater than  nor equal to b")

Output

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not less than b
Line 4 - a is greater than b
Line 5 - a is either less than or equal to  b
Line 6 - b is either greater than  or equal to b

Python Assignment Operators

Python Assignment operators 用于将值分配给变量。以下表格显示了所有 Python 赋值运算符。

下表包含所有赋值运算符及其符号、名称和示例 −

Operator

Example

Same As

=

a = 10

a = 10

+=

a += 30

a = a + 30

-=

a -= 15

a = a - 15

*=

a *= 10

a = a * 10

/=

a /= 5

a = a / 5

%=

a %= 5

a = a % 5

**=

a **= 4

a = a ** 4

//=

a //= 5

a = a // 5

&=

a &= 5

a = a & 5

=

a

= 5

a = a

5

^=

a ^= 5

a = a ^ 5

>>=

a >>= 5

a = a >> 5

<⇐

a <⇐ 5

a = a << 5

Example of Python Assignment Operators

a = 21
b = 10
c = 0
print ("a: {} b: {} c : {}".format(a,b,c))
c = a + b
print ("a: {}  c = a + b: {}".format(a,c))

c += a
print ("a: {} c += a: {}".format(a,c))

c *= a
print ("a: {} c *= a: {}".format(a,c))

c /= a
print ("a: {} c /= a : {}".format(a,c))

c  = 2
print ("a: {} b: {} c : {}".format(a,b,c))
c %= a
print ("a: {} c %= a: {}".format(a,c))

c **= a
print ("a: {} c **= a: {}".format(a,c))

c //= a
print ("a: {} c //= a: {}".format(a,c))

Output

a: 21 b: 10 c : 0
a: 21  c = a + b: 31
a: 21 c += a: 52
a: 21 c *= a: 1092
a: 21 c /= a : 52.0
a: 21 b: 10 c : 2
a: 21 c %= a: 2
a: 21 c **= a: 2097152
a: 21 c //= a: 99864

Python Bitwise Operators

Python Bitwise operator 对位进行操作,执行逐位操作。这些算子用于比较二进制数。

下表包含所有按位操作符的符号、名称和示例 −

Operator

Name

Example

&

AND

a & b

OR

a

b

^

XOR

a ^ b

~

NOT

~a

<<

Zero fill left shift

a << 3

>>

Example of Python Bitwise Operators

a = 20
b = 10

print ('a=',a,':',bin(a),'b=',b,':',bin(b))
c = 0

c = a & b;
print ("result of AND is ", c,':',bin(c))

c = a | b;
print ("result of OR is ", c,':',bin(c))

c = a ^ b;
print ("result of EXOR is ", c,':',bin(c))

c = ~a;
print ("result of COMPLEMENT is ", c,':',bin(c))

c = a << 2;
print ("result of LEFT SHIFT is ", c,':',bin(c))

c = a >> 2;
print ("result of RIGHT SHIFT is ", c,':',bin(c))

Output

a= 20 : 0b10100 b= 10 : 0b1010
result of AND is  0 : 0b0
result of OR is  30 : 0b11110
result of EXOR is  30 : 0b11110
result of COMPLEMENT is  -21 : -0b10101
result of LEFT SHIFT is  80 : 0b1010000
result of RIGHT SHIFT is  5 : 0b101

Python Logical Operators

Python logical operators 用于组合两个或更多条件并检查最终结果。Python 语言支持以下逻辑运算符。假设变量 a 保存 10,变量 b 保存 20,则

下表包含所有逻辑运算符的符号、名称和示例 −

Operator

Name

Example

and

AND

a and b

or

OR

a or b

not

NOT

not(a)

Example of Python Logical Operators

var = 5

print(var > 3 and var < 10)
print(var > 3 or var < 4)
print(not (var > 3 and var < 10))

Output

True
True
False

Python Membership Operators

Python 的 membership operators 测试序列(例如字符串、列表或元组)的成员资格。

如下所述,有两个成员资格运算符:

Operator

Description

Example

in

如果在指定序列中找到变量,则返回 True,否则返回 False。

a in b

not in

如果在指定序列中找不到变量,则返回 True,否则返回 False。

a not in b

Example of Python Membership Operators

a = 10
b = 20
list = [1, 2, 3, 4, 5 ]

print ("a:", a, "b:", b, "list:", list)

if ( a in list ):
   print ("a is present in the given list")
else:
   print ("a is not present in the given list")

if ( b not in list ):
   print ("b is not present in the given list")
else:
   print ("b is present in the given list")

c=b/a
print ("c:", c, "list:", list)
if ( c in list ):
   print ("c is available in the given list")
else:
    print ("c is not available in the given list")

Output

a: 10 b: 20 list: [1, 2, 3, 4, 5]
a is not present in the given list
b is not present in the given list
c: 2.0 list: [1, 2, 3, 4, 5]
c is available in the given list

Python Identity Operators

Python identity operators 比较两个对象的内存地址。

如下所述,有两个标识符运算符:

Operator

Description

Example

is

如果两个变量是同一对象,则返回 True,否则返回 False。

a is b

is not

如果两个变量不是同一对象,则返回 True,否则返回 False。

a is not b

Example of Python Identity Operators

a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]
c = a

print(a is c)
print(a is b)

print(a is not c)
print(a is not b)

Output

True
False
False
True

Python Operators Precedence

运算符优先级决定了运算符求值执行的顺序。Python 运算符具有不同的优先级。下表包含优先级从最高到最低排列的运算符列表 −

下表列出了从最高 precedence 到最低的所有运算符。

Sr.No.

Operator & Description

1

指数(指数幂)

2

~ + - 补码、一元加号和减号(后两者的方法名称分别是“+@”和“-@”)

3

* / % // 乘法、除法、求余和取整除法

4

+ - Addition and subtraction

5

&gt;&gt; &lt;&lt; 右移和左移位运算符

6

& Bitwise 'AND'

7

*^

*按位异或“OR”和常规“OR”

8

&#8656; &lt; &gt; &gt;= 比较运算符

9

<> == != Equality operators

10

= %= /= //= -= += *= * *=*赋值运算符

11

is is not Identity operators

12

in not in Membership operators

13

在此处详细了解 Python 运算符优先级: Python operators precedence