In Python, an operator is a symbol that performs a specific operation on one, two, or more operands. Operands are the values or variables that the operator acts on. Operators are fundamental to writing Python code and are used to perform various tasks such as addition, subtraction, comparison, and much more.
Types of Operators in Python
There are several types of operators in Python. We will go over the most commonly used ones:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Membership Operators
- Identity Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, etc.
Example 1: Arithmetic Operators
# Arithmetic Operations
a = 10
b = 5
# Addition
print(a + b) # Output: 15
# Subtraction
print(a - b) # Output: 5
# Multiplication
print(a * b) # Output: 50
# Division
print(a / b) # Output: 2.0
# Modulus (remainder)
print(a % b) # Output: 0
# Exponentiation (power)
print(a ** b) # Output: 100000
# Floor Division (rounds down the result)
print(a // b) # Output: 2
Explanation:
+
adds two numbers.-
subtracts the second number from the first.*
multiplies two numbers./
divides the first number by the second.%
gives the remainder after division.**
raises the first number to the power of the second.//
divides and rounds down the result.
2. Comparison Operators
Comparison operators are used to compare two values. They return either True
or False
based on whether the comparison is correct.
Example 2: Comparison Operators
a = 10
b = 5
# Equal to
print(a == b) # Output: False
# Not equal to
print(a != b) # Output: True
# Greater than
print(a > b) # Output: True
# Less than
print(a < b) # Output: False
# Greater than or equal to
print(a >= b) # Output: True
# Less than or equal to
print(a <= b) # Output: False
Explanation:
==
checks if two values are equal.!=
checks if two values are not equal.>
checks if the first value is greater than the second.<
checks if the first value is less than the second.>=
checks if the first value is greater than or equal to the second.<=
checks if the first value is less than or equal to the second.
3. Logical Operators
Logical operators are used to combine multiple conditional statements. They are mostly used in decision-making statements like if
and while
.
Example 3: Logical Operators
a = True
b = False
# AND (both conditions must be True)
print(a and b) # Output: False
# OR (at least one condition must be True)
print(a or b) # Output: True
# NOT (reverses the Boolean value)
print(not a) # Output: False
Explanation:
and
returnsTrue
if both conditions areTrue
.or
returnsTrue
if at least one condition isTrue
.not
reverses the Boolean value (True
becomesFalse
and vice versa).
4. Assignment Operators
Assignment operators are used to assign values to variables.
Example 4: Assignment Operators
a = 10 # Assign value 10 to variable a
b = 5
# Add and assign
a += b # a = a + b
print(a) # Output: 15
# Subtract and assign
a -= b # a = a - b
print(a) # Output: 10
# Multiply and assign
a *= b # a = a * b
print(a) # Output: 50
# Divide and assign
a /= b # a = a / b
print(a) # Output: 10.0
Explanation:
=
assigns a value to a variable.+=
,-=
,*=
,/=
, etc., perform the operation and assign the result to the variable.
5. Membership Operators
Membership operators are used to test if a value is in a sequence (like a list or string).
Example 5: Membership Operators
# Using 'in' and 'not in' with a list
fruits = ['apple', 'banana', 'orange']
print('apple' in fruits) # Output: True
print('grape' not in fruits) # Output: True
Explanation:
in
checks if a value is present in a sequence.not in
checks if a value is not present in a sequence.
6. Identity Operators
Identity operators are used to compare objects based on their memory locations.
Example 6: Identity Operators
a = [1, 2, 3]
b = [1, 2, 3]
# 'is' checks if both variables refer to the same object
print(a is b) # Output: False
# 'is not' checks if both variables do not refer to the same object
print(a is not b) # Output: True
Explanation:
is
returnsTrue
if both variables point to the same object in memory.is not
returnsTrue
if both variables do not point to the same object in memory.
7. Bitwise Operators
Bitwise operators work on binary numbers (i.e., numbers represented as sequences of bits).
Example 7: Bitwise Operators
a = 5 # In binary: 101
b = 3 # In binary: 011
# Bitwise AND
print(a & b) # Output: 1 (binary: 001)
# Bitwise OR
print(a | b) # Output: 7 (binary: 111)
# Bitwise XOR
print(a ^ b) # Output: 6 (binary: 110)
# Bitwise NOT
print(~a) # Output: -6 (inverts all bits)
# Bitwise Left Shift
print(a << 1) # Output: 10 (binary: 1010)
# Bitwise Right Shift
print(a >> 1) # Output: 2 (binary: 010)
Explanation:
&
performs bitwise AND.|
performs bitwise OR.^
performs bitwise XOR.~
performs bitwise NOT (inverts bits).<<
performs a left shift.>>
performs a right shift.