Operators in Python

Loading

Operators are special symbols in Python that perform computations on values and variables. Python supports various types of operators, each serving a specific purpose in operations like arithmetic calculations, comparisons, logical evaluations, and more.

Types of Operators in Python

Python operators are categorized into the following types:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Identity Operators
  7. Membership Operators

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations such as addition, subtraction, multiplication, etc.

OperatorSymbolExampleOutput
Addition+5 + 38
Subtraction-10 - 46
Multiplication*6 * 318
Division/10 / 25.0
Floor Division//10 // 33
Modulus%10 % 31
Exponentiation**2 ** 38

Example Code:

a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333
print(a // b) # Output: 3
print(a % b) # Output: 1
print(a ** b) # Output: 1000 (10^3)

2. Comparison (Relational) Operators

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

OperatorSymbolExampleOutput
Equal to==5 == 5True
Not equal to!=5 != 3True
Greater than>10 > 3True
Less than<4 < 7True
Greater than or equal to>=6 >= 6True
Less than or equal to<=5 <= 8True

Example Code:

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 for combining multiple conditions.

OperatorSymbolExampleOutput
Logical ANDand(5 > 3) and (10 > 5)True
Logical ORor(5 > 10) or (10 > 5)True
Logical NOTnotnot(5 > 3)False

Example Code:

x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False

4. Bitwise Operators

Bitwise operators perform operations at the binary level.

OperatorSymbolExample (x=5, y=3)Output
AND&5 & 31
OR|5 | 37
XOR^5 ^ 36
NOT~~5-6
Left Shift<<5 << 110
Right Shift>>5 >> 12

Example Code:

a = 5  # Binary: 101
b = 3 # Binary: 011
print(a & b) # Output: 1 (001)
print(a | b) # Output: 7 (111)
print(a ^ b) # Output: 6 (110)
print(~a) # Output: -6
print(a << 1) # Output: 10 (1010)
print(a >> 1) # Output: 2 (10)

5. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorSymbolExampleEquivalent To
Assign=x = 10x = 10
Add and Assign+=x += 5x = x + 5
Subtract and Assign-=x -= 2x = x - 2
Multiply and Assign*=x *= 3x = x * 3
Divide and Assign/=x /= 2x = x / 2
Floor Divide and Assign//=x //= 2x = x // 2
Modulus and Assign%=x %= 3x = x % 3
Exponent and Assign**=x **= 2x = x ** 2

Example Code:

x = 10
x += 5
print(x) # Output: 15
x *= 2
print(x) # Output: 30

6. Identity Operators

Identity operators check if two variables reference the same object in memory.

OperatorSymbolExampleOutput
isisx is yTrue or False
is notis notx is not yTrue or False

Example Code:

a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b) # True (Same object)
print(a is c) # False (Different objects)
print(a is not c) # True

7. Membership Operators

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

OperatorSymbolExampleOutput
inin5 in [1, 2, 3, 5]True
not innot in7 not in [1, 2, 3]True

Example Code:

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

Leave a Reply

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