Operators In Python
Operators in Python are symbols that perform operations on variables and values. This tutorial covers various types of operators and how to use them in Python.
Example
# Using + Operator for addition
sum = 1 + 2
print(sum)
Types Of Operators
Here is the list of different type of operators we will learn in this tutorial:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
As you can see there are around 7 types of operators so let’s see each one of them one by one.
1. Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations such as addition, subtraction, multiplication, division etc.
Operator | Name | Example |
+ | Additioin | a + b |
– | Subtraction | a – b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
** | Exponentiation | a ** b |
// | Floor division | a // b |
Example
x = 10
y = 3
# Addition
result_addition = x + y
print("Addition:", result_addition)
# Subtraction
result_subtraction = x - y
print("Subtraction:", result_subtraction)
# Multiplication
result_multiplication = x * y
print("Multiplication:", result_multiplication)
# Division
result_division = x / y
print("Division:", result_division)
# Modulus (remainder)
result_modulus = x % y
print("Modulus (remainder):", result_modulus)
# Exponentiation
result_exponentiation = x ** y
print("Exponentiation:", result_exponentiation)
# Floor Division
result_floor_division = x // y
print("Floor Divsion:", result_floor_division)
2. Comparison Operators
Comparison operators are used to compare values and return a Boolean result
Operator | Name | Example |
== | Equal To | a == b |
!= | Not Equal To | a != b |
> | Greater Than | a > b |
< | Less Than | a < b |
>= | Greater Than or Equal To | a >= b |
<= | Less Than or Equal To | a <= b |
Example
x = 10
y = 5
# Equal to
is_equal = x == y
print("Equal to:", is_equal)
# Not equal to
is_not_equal = x != y
print("Not equal to:", is_not_equal)
# Greater than
is_greater_than = x > y
print("Greater than:", is_greater_than)
# Less than
is_less_than = x < y
print("Less than:", is_less_than)
# Greater than or equal to
is_greater_equal = x >= y
print("Greater than or equal to:", is_greater_equal)
# Less than or equal to
is_less_equal = x <= y
print("Less than or equal to:", is_less_equal)
3. Logical Operators
A logical operators are used to connect two or more expressions.
Operator | Name | Example |
and | Returns True if both statements are true | a < 4 and a < 9 |
or | Returns True if one of the statements is true | a < 4 or a < 3 |
not | Reverse the result, returns False if the result is true | not(a < 4 and a < 9) |
Example
x = True
y = False
# AND
result_and = x and y
print("Logical AND:", result_and)
# OR
result_or = x or y
print("Logical OR:", result_or)
# NOT
result_not = not x
print("Logical NOT:", result_not)
4. Assignment Operators
Assignment operators are used to assign values.
Operator | Name | Example |
= | Assigns the value on the right to the variable on the left | x = 4 |
+= | Adds and assigns in one step. | x += 2 |
-= | Subtracts and assigns in one step. | x -= 2 |
*= | Multiplies and assigns in one step. | x *= 2 |
/= | Divides and assigns in one step. | x /= 2 |
Example
x = 5
# += (Addition assignment)
x += 3
print("Addition assignment:", x)
# -= (Subtraction assignment)
x -= 2
print("Subtraction assignment:", x)
# *= (Multiplication assignment)
x *= 4
print("Multiplication assignment:", x)
# /= (Division assignment)
x /= 2
print("Division assignment:", x)
5. Identity Operators
Identity operators are used to compare the memory locations of two objects..
Operator | Name | Example |
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
Example
x = [1, 2, 3]
y = [1, 2, 3]
# is (True if the variables reference the same object)
is_same_object = x is y
print("is (same object):", is_same_object)
# is not (True if the variables reference different objects)
is_different_object = x is not y
print("is not (different object):", is_different_object)
6. Membership Operators
Membership operators are used to check if a value is a member of a sequence (e.g., list, tuple).
Operator | Name | Example |
in | Returns True if the specified value is present in the object sequence | x in y |
not in | Returns True if the specified value is not present in the object sequence | x not in y |
Example
numbers = [1, 2, 3, 4, 5]
# in (True if the value is in the sequence)
is_in_sequence = 3 in numbers
print("in (in sequence):", is_in_sequence)
# not in (True if the value is not in the sequence)
is_not_in_sequence = 6 not in numbers
print("not in (not in sequence):", is_not_in_sequence)
7. Bitwise Operators
Bitwise operators in Python manipulate individual bits of binary numbers.
Operator | Name | Example |
& | AND | x & y |
| | OR | x | y |
^ | XOR | x ^ y |
~ | NOT | ~x |
<< | Zero fill left shift | x << 2 |
>> | Signed right shift | x >> 2 |
Example
# Bitwise AND
x = 5 # 0b0101 in binary
y = 3 # 0b0011 in binary
result_and = x & y
print("Bitwise AND:", result_and)
# Bitwise OR
result_or = x | y
print("Bitwise OR:", result_or)
# Bitwise XOR
result_xor = x ^ y
print("Bitwise XOR:", result_xor)
# Bitwise NOT
result_not_x = ~x
print("Bitwise NOT (x):", result_not_x)
# Bitwise Left Shift
result_left_shift = x << 1
print("Bitwise Left Shift:", result_left_shift)
# Bitwise Right Shift
result_right_shift = x >> 1
print("Bitwise Right Shift:", result_right_shift)
So these were all the operators in python, don't worry you will not require to memorise all of them there are few which will be used most of the time.