Login Register

What Are Operators In Python?

What Are Operators?

Operators in Python are special symbols or keywords used to perform operations on variables and values. They are the building blocks of any programming language and enable developers to manipulate data and variables effectively.

Types of Operators in Python

Python provides multiple types of operators, such as:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators
  • Each type represents a unique purpose in programming, and these all essential for writing efficient code.
  • We can do lots of thing using operators in real-world projects.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

OperatorDescriptionExample
+Addition5 + 3 → 8
Subtraction5 – 3 → 2
*Multiplication5 * 3 → 15
/Division5 / 2 → 2.5
%Modulus (Remainder)5 % 2 → 1
**Exponentiation5 ** 3 → 125
//Floor Division5 // 2 → 2

Example:

x = 10
y = 3

print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1
print(x ** y) # Output: 1000
print(x // y) # Output: 3

2. Comparison Operators

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

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

Example:

a = 10
b = 20

print(a > b) # Output: False
print(a == b) # Output: False
print(a <= b) # Output: True

3. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assignx = 5
+=Add and assignx += 3 → x = x + 3
-=Subtract and assignx -= 3 → x = x – 3
*=Multiply and assignx *= 3 → x = x * 3
/=Divide and assignx /= 3 → x = x / 3
%=Modulus and assignx %= 3 → x = x % 3
**=Exponent and assignx **= 3 → x = x ** 3
//=Floor division and assignx //= 3 → x = x // 3

Example:

x = 5
x += 3
print(x) # Output: 8

x *= 2
print(x) # Output: 16

4. Logical Operators

Logical operators are used to combine conditional statements.

OperatorDescriptionExample
andReturns True if both are TrueTrue and False → False
orReturns True if one is TrueTrue or False → True
notReverses the Boolean valuenot True → False

Example:

x = 10
y = 5

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

5. Bitwise Operators

Bitwise operators work on binary representations of numbers.

OperatorDescriptionExample
&AND5 & 3 → 1
``OR
^XOR5 ^ 3 → 6
~NOT~5 → -6
<<Left Shift5 << 1 → 10
>>Right Shift5 >> 1 → 2

Example:

x = 5  # Binary: 0101
y = 3 # Binary: 0011

print(x & y) # Output: 1 (Binary: 0001)
print(x | y) # Output: 7 (Binary: 0111)
print(x ^ y) # Output: 6 (Binary: 0110)

6. Membership Operators

Membership operators check whether a value exists in a sequence (e.g., string, list, tuple).

OperatorDescriptionExample
inReturns True if present‘a’ in ‘apple’ → True
not inReturns True if not present‘z’ not in ‘apple’ → True

Example:

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

7. Identity Operators

Identity operators compare memory locations of two objects.

OperatorDescriptionExample
isReturns True if same objectx is y
is notReturns True if not same objectx is not y

Example:

x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y) # Output: True
print(x is not z) # Output: True