An operator in Python is a symbol which performs some mathematical operations between operands. For example, an assignment operator(=) is used to store values in variables, an addition operator(+) is used to add numbers.
Python provides us several groups of operators. These are as follows:
- Arithmetic operators
- Comparison operators
- Bitwise operators
- Assignment operators
- Logical operators
- Identity operators
- Membership operators
Python Arithmetic Operators
Arithmetic operators are used to perform simple mathematical operations like addition, subtraction, multiplication, etc. Arithmetic operators are as follows:
Operator Symbol | Operator Name | Example |
+ | Addition | a + b |
– | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
** | Exponent | a ** b |
// | Floor division | a // b |
Example 1: Arithmetic Operators in Python
# A program to demonstrate arithmetic operators in Python a = 3 b = 2 # Addition operator print('a + b = ', a+b) # Subtraction operator print('a - b = ', a-b) # Multiplication operator print('a * b = ', a*b) # Division operator print('a / b = ', a/b) # Modulus operator print('a % b = ', a%b) # Exponent operator print('a ** b = ', a**b) # Floor division operator print('a // b = ', a//b)
Output:
a + b = 5
a - b = 1
a * b = 6
a / b = 1.5
a % b = 1
a ** b = 9
a // b = 1
Python Comparison Operators
Comparison operators are used to compare two operands. The result of a comparison is always True or False.
Operator Symbol | Operator Name | Example |
== | Equal | a == b |
!= | Not equal | a != b |
< | Less than | a < b |
> | Greater than | a > b |
<= | Less than or equal to | a <= b |
>= | Greater than or equal to | a >= b |
Example 2: Comparison Operators in Python
# A sample program to demonstrate comparison operators in Python a = 10 b = 20 # Equal operator print('a == b is: ', a==b) # Not equal operator print('a != b is: ', a!=b) # Less than operator print('a < b is: ', a<b) # Greater than operator print('a > b is: ', a>b) # Less than or equal operator print('a <= b is: ',a<=b) # Greater than or equal operator print('a >= b is: ', a>=b)
Output:
a == b is: False
a != b is: True
a < b is: True
a > b is: False
a <= b is: True
a >= b is: False
Python Bitwise Operators
Bitwise operators are used to perform bitwise operations(operations on bits). This is done by converting a given number into its binary equivalent and then performing bit by bit operations.
Symbol | Name | Meaning | Example |
& | Bitwise AND | Result is 1 if both bits are 1 | a & b |
| | Bitwise OR | Result is 1 if any of the two bits is 1 | a | b |
~ | Bitwise NOT | Reverts the bit | ~ a |
^ | Bitwise XOR | Result is 1 if both bits are not the same | a ^ b |
<< | Left shift | Shifts each bit towards left | a << 1 |
>> | Right shift | Shifts each bit towards right | a >> 1 |
Example 3: Bitwise Operators in Python
# A program to demonstrate bitwise operators in Python a = 2 # Binary equivalent is: 1 0 b = 3 # Binary equivalent is: 1 1 # Bitwise AND print('a & b is: ', a&b) # Bitwise OR print('a | b is: ', a|b) # Bitwise NOT print(' ~ a is: ', ~a) # Bitwise XOR print('a ^ b is: ', a^b) # Left shift print('a << 1 is: ', a<<1) # Shifts each bit towards left by 1 # Right shift print('a >> 1 is: ', a>>1) # Shifts each bit towards right by 1
Output:
a & b is: 2
a | b is: 3
~ a is: -3
a ^ b is: 1
a << 1 is: 4
a >> 1 is: 1
Python Assignment Operators
Assignment operators are used to assign values to variables. Assignment is done using assignment operator ( = ). The assignment operator can also be used as a compound operator like +=, -=, etc.
# Simple assignment a = 5 # Compound assignment a += 5 # same as a = a + 5
Example 4: Assignment Operators in Python
Operator Symbol | Example | Equivalent to |
= | a = 4 | a = 4 |
+= | a += 4 | a = a + 4 |
-= | a -= 4 | a = a – 4 |
*= | a *= 4 | a = a * 4 |
/= | a /= 4 | a = a / 4 |
%= | a %= 4 | a = a % 4 |
**= | a **= 4 | a = a ** 4 |
//= | a //= 4 | a = a // 4 |
&= | a &= 4 | a = a & 4 |
|= | a |= 4 | a = a | 4 |
^= | a ^= 4 | a = a ^ 4 |
>>= | a >>= 4 | a = a >> 4 |
<<= | a <<= 4 | a = a << 4 |
Python Logical Operators
Logical operators are used to perform logical operations on operands like logical AND, logical OR, etc. The result of the logical operation is always a Boolean(True or False) value.
Operator Symbol | Operator Name | Example | Meaning |
and | Logical AND | a>2 and b>3 | Returns True only if both statements are true |
or | Logical OR | a>2 or b>3 | Returns True if any of the statements are true |
not | Logical NOT | not a | Result is the reverse of the operand |
Example 5: Logical operators in Python
# A program to demonstrate logical operators in Python a = True b = False # Logical AND operator print('a and b is: ', a and b) # Logical OR operator print('a or b is: ', a or b) # Logical NOT operator print('not a is: ', not a)
Output:
a and b is: False
a or b is: True
not a is: False
Python Identity operators
Identity operators are used to check if two variables are equal or not. Here equal means not equal by value but whether both the variables have exactly the same object or not and also the same location in memory. If both the variables have the same address in the memory and also have the same object it simply means that they are equal otherwise not.
Operator | Meaning | Example |
is | Returns True if both variables are exactly the same object(have exactly the same address in the memory) | a is b |
is not | Returns True if both the variables are not exactly the same object(do not have the same address in the memory) | a is not b |
Example 6: Identity operators in Python
# A program to demonstrate identity operators in Python a = [1, 2] b = [1, 2] print('a is b: ', a is b) print('a is not b: ', a is not b) print('id of a: ', id(a)) print('id of b: ', id(b))
Output:
a is b: False
a is not b: True
id of a: 139632750588464
id of b: 139632750591024
In the above example, both a and b have exactly the same value but they are not equal. This is because, both the variables have a different identity(or a different location in memory).
Membership Operators
Python membership operators are used to check the existence of a given variable in a given sequence(string, list, dictionary, etc). If the given variable exists in the sequence, the result is True, otherwise False. There are two membership operators which Python provides.
Operator | Meaning | Example |
in | Evaluates to True if the given variable exists in the sequence. | a in b |
not in | Evaluates to True only if the given variable does not exist in the sequence. | a not in b |
Example 7: Membership operators in Python
# A program to demonstrate membership operator in Python a = 2 b = [1, 2, 3, 4, 5] # The in operator print('a in b is: ', a in b) # The not in operator print('a not in b is: ',a not in b)
Output:
a in b is: True
a not in b is: False