In Python, operators are special symbols or keywords that carry out operations on values and python variables. They serve as a basis for expressions, which are used to modify data and executecomputations. Python contains several operators, each with its unique purpose.
Types of Python Operators: Python language supports various types of operators, which are:
- Arithmetic Operators
- Logical Operators
- Comparison (Relational) Operators
- Conditional operators
- Bitwise Operators
- Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. For example,
Operator | Operation | Example |
---|---|---|
+ | Addition | 2 + 2 = 4 |
- | Subtraction | 2 - 2 = 0 |
* | Multiplication | 2 * 2 = 4 |
/ | Division | 4 / 2 = 2 |
// | Floor Division | 13 // 3 = 4 |
% | Modulo | 7 % 2 = 1 |
** | Power | 2 ** 4 = 16 |
Example 1: Arithmetic Operators in Python
a = 2
b = 2
# addition
print ('Sum: ', a + b)
# subtraction
print ('Subtraction: ', a - b)
# multiplication
print ('Multiplication: ', a * b)
a=4
b=2
# division
print ('Division: ', a / b)
a=13
b=3
# floor division
print ('Floor Division: ', a // b)
a=7
b=2
# modulo
print ('Modulo: ', a % b)
a=2
b=4
# a to the power b
print ('Power: ', a ** b)
OUTPUT
Sum: 4
Subtraction: 0
Multiplication: 4
Division: 2.0
Floor Division: 4
Modulo: 1
Power: 16
=== Code Execution Successful ===
2. Python Logical Operators
Operator | Example | Meaning |
---|---|---|
and | a and b | Logical AND:True only if both the operands are True |
or | a or b | Logical OR:True if at least one of the operands is True |
not | not a | Logical NOT:True if the operand is False and vice-versa. |
Example 2: Logical Operators
# logical AND
print(True and True) # True
print(True and False) # False
# logical OR
print(True or False) # True
# logical NOT
print(not True) # False
True
False
True
False
=== Code Execution Successful ===
3. Relational Operators
Operator | Meaning | Example |
---|---|---|
== | Is Equal To | 2 == 4 gives us False |
!= | Not Equal To | 2 != 4 gives us True |
> | Greater Than | 2 > 4 gives us False |
< | Less Than | 2 < 4 gives us True |
>= | Greater Than or Equal To | 2 >= 4 give us False |
<= | Less Than or Equal To | 2 <= 4 gives us True |
Example 3: Relational Operators
a = 2
b = 4
# equal to operator
print('a == b =', a == b)
# not equal to operator
print('a != b =', a != b)
# greater than operator
print('a > b =', a > b)
# less than operator
print('a < b =', a < b)
# greater than or equal to operator
print('a >= b =', a >= b)
# less than or equal to operator
print('a <= b =', a <= b)
OUTPUT
a == b = False
a != b = True
a > b = False
a < b = True
a >= b = False
a <= b = True
=== Code Execution Successful ===
4. Conditional Operators (Ternary Operators)
In Python, Ternary Operator determines if a condition is true or false and then returns the appropriate value as the result.
Syntax: true_value if condition else false_value
# Program to demonstrate ternary operator
a = 5
b = -20
# python ternary operator
large = "a is largest" if a > b else "b is largest"
print(large)
OUTPUT:
a is largest
5. Bitwise Operators
Python bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format.
Operator | Meaning | Example |
---|---|---|
& Bitwise AND | Sets each bit to 1 if both bits are 1 | a = 12 # 1100 in binary b = 5 # 0101 in binary result = a & b print(result) # Output: 4 (0100 in binary) |
| Bitwise OR | Sets each bit to 1 if one of two bits is 1 | a = 12 # 1100 in binary b = 5 # 0101 in binary result = a | b print(result) # Output: 13 (1101 in binary) |
^Bitwise XOR | Sets each bit to 1 if only one of two bits is 1 | a = 12 # 1100 in binary b = 5 # 0101 in binary result = a ^ b print(result) # Output: 9 (1001 in binary) |
~ Bitwise NOT | Inverts all the bits | a = 12 # 1100 in binary result = ~a print(result) # Output: -13 (in binary: -1101, due to two’s complement representation) |
<< Bitwise Left Shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | a = 5 # 0101 in binary result = a << 2 print(result) # Output: 20 (10100 in binary) |
>> Bitwise Right Shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | a = 20 # 10100 in binary result = a >> 2 print(result) # Output: 5 (0101 in binary) |