Python operators

Python operators

What is an operator?

This chapter mainly describes the operators of Python. Take a simple example 4 + 5 = 9 . In the example, 4 and 5 are called operands and " + " is called operator.

The Python language supports the following types of operators:

Next let's learn Python's operators one by one.

Python arithmetic operators

The following hypothetical variables: a=10, b=20 :

operator describe example
+ add - add two objects a + b outputs the result 30
- Subtract - get a negative number or subtract one number from another a - b output result -10
* multiply - multiply two numbers or return a string repeated several times a * b output result 200
/ divide - x divided by y b / a output result 2
% modulo - returns the remainder of the division b % a output result is 0
** power - returns x raised to the y power a**b is 10 to the 20th power, and the output result is 100000000000000000000
// Divide - Returns the integer part of the quotient 9//2 outputs result 4 , 9.0//2.0 outputs result 4.0

The following examples demonstrate the operation of all arithmetic operators in Python:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 21 b = 10 c = 0 c = a + b print "The value of 1 - c is: ", cc = a - b print "2 - value of c:", cc = a * b print "3 - value of c:", cc = a / b print "4 - value of c:", cc = a % b print " 5 - The value of c is: ", c # Modify variables a , b , ca = 2 b = 3 c = a**b print "6 - The value of c is: ", ca = 10 b = 5 c = a/ /b print "7 - the value of c is: ", c


Running the instance »

The output of the above example is:

1 - the value of c is: 31
2 - the value of c is: 11
3 - the value of c is: 210
4 - the value of c is: 2
5 - the value of c is: 1
6 - the value of c is: 8
7 - the value of c is: 2

Note: In Python2.x, integers are divided by integers, and only integers can be obtained. If you want to get the fractional part, just change one of the numbers to a floating point number.

>>> 1/2
0
>>> 1.0/2
0.5
>>> 1/float(2)
0.5

Python comparison operators

The following assumes that variable a is 10 and variable b is 20:

operator describe example
== equals - compares objects for equality (a == b) returns False.
!= not equal - compares whether two objects are not equal (a != b) returns true.
<> not equal - compares whether two objects are not equal (a <> b) returns true. This operator is similar to != .
> Greater than - Returns whether x is greater than y (a > b) returns False.
< less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False, respectively. (a < b) returns true.
>= Greater than or equal to - Returns whether x is greater than or equal to y. (a >= b) returns False.
<= Less than or equal to - Returns whether x is less than or equal to y. (a <= b) returns true.

The following examples demonstrate the operation of all Python comparison operators:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 21 b = 10 c = 0 if ( a == b ): print "1 - a is equal to b" else: print "1 - a is not equal to b" if ( a != b ): print "2 - a is not equal to b" else: print "2 - a is not equal to b" if ( a <> b ): print "3 - a is not equal to b" else: print "3 - a is equal to b" if ( a < b ): print "4 - a is less than b" else: print "4 - a is greater than or equal to b" if ( a > b ): print "5 - a is greater than b " else: print "5 - a is less than or equal to b" # modify the values ​​of variables a and b a = 5 b = 20 if ( a <= b ): print "6 - a is less than or equal to b" else: print "6 - a greater than b" if ( b >= a ): print "7 - b is greater than or equal to a" else: print "7 - b is less than a"

The output of the above example is:

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

Python assignment operator

The following assumes that variable a is 10 and variable b is 20:

operator describe example
= simple assignment operator c = a + b assigns the result of the operation of a + b to c
+= addition assignment operator c += a is equivalent to c = c + a
-= Subtraction assignment operator c -= a is equivalent to c = c - a
*= multiplication assignment operator c *= a is equivalent to c = c * a
/= division assignment operator c /= a is equivalent to c = c / a
%= Modulo assignment operator c %= a is equivalent to c = c % a
**= exponentiation assignment operator c **= a is equivalent to c = c ** a
//= Integer division assignment operator c //= a is equivalent to c = c // a

The following example demonstrates the operation of all assignment operators in Python:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 21 b = 10 c = 0 c = a + b print "1 - The value of c is: ", cc += a print "2 - value of c:", cc *= a print "3 - value of c:", cc /= a print "4 - value of c:", cc = 2 c %= a print "5 - value of c: ", cc **= a print "6 - value of c: ", cc //= a print "7 - value of c: ", c

The output of the above example is:

1 - the value of c is: 31
2 - the value of c is: 52
3 - the value of c is: 1092
4 - the value of c is: 52
5 - the value of c is: 2
6 - The value of c is: 2097152
7 - the value of c is: 99864

Python bitwise operators

Bitwise operators treat numbers as binary. The bitwise arithmetic in Python is as follows:

In the following table, variable a is 60, b is 13, and the binary format is as follows:

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011
operator describe example
& Bitwise AND operator: two values ​​involved in the operation, if both corresponding bits are 1, the result of the bit is 1, otherwise it is 0 (a & b) output result 12, binary interpretation: 0000 1100
| Bitwise OR: As long as one of the corresponding two binary bits is 1, the result bit is 1. (a | b) Output result 61, binary interpretation: 0011 1101
^ Bitwise XOR Operator: When two corresponding binary bits are different, the result is 1 (a ^ b) output result 49, binary interpretation: 0011 0001
~ Bitwise negation operator: Negates each binary bit of the data, that is, changes 1 to 0 and 0 to 1. ~x is like -x-1 (~a ) outputs the result -61, binary interpretation: 1100 0011, in one's complement form of a signed binary number.
<< Left shift operator: All binary bits of the operand are shifted to the left by several bits, the number of bits to be shifted is specified by the number on the right side of <<, the high bits are discarded, and the low bits are filled with 0. a << 2 output result 240, binary interpretation: 1111 0000
>> Right shift operator: shifts all the binary bits of the operand on the left of ">>" by a number of bits, and the number on the right of >> specifies the number of bits to move a >> 2 output result 15, binary interpretation: 0000 1111

The following example demonstrates the operation of all of Python's bitwise operators:

Example (Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "1 - c 的值为:", c c = a | b; # 61 = 0011 1101 print "2 - c 的值为:", c c = a ^ b; # 49 = 0011 0001 print "3 - c 的值为:", c c = ~a; # -61 = 1100 0011 print "4 - c 的值为:", c c = a << 2; # 240 = 1111 0000 print "5 - c 的值为:", c c = a >> 2; # 15 = 0000 1111 print "6 - c 的值为:", c

以上实例输出结果:

1 - c 的值为: 12
2 - c 的值为: 61
3 - c 的值为: 49
4 - c 的值为: -61
5 - c 的值为: 240
6 - c 的值为: 15

Python逻辑运算符

Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20:

运算符 逻辑表达式 描述 实例
and x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 (a and b) 返回 20。
or x or y 布尔"或" - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。 (a or b) 返回 10。
not not x 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 not(a and b) 返回 False

以上实例输出结果:

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 10 b = 20 if ( a and b ): print "1 - 变量 a 和 b 都为 true" else: print "1 - 变量 a 和 b 有一个不为 true" if ( a or b ): print "2 - 变量 a 和 b 都为 true,或其中一个变量为 true" else: print "2 - 变量 a 和 b 都不为 true" # 修改变量 a 的值 a = 0 if ( a and b ): print "3 - 变量 a 和 b 都为 true" else: print "3 - 变量 a 和 b 有一个不为 true" if ( a or b ): print "4 - 变量 a 和 b 都为 true,或其中一个变量为 true" else: print "4 - 变量 a 和 b 都不为 true" if not( a and b ): print "5 - 变量 a 和 b 都为 false,或其中一个变量为 false" else: print "5 - 变量 a 和 b 都为 true"

以上实例输出结果:

1 - 变量 a 和 b 都为 true
2 - 变量 a 和 b 都为 true,或其中一个变量为 true
3 - 变量 a 和 b 有一个不为 true
4 - 变量 a 和 b 都为 true,或其中一个变量为 true
5 - 变量 a 和 b 都为 false,或其中一个变量为 false

Python成员运算符

除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

运算符 描述 实例
in 如果在指定的序列中找到值返回 True,否则返回 False。 x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in 如果在指定的序列中没有找到值返回 True,否则返回 False。 x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。

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

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print "1 - 变量 a 在给定的列表中 list 中" else: print "1 - 变量 a 不在给定的列表中 list 中" if ( b not in list ): print "2 - 变量 b 不在给定的列表中 list 中" else: print "2 - 变量 b 在给定的列表中 list 中" # 修改变量 a 的值 a = 2 if ( a in list ): print "3 - 变量 a 在给定的列表中 list 中" else: print "3 - 变量 a 不在给定的列表中 list 中"

以上实例输出结果:

1 - 变量 a 不在给定的列表中 list 中
2 - 变量 b 不在给定的列表中 list 中
3 - 变量 a 在给定的列表中 list 中

Python身份运算符

身份运算符用于比较两个对象的存储单元

运算符 描述 实例
is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False
is not is not 是判断两个标识符是不是引用自不同对象 x is not y , 类似 id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。

注: id() 函数用于获取对象内存地址。

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

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 20 b = 20 if ( a is b ): print "1 - a 和 b 有相同的标识" else: print "1 - a 和 b 没有相同的标识" if ( a is not b ): print "2 - a 和 b 没有相同的标识" else: print "2 - a 和 b 有相同的标识" # 修改变量 b 的值 b = 30 if ( a is b ): print "3 - a 和 b 有相同的标识" else: print "3 - a 和 b 没有相同的标识" if ( a is not b ): print "4 - a 和 b 没有相同的标识" else: print "4 - a 和 b 有相同的标识"

以上实例输出结果:

1 - a 和 b 有相同的标识
2 - a 和 b 有相同的标识
3 - a 和 b 没有相同的标识
4 - a 和 b 没有相同的标识

is 与 == 区别:

is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。

>>> a = [1, 2, 3]
>>> b = a
>>> b is a 
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True

Python运算符优先级

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

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

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

实例(Python 2.0+)

#!/usr/bin/python # -*- coding: UTF-8 -*- a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print "(a + b) * c / d 运算结果为:", e e = ((a + b) * c) / d # (30 * 15 ) / 5 print "((a + b) * c) / d 运算结果为:", e e = (a + b) * (c / d); # (30) * (15/5) print "(a + b) * (c / d) 运算结果为:", e e = a + (b * c) / d; # 20 + (150/5) print "a + (b * c) / d 运算结果为:", e

以上实例输出结果:

(a + b) * c / d 运算结果为: 90
((a + b) * c) / d 运算结果为: 90
(a + b) * (c / d) 运算结果为: 90
a + (b * c) / d 运算结果为: 50

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063523&siteId=291194637