Python expression evaluation

Python expression evaluation

In Python, expressions are composed of operands and operators. Python provides a wealth of built-in functions and operators to facilitate various calculations and processing of data.

1. Arithmetic operators
Arithmetic operators include four basic operators of addition, subtraction, multiplication and division, represented by "+", "-", "*", and "/" respectively.

Code example:
a = 10
b = 3
c = a + b # addition
d = a - b # subtraction
e = a * b # multiplication
f = a / b # division

print(c, d, e, f)

Second, the comparison
operator The comparison operator is used to compare whether two values ​​are equal or not, and return the Boolean value True or False. Including equal, not equal to, greater than, less than, greater than or equal to, less than or equal to six operators, represented by "==", "!=", ">", "<", ">=", "<=" respectively .

代码示例:
a = 10
b = 3
print(a == b) # False
print(a != b) # True
print(a > b) # True
print(a < b) # False
print(a >= b) # True
print(a <= b) # False

3. Logical operators
Logical operators include and, or, and not, which are represented by "and", "or" and "not" respectively. Logical operators are often used to judge the combination of Boolean values ​​and return Boolean values ​​True or False.

Code example:
a = 10
b = 3
c = 5

print(a > b and b <

Guess you like

Origin blog.csdn.net/update7/article/details/131485787