Python3.0 language supported operators are:
Arithmetic operators, comparison (relationship) operators, assignment operators, logical operators, bit operators, members of the operators, the identity of operators, operator precedence
First, arithmetic operators
Common arithmetic operators are +, -, *, /,%, these relatively common, they say ignored, mainly explained the other two arithmetic operators under Python3.0 in:
* Power - y x to the power return, such as 2 ** 3, i.e., the third power of 2.
// take divisible - rounded down to the integer divisor close, pay attention to positive and negative difference lies.
>>> 9//2 4 >>> -9//2 -5
Second, comparison (relationship) operator
Common comparison operators are: ==, =,>, <,> =, <=!. Here is not a detailed explanation.
Third, the assignment operator
Common assignment operator: = simple assignment operator + = addition assignment operator - = subtraction assignment operator, * = multiplication assignment operator, / = division assignment operator,% = modulo assignment operator * * = exponentiation assignment operator, = // assignment operator take divisible
Note here that, in the Python language, there is no + or - this operation with other languages to note the difference. Or being given, as shown:
Without the use of philosophical logic ++: compile simple and concise language on the resolution itself,
Right increment operator should a = a + 1 or a + = 1, when this a self-energizing, by id () Observe that, id value is changed, i.e., the name has a new value.
Fourth, the logical operators
Python language supports logical operators, the following is assumed as a variable 10, b is 20:
Operators |
Logical expression |
description |
Examples |
and |
x and y |
Boolean "and" - if x is False, x and y returns False, else it returns evaluation of y. |
(A and b) returns 20. |
or |
x or y |
Boolean "or" - If x is True, it returns the value of x, otherwise it returns evaluation of y. |
(A or b) to return 10. |
not |
not x |
Boolean "NOT" - If x is True, it returns False. If x is False, it returns True. |
not (a and b) returns False |
Look at the operation of the example and or not. Priority: not> and> or
and: front false ( 0 or False) expression is false, the value of the expression or later; or: the foregoing is true (not 0 or non False) is the preceding value expression, or the expression of the latter value;
V. Bitwise Operators
python-bit computing is looking at the numbers to binary operations. The following calculation example a = 60, b = 13:
Operators |
description |
Examples |
---|---|---|
& | Bitwise AND operator: two values involved in operations, if the corresponding two bits are 1, then the 1-bit result is 0 otherwise | (A & b) output 12, a binary interpretation: 00001100 |
| | Or bitwise operators: two long as the corresponding binary bit is a 1, it is a result bit. | (A | b) output 61, a binary interpretation: 00111101 |
^ | Bitwise exclusive OR operator: When two different corresponding binary, the result is a | (A ^ b) output 49, a binary interpretation: 00110001 |
~ | Bitwise operators: for each binary data bit inverse, i.e., the 1 to 0, the 0 to 1. similarly ~ x -x-1 | (~ A) -61 output, binary interpreter: 11000011, in the form of a complement to unsigned binary number. |
<< | Mobile operators left: each binary operand to the left a number of bits of all, the number to the right the number of bits specified << movement, discarding high, low 0s. | output a << 2 240, the binary interpretation: 11110000 |
>> | 右移动运算符:把">>"左边的运算数的各二进位全部右移若干位,>> 右边的数字指定了移动的位数 | a >> 2 输出结果 15 ,二进制解释: 0000 1111 |
六、成员运算符
除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。
运算符 |
描述 |
实例 |
in |
如果在指定的序列中找到值返回 True,否则返回 False。 |
x 在 y 序列中 , 如果 x 在 y 序列中返回 True。 |
not in |
如果在指定的序列中没有找到值返回 True,否则返回 False。 |
x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。 |
七、身份运算符
身份运算符用于比较两个对象的存储单元
运算符 |
描述 |
实例 |
---|---|---|
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() 函数用于获取对象内存地址。
is 与 == 区别: is 用于判断两个变量引用对象是否为同一个(同一块内存空间)即内存地址, 判断两个对象是否为同一对象, 是通过 id 来判断的; 当两个基本类型数据(或元组)内容相同时, id 会相同, 但并不代表 a 会随 b 的改变而改变。 == 用于判断引用变量的值是否相等。 判断两个对象的内容是否相同, 是通过调用 __eq__() 来判断的。
例子1:
a = 20 b = 20 if ( id(a) == id(b) ): print ("2 - a 和 b 有相同的标识") else: print ("2 - a 和 b 没有相同的标识")
例子2:
a = [1, 2, 3] b = a print("b is a :", b is a) print("b == a :", b == a) b = a[:] print("b is a :", b is a) ##虽然值一样,但是内存地址不一样。 print("b == a :", b == a)
结果:
例子3
# 当列表,元组,字典中的值都引用 a,b 时,总是返回 True,不受 a,b 值大小的影响 a=1000 b=1000 list1=[a,3,5] list2=[b,4,5] print(list1[0] is list2[0]) # >>>>> True tuple1=(a,3,5) tuple2=(b,4,5) print(tuple1[0] is tuple2[0]) # >>>>> True dict1={6:a,2:3,3:5} dict2={1:b,2:4,3:7} print(dict1[6] is dict2[1]) # >>>>> True print("-------------------------------------------------------") # 当不引用a,b,直接用具体值来测试时,列表,字典,不受值大小影响,返回True,元组则受 256 值范围的影响,超出范围则地址改变,返回 False。 list1=[1000,3,5] list2=[1000,4,5] print(list1[0] is list2[0]) # >>>>> True tuple1=(1000,3,5) tuple2=(1000,4,5) print(tuple1[0] is tuple2[0]) # >>>>> False dict1={6:1000,2:3,3:5} dict2={1:1000,2:4,3:7} print(dict1[6] is dict2[1]) # >>>>> True print("-------------------------------------------------------") # 当直接用列表、元组、字典本身来测试时,刚好相反,元组返回 True,列表,字典返回 False。 list1=[1000,3,5] list2=[1000,3,5] print(list1 is list2) # >>>>> False tuple1=(1000,3,5) tuple2=(1000,3,5) print(tuple1 is tuple2) # >>>>> True dict1={1:1000,2:3,3:5} dict2={1:1000,2:3,3:5} print(dict1 is dict2) # >>>>> False
八、运算符优先级
以下表格列出了从最高到最低优先级的所有运算符:
运算符 | 描述 |
---|---|
** | 指数 (最高优先级) |
~ + - | 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) |
* / % // | 乘,除,取模和取整除 |
+ - | 加法减法 |
>> << | 右移,左移运算符 |
& | 位 'AND' |
^ | | 位运算符 |
<= < > >= | 比较运算符 |
<> == != | 等于运算符 |
= %= /= //= -= += *= **= | 赋值运算符 |
is is not | 身份运算符 |
in not in | 成员运算符 |
not and or | 逻辑运算符 |