Definition

Operators are special symbols or keywords used to perform operations on variables and values. They are used for arithmetic calculations, comparisons, logical decisions, bitwise operations, assignments, and checking membership or identity.

Example:

a = 10
b = 5

print(a + b)

Output:

15

Types of Operators in Python

Python provides the following types of operators:

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

1. Arithmetic Operators

These operators perform mathematical calculations.

OperatorDescriptionExample
+Addition10 + 5 = 15
-Subtraction10 - 5 = 5
*Multiplication10 * 5 = 50
/Division10 / 5 = 2.0
//Floor Division10 // 3 = 3
%Modulus (Remainder)10 % 3 = 1
**Exponent (Power)2 ** 3 = 8

Example

a = 10
b = 3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Power:", a ** b)

Output

Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Power: 1000

2. Comparison (Relational) Operators

These operators compare two values and return True or False.

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

x = 10
y = 20

print(x == y)
print(x != y)
print(x > y)
print(x &lt; y)
print(x >= y)
print(x &lt;= y)

Output

False
True
False
True
False
True

3. Assignment Operators

Assignment operators assign values to variables.

OperatorExampleEquivalent To
=x = 5Assign value
+=x += 2x = x + 2
-=x -= 2x = x - 2
*=x *= 2x = x * 2
/=x /= 2x = x / 2
%=x %= 2x = x % 2
//=x //= 2x = x // 2
**=x **= 2x = x ** 2

Example

x = 10
x += 5
print(x)

Output

15

4. Logical Operators

Logical operators combine or negate conditions.

OperatorDescription
andTrue if both conditions are true
orTrue if at least one condition is true
notReverses the result

Example

a = True
b = False

print(a and b)
print(a or b)
print(not a)

Output

False
True
False

5. Bitwise Operators

Bitwise operators work on the binary representation of integers.

OperatorDescription
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

Example

a = 5
b = 3

print(a &amp; b)
print(a | b)
print(a ^ b)

Output

1
7
6

6. Membership Operators

Membership operators check whether a value exists in a sequence.

OperatorDescription
inReturns True if present
not inReturns True if not present

Example

numbers = [10, 20, 30]

print(20 in numbers)
print(40 not in numbers)

Output

True
True

7. Identity Operators

Identity operators compare whether two variables refer to the same object.

OperatorDescription
isTrue if both variables refer to the same object
is notTrue if they refer to different objects

Example

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

print(a is b)
print(a is c)
print(a is not c)

Output

True
False
True

Operator Precedence (Highest to Lowest)

  1. () (Parentheses)
  2. ** (Exponent)
  3. *, /, //, %
  4. +, -
  5. Comparison operators (==, !=, <, >, <=, >=)
  6. not
  7. and
  8. or

Advantages of Operators

  • Simplify calculations and expressions.
  • Reduce the amount of code.
  • Improve readability.
  • Support decision-making and logical operations.
  • Enable efficient data manipulation.

Related Posts