Home > Article > Backend Development > A Complete Guide to Python Operators
Python is a simple and easy-to-learn programming language that provides a rich set of operators for performing various mathematical and logical operations. This article will introduce commonly used operators in Python and provide specific code examples.
a = 10
b = 3
print(a b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1
print(a ** b) # Output: 1000
a = 10
b = 5
print(a == b) # Output: False
print(a != b ) # Output: True
print(a > b) # Output: True
print(a print(a >= b) # Output: True
print(a
a = 10
b = 5
a = b # Equivalent to a = a b
print(a) # Output: 15
a = 2 # Binary representation is 10
b = 3 # Binary representation is 11
print(a & b) # Output: 2 , bitwise AND operation
print(a | b) #Output: 3, bitwise OR operation
print(a ^ b) #Output: 1, bitwise XOR operation
print(~a) # Output: -3, negation operation
print(a print(b >> 1) # Output: 1, shift one to the right Bit
a = True
b = False
print(a and b) # Output: False, logical AND operation
print(a or b) #Output: True, logical OR operation
print(not b) #Output: True, logical NOT operation
a = [1, 2, 3, 4, 5]
print(2 in a) # Output: True
print(6 not in a) # Output: True
a = 10
b = 10
print(a is b) # Output: True
print(a is not b) # Output: False
The above are commonly used operators in Python. Of course, there are other operators, such as conditional operators (ternary operators), string operators, etc. Mastering these operators is very important for writing efficient Python code. I hope this article can help you understand and use Python operators.
The above is the detailed content of A Complete Guide to Python Operators. For more information, please follow other related articles on the PHP Chinese website!