Operators in Python

Operators in Python are special symbols used to perform operations on variables and values. Python supports various types of operators, including arithmetic, comparison, logical, bitwise, assignment, identity, and membership operators.


1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

OperatorDescriptionExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
//Floor Divisionx // y
%Modulus (Remainder)x % y
**Exponentiationx ** y

Example:

x = 10
y = 3
print(x + y)  # 13
print(x - y)  # 7
print(x * y)  # 30
print(x / y)  # 3.3333
print(x // y) # 3
print(x % y)  # 1
print(x ** y) # 1000

2. Comparison Operators

Comparison operators compare values and return a Boolean result (True or False).

OperatorDescriptionExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

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 conditions are truex > 5 and x < 15
orReturns True if at least one condition is truex > 5 or x < 3
notReverses the logical statenot(x > 5)

Example:

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

4. Assignment Operators

Assignment operators assign values to variables.

OperatorExampleEquivalent to
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 4x = x * 4
/=x /= 2x = x / 2
//=x //= 2x = x // 2
%=x %= 3x = x % 3
**=x **= 2x = x ** 2

Example:

x = 10
x += 5  # Equivalent to x = x + 5
print(x)  # 15

5. Identity Operators

Identity operators compare the memory locations of two objects.

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

Example:

a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)  # True (same memory location)
print(a is c)  # False (different objects with same value)

6. Membership Operators

Membership operators check if a value exists in a sequence (list, tuple, string, etc.).

OperatorDescriptionExample
inReturns True if value is in sequence'a' in 'apple'
not inReturns True if value is not in sequence5 not in [1, 2, 3]

Example:

fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits)  # True
print('grape' not in fruits)  # True

7. Bitwise Operators

Bitwise operators perform operations on binary numbers.

OperatorDescriptionExample
&ANDx & y
``OR
^XORx ^ y
~NOT~x
<<Left Shiftx << 2
>>Right Shiftx >> 2

Example:

x = 5  # Binary: 0101
y = 3  # Binary: 0011
print(x & y)  # 1 (0001)
print(x | y)  # 7 (0111)
print(x ^ y)  # 6 (0110)
print(~x)     # -6 (inverts bits)

Summary

✅ Arithmetic operators perform mathematical calculations.
✅ Comparison operators compare values.
✅ Logical operators are used for conditions.
✅ Assignment operators modify values.
✅ Identity and membership operators check object identity and membership.
✅ Bitwise operators manipulate binary values.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top