Common operators in python

arithmetic operator

+ 加法运算符(如果两个字符串之间进行加法运算,则会进行拼串操作)
- 减法运算符
* 乘法运算符(如果将字符串和数字相乘,则会对字符串进行复制操作,将字符串复制指定次数)
/ 除法运算符,运算结果总会返回一个浮点类型
// 整除,只会保留计算后的整数位,总会返回一个整型(注意:一个浮点数整除一个整数时,返回一个.0的数,也是取整了,只是小数点后是零)
** 幂运算,求一个值的几次幂
% 取模,求两个数相除的余数

assignment operation

= can assign the value on the right side of the equal sign to the variable on the left side of the equal sign

+= a += 5 is equivalent to a = a + 5
-= a -= 5 is equivalent to a = a - 5
*= a *= 5 is equivalent to a = a * 5
**= a **= 5 is equivalent to a = a ** 5
/= a /= 5 is equivalent to a = a / 5
//= a //= 5 is equivalent to a = a // 5
%= a %= 5 is equivalent to a = a % 5

relational operator

Relational operators are used to compare the relationship between two values ​​and always return a Boolean value.
If the relationship is established, return True, otherwise return False

> 比较左侧值是否大于右侧值
>= 比较左侧的值是否大于或等于右侧的值
< 比较左侧值是否小于右侧的值
<= 比较左侧值是否小于或等于右侧的值
== 比较两个对象的值是否相等
!= 比较两个对象的值是否不相等
注意 == 和 != 比较的是对象的值,而不是id
is 比较两个对象是否是同一个对象,比较的是对象的id
is not 比较两个对象是否不是同一个对象,比较的是对象的id

You can perform size (equal) or less than (equal) operations on two strings in Python.
When strings are compared, the Unicode encodings of the strings are actually compared.
When comparing the Unicode encodings of two strings, it is compared bit by bit (if the previous bit can be compared, the next bit of both sides will not be compared).
Note: If you don't want to compare the Unicode encodings of two strings, you need to convert them to numbers and then compare them.

result = 10 > 20
print(result)
result = '1' > '2'
print(result)
result = 'db' > 'bc'
print(result)
result = 1 > False
print(result)

insert image description here

Logical Operators

Logical operators are mainly used to make some logical judgments
. Not logical
not can perform negation on the value on the right side of the symbol
. For Boolean values, negation will invert it, True becomes False, and False becomes True.
For non-Boolean values, the NOT operation converts them to Boolean and then negates them.
and Logical and
and can perform an AND operation on the values ​​​​on both sides of the symbol.
Only when the values ​​​​on both sides of the symbol are True, it will return True, and as long as there is one False, it will return False.
The AND operation is to find False.
The AND operation in Python is a short-circuit AND, if the first value is False, then no longer look at the second value

or logical not
or ORs the values ​​on either side of a sign.
The OR operation returns True as long as one of the two values ​​is True.
The OR operation is to find True.
The OR operation in Python is a short-circuit or, if the first value is True, it does not look at the second value.

result = True and print("你猜我出来来吗?")  #第一个值为True,会看第二个值,所以print()会执行 
result = False and print("你猜我出来来吗?") #第一个值为False,不会看第二个值,所以print()不会被执行 

result = True or print("你猜我出来来吗?")   #第一个值为True,不会看第二个值,所以print()不会被执行 
result = False or print("你猜我出来来吗?")  #第一个值为False,会看第二个值,所以print()会执行 

Logical operators can also be used in conjunction

result = 1 < 2 < 3 # 等价于  2 > 1 and 2 < 3

AND OR NOT OPERATIONS FOR NON-BOOL VALUES

When we perform an AND or operation on a non-Boolean value, Python will treat it as a Boolean value operation, and will eventually return the original value. The
rule of AND
operation is to find False. If the first value is False, it will not look at the second A value
If the first value is False, return the first value directly, otherwise return the second value
Or operation rules
Or operation is to find True If the first value is True, then do not look at the second value
If If the first value is True, return the first value directly, otherwise return the second value

# True and True
result = 1 and 2        # 2 
# True and False
result = 1 and 0        # 0
# False and True
result = 0 and 1        # 0
# False and False
result = 0 and None     # 0

# True or True
result = 1 or 2         # 1 
# True or False
result = 1 or 0         # 1
# False or True
result = 0 or 1         # 1
# False or False
result = 0 or None      # None

Conditional operator (also called ternary operator)

Syntax: Statement 1 if conditional expression else Statement 2
Execution process:
When the conditional operator is executed, it will first evaluate and judge the conditional expression.
If the judgment result is True, execute statement 1 and return the execution result.
If the judgment result is False, execute statement 2 and return the execution result

a = 30
b = 20
print('a的值比较大!') if a > b else print('b的值比较大!') # a的值比较大!
a = 10
print('a的值比较大!') if a > b else print('b的值比较大!') # b的值比较大!
#获取a和b之间的较大值
max = a if a > b else b
print(max)

operator precedence

insert image description here

Guess you like

Origin blog.csdn.net/adminstate/article/details/130791127