Operators are used to perform operations on variables and values.
Python divides operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Member operators
Bitwise operators
Arithmetic operators are used with numerical values to Perform common mathematical operations:
Operators:
Name: Add
Example: x y
Try it
x = 5 y = 3 print(x + y)
Operator: -
Name: Minus
Example: x - y
Try it
x = 5 y = 3 print(x - y)
Operator: *
Name: Multiply
Example: x * y
Try it
x = 5 y = 3 print(x * y)
Operator: /
Name: except
Instance: x / y
Try it
x = 15 y = 3 print(x / y)
Operator: %
Name: Modulo
Example: x % y
Try it
x = 5 y = 2 print(x % y)
Operator: **
Name: Power
Example: x ** y
Try it
x = 2 y = 5 print(x ** y) #same as 2*2*2*2*2
Operator: //
Name: floor division (dividing by integer)
Example: x // y
Try it
x = 15 y = 2 print(x // y) #the floor division // rounds the result down to the nearest whole number
The assignment operator is used to assign values to variables
Operator: =
Example: x = 5
is equivalent to: x = 5
Try it:
x = 5print(x)
Operator: =
Example :x = 3
is equivalent to: x = x 3
Try:
x = 5 x += 3 print(x)
Operator: -=
Example: x -= 3
Equivalent to: x = x - 3
Try it:
x = 5 x -= 3 print(x)
Operator: *=
Example: x *= 3
Equivalent to: x = x * 3
Try it:
x = 5 x *= 3 print(x)
Operator: /=
Example: x /= 3
Equivalent to: x = x / 3
Try it:
x = 5 x /= 3 print(x)
Operator: %=
Example: x %= 3
Equivalent to: x = x % 3
Try it:
x = 5 x%=3 print(x)
Operator: //=
Example: x //= 3
Equivalent to: x = x // 3
Try it:
x = 5 x//=3 print(x)
Operator: **=
Example: x **= 3
Equivalent to: x = x ** 3
Try it:
x = 5 x **= 3 print(x)
Operator: &=
Example: x &= 3
Equivalent to: x = x & 3
Try:
x = 5 x &= 3 print(x)
x = 5 x |= 3 print(x)
x = 5 x ^= 3 print(x)
x = 5 x >>= 3 print(x)
##Operator: <<=
实例:x <<= 3
等同于: x = x << 3
试一试:
x = 5 x <<= 3 print(x)
The above is the detailed content of What are the operators in Python and how to use them. For more information, please follow other related articles on the PHP Chinese website!