Python operators are used in programming to perform a variety of operations on variables and values. In addition, these operators can manipulate individual items and returns a result and the data items are referred as operands or arguments.
Python has 7 operator groups as shown below:
- Python Arithmetic Operators
- Python Assignment Operators
- Python Comparison Operators
- Python Logical Operators
- Python Identity Operators
- Python Membership Operators
- Python Bitwise Operators
Python Arithmetic Operators
Python arithmetic operators include basic mathematical operations.
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | a = 20 b = 10 addition = a + b subtraction = a - b multiplication = a * b division = a / b modulus = a % b exponentiation = a ** b floordivision = a // b print(f"First variable is: {a} ") print(f"Second variable: {b} ") print(f"Python addition example: {addition} ") print(f"Python subtraction example: {subtraction} ") print(f"Python multiplication example: {multiplication} ") print(f"Python division example: {division} ") print(f"Python modulus example: {modulus} ") print(f"Python exponentiation example: {exponentiation} ") print(f"Python floordivision example: {floordivision} ") |
Output:
1 2 3 4 5 6 7 8 9 | First variable is: 20 Second variable: 10 Python addition example: 30 Python subtraction example: 10 Python multiplication example: 200 Python division example: 2.0 Python modulus example: 0 Python exponentiation example: 10240000000000 Python floordivision example: 2 |
Python Assignment Operators
Python assignment operators are used to assign variables with a value.
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | a = 20 b = 7 print("nPython Assignment Operator Examples") print(f" First variable is: {a} ") print(f" Second variable: {b} ") print(f" a = 20: {a}") a = 20 a += b print(f" a += b: {a}") a = 20 a -= b print(f" a -= b: {a}") a = 20 a *= b print(f" a *= b: {a}") a = 20 a /= b print(f" a /= b: {a}") a = 20 a %= b print(f" a %= b: {a}") a = 20 a //= b print(f" a //= b: {a}") a = 20 a**=b print(f" a **= b: {a}") a = 20 a&=b print(f" a &= b: {a}") a = 20 a |= b print(f" a |= b: {a}") a = 20 a ^= b print(f" a ^= b: {a}") a = 20 a >>= b print(f" a >>= b: {a}") a = 20 a <<= b print(f" a <<= b: {a}") |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Python Assignment Operator Examples First variable is: 20 Second variable: 7 a = 20: 20 a += b: 27 a -= b: 13 a *= b: 140 a /= b: 2.857142857142857 a %= b: 6 a //= b: 2 a **= b: 1280000000 a &= b: 4 a |= b: 23 a ^= b: 19 a >>= b: 0 a <<= b: 2560 |
Python Comparison Operators
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | equal = a == b not_equal = a != b greater_than = a > b less_than = a < b greater_than_or_equal_to = a >= b less_than_or_equal_to = a <= b print("nPython Comparison Operator Examples") print(f" First variable is: {a} ") print(f" Second variable: {b} ") print(f" a is equal to b: {equal} ") print(f" a is not equal to b: {not_equal} ") print(f" a is greater than b: {greater_than} ") print(f" a is less than b: {less_than} ") print(f" a is greater than or equal to b: {greater_than_or_equal_to} ") print(f" a is less than or equal to b: {less_than_or_equal_to} ") |
Output:
1 2 3 4 5 6 7 8 9 | Python Comparison Operator Examples First variable is: 20 Second variable: 30 a is equal to b: False a is not equal to b: True a is greater than b: False a is less than b: True a is greater than or equal to b: False a is less than or equal to b: True |
Python Logical Operators
Python logical operators are used to combine conditional statements to
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 | a = 1 b = 20 logical_and = a < 5 and a < 10 logical_or = a < 5 or a < 10 logical_not = not(a < 5 and a < 10) print("nPython Logical Operator Examples") print(f" First variable is: {a} ") print(f" Second variable: {b} ") print(f" a < 5 and a < 10: {logical_and} ") print(f" a < 5 or a < 10: {logical_or} ") print(f" a < 5 not a < 10: {logical_not} ") |
Output:
1 2 3 4 5 6 | Python Logical Operator Examples First variable is: 1 Second variable: 20 a < 5 and a < 10: True a < 5 or a < 10: True a < 5 not a < 10: False |
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Input:
1 2 3 4 5 6 7 8 9 10 11 | a = 1 b = 20 identity_is = a is b identity_is_not = a is not b print("nPython Identity Operator Examples") print(f" First variable is: {a} ") print(f" Second variable: {b} ") print(f" identity_is = a is b: {identity_is} ") print(f" identity_is_not = a is not b: {identity_is_not} ") |
Output:
1 2 3 4 5 | Python Identity Operator Examples First variable is: 1 Second variable: 20 identity_is = a is b: False identity_is_not = a is not b: True |
Python Membership Operators
Python membership operators are used to test if a value is presented in an object.
Input:
1 2 3 4 5 6 7 8 | cars = ["Aston Martin", "BMW", "Ferrari", "Lamborghini"] membership_in = 'Aston Martin' in cars membership_not_in = 'Mercedes' not in cars print("nPython Membership Operator Examples") print(f" membership_in = 'Aston Martin' in cars: {membership_in} ") print(f" membership_not_in = 'Mercedes' not in cars: {membership_not_in} ") |
Output:
1 2 3 | Python Membership Operator Examples membership_in = 'Aston Martin' in cars: True membership_not_in = 'Mercedes' not in cars: True |
Python Bitwise Operators
Python bitwise operators are used to compare (binary) numbers: