The Python operator is associated with an expression 1. Operator classification
The operators are divided into 5 main types:
1. 算术运算符 2. 比较运算符3. 位运算符4. 逻辑运算符 5. 成员运算符 6. 身份运算符7. 赋值运算符
1. Arithmetic operators
算术运算符 描述 实例 输出结果 + 两个对象相加 abc+d abcd - 两个对象相减 5-2 3 * 两个对象相乘 3*ab ababab / 两个对象相除 8/2 4 % 取模,返回除法的余数 5%3 2 ** 幂 2**3 8 // 取整除,返回商的整数部分 3//2 1
2. Comparison operators
比较运算符 描述 实例 输出结果 == 等于 abc==abc True != 不等于,与<>意思一样 abc!=ab True < 小于 5<6 True <= 小于等于 5<=6 True > 大于 5>6 False >= 大于等于 5>=6 False
3. Bitwise operators
位运算符 描述 实例 输出结果 按位与运算符:参与运算的两个 & 值,二进制位都为1,则该位的 60&13 12 结果为1,否则为0 | 按位或运算符:参与运算的两个 值,二进制位只要有一个为1时, 60|13 61 结果位就为1 ^ 按位异或运算符:参与运算的两 个值,二进制位不同时,该位的 60^13 49 结果为1 ~ 按位取反运算符:参与运算的值, 二进制位取反,0变1,1变0 ~60 -61 << 左移动运算符:参与运算的值, 所有二进制位左移动指定位数 60<<2 240 >> 右移动运算符:参与运算的值, 所有二进制位右移动指定位数 60>>3 7
4. Logical operators
逻辑运算符 描述 实例 输出结果 and 布尔"与":当两个值都为真, a and b 当a,b均为True,则结果为True 结果才为真,否则均为假 or 布尔"或":当两个值有一个 a or b 当a,b有一个为True,则结果为True 为真,结果就为真 not 布尔"非":当值为假时,结 not a 当a为False,则结果为True 果才为真
5. Member Operators
成员运算符 描述 实例 输出结果 in 如果指定的对象中包含该成 ‘a‘ in ‘abc‘ True 员,则返回True,否则False not in 如果指定的对象中不包含该成 ‘c‘ not in ‘abc‘ True 员,则返回True,否则False
6. Identity operator
身份运算符 描述 实例 输出结果 is 判断两个标识符是否是同一 ‘abc‘ is ‘abc‘ True 对象,如果是,则返回True is not 判断两个标识符是否不是同 ‘ab‘ isa not ‘abc‘ True 一对象,如果不是,则返回True
7. Assignment operator (a=10)
赋值运算符 描述 实例 输出结果 += 加法赋值运算符 a += 2 a=12 -= 减法赋值运算符 a -= 2 a=8 *= 乘法赋值运算符 a *= 2 a=20 /= 除法赋值运算符 a /= 2 a=5 %= 取模赋值运算符 a %= 3 a=1 //= 地板除赋值运算符 a //= 3 a=3 **= 幂赋值运算符 a **= 3 a=1000
Note: all assignment operations can be converted to (variable = variable arithmetic operator value) format.
赋值运算: a += 2算术运算: a = a + 2
2. Operator Precedence
Operator description priority (lower priority from top to bottom) ' Expression,... ' String Conversion 1{key,datum,...} Dictionary display 2[expression,...] List Display 3 (expression,...) binding or tuple display 4f (arguments,...) function call 5x[index:index] Addressing segment 6x[index] subscript (that is, index) 7x.attribute attribute Reference 8** exponent (power) 9~x rollover 10+x,-X plus sign 11*,/,% multiplication, division, modulo 12+,-addition, subtraction 13< ; <, >> left (right) bit move 14& bitwise AND operation 15^ bitwise XOR OR Operation 16| Bitwise OR operation 17<,<=,>,>=,==,!= comparison size operation 18is, is not identity operation 19in, not in member Operation 20not x boolean "non" operation 21and boolean "and" Arithmetic 22or boolean "or" Operation 23lambda Lambda Expression 24
Note: It is not recommended to prioritize rote operators, and it is recommended to use () to solve the priority problem in practice.
Detection data Attribution type (A=100)
type" () test can be used, in development try to avoid using this method
format: type (variable) b = Type (a) print (b) #<class ' int ';
isinstance () detects whether a data is created by the specified type
format: isinstance (variable, class) b = Isinstance (a,int) print (b) #True
Note: It is forbidden to detect whether any data is created by the object class because everything in Python is an object.
Python's operators and expressions