Operators in Python

In Python, operators are used to perform operations on variables and values. Python supports a wide variety of operators, which can be categorized as follows:

1. Arithmetic Operators

These operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, etc.

OperatorDescriptionExample
+Addition5 + 3 results in 8
-Subtraction5 - 3 results in 2
*Multiplication5 * 3 results in 15
/Division5 / 3 results in 1.666...
//Floor Division5 // 3 results in 1
%Modulus (remainder)5 % 3 results in 2
**Exponentiation5 ** 3 results in 125

Example:

x = 10
y = 3
print(x + y)  # Addition: 10 + 3 = 13
print(x - y)  # Subtraction: 10 - 3 = 7
print(x * y)  # Multiplication: 10 * 3 = 30
print(x / y)  # Division: 10 / 3 = 3.3333...
print(x // y) # Floor Division: 10 // 3 = 3
print(x % y)  # Modulus: 10 % 3 = 1
print(x ** y) # Exponentiation: 10 ** 3 = 1000

2. Comparison Operators

Comparison operators are used to compare two values and return a Boolean result (True or False).

OperatorDescriptionExample
==Equal to5 == 3 results in False
!=Not equal to5 != 3 results in True
>Greater than5 > 3 results in True
<Less than5 < 3 results in False
>=Greater than or equal to5 >= 3 results in True
<=Less than or equal to5 <= 3 results in False

Example:

x = 10
y = 5
print(x == y)  # False
print(x != y)  # True
print(x > y)   # True
print(x < y)   # False
print(x >= y)  # True
print(x <= y)  # False

3. Logical Operators

Logical operators are used to combine conditional statements.

OperatorDescriptionExample
andReturns True if both statements are trueTrue and False results in False
orReturns True if one of the statements is trueTrue or False results in True
notReverses the result, returns True if the statement is falsenot True results in False

Example:

x = 10
y = 5
print(x > y and y > 3)   # True
print(x < y or y > 3)    # True
print(not(x > y))        # True

4. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns a valuex = 10
+=Adds and assigns the valuex += 5 (equivalent to x = x + 5)
-=Subtracts and assigns the valuex -= 5 (equivalent to x = x - 5)
*=Multiplies and assigns the valuex *= 5 (equivalent to x = x * 5)
/=Divides and assigns the valuex /= 5 (equivalent to x = x / 5)
//=Floor divides and assigns the valuex //= 5 (equivalent to x = x // 5)
%=Modulus and assigns the valuex %= 5 (equivalent to x = x % 5)
**=Exponentiates and assigns the valuex **= 5 (equivalent to x = x ** 5)

Example:

x = 10
x += 5  # x = x + 5 -> 15
x -= 3  # x = x - 3 -> 12
x *= 2  # x = x * 2 -> 24
x /= 4  # x = x / 4 -> 6.0

5. Membership Operators

Membership operators are used to test if a sequence (like a list, tuple, or string) contains a value.

OperatorDescriptionExample
inReturns True if the value is found in the sequence'a' in 'apple' results in True
not inReturns True if the value is not found in the sequence'b' not in 'apple' results in True

Example:

x = [1, 2, 3, 4, 5]
print(3 in x)  # True
print(6 not in x)  # True

6. Identity Operators

Identity operators are used to compare the memory locations of two objects.

OperatorDescriptionExample
isReturns True if both variables point to the same objectx is y
is notReturns True if both variables do not point to the same objectx is not y

Example:

x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y)     # True
print(x is z)     # False
print(x is not z) # True

Copyright © 2025 Devship. All rights reserved.

Made by imParth