Python Operators


Python is a versatile language, and one of the fundamental concepts for any Python programmer to understand is operators. Operators in Python are special symbols that perform operations on variables or values. Python provides a wide range of operators that you can use to perform mathematical, logical, and comparison operations.

In this blog post, we will dive into the different types of Python operators, how they work, and practical examples to illustrate their use.


What Are Python Operators?

In Python, an operator is a symbol that performs operations on variables or values. Python has a variety of operators, including:

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Each type of operator serves a specific purpose and is used in various scenarios in your Python code.


1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

Common Arithmetic Operators:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division (returns float)
  • // : Floor Division (returns integer result)
  • % : Modulus (returns the remainder of division)
  • ** : Exponentiation (raises the power)

Example:

# Arithmetic Operators
a = 10
b = 3

print("Addition: ", a + b)   # Output: 13
print("Subtraction: ", a - b)  # Output: 7
print("Multiplication: ", a * b)  # Output: 30
print("Division: ", a / b)   # Output: 3.333...
print("Floor Division: ", a // b)  # Output: 3
print("Modulus: ", a % b)   # Output: 1
print("Exponentiation: ", a ** b)  # Output: 1000

2. Comparison Operators

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

Common Comparison Operators:

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

Example:

# Comparison Operators
x = 5
y = 10

print(x == y)  # Output: False (5 is not equal to 10)
print(x != y)  # Output: True (5 is not equal to 10)
print(x > y)   # Output: False (5 is not greater than 10)
print(x < y)   # Output: True (5 is less than 10)
print(x >= y)  # Output: False (5 is not greater than or equal to 10)
print(x <= y)  # Output: True (5 is less than or equal to 10)

3. Logical Operators

Logical operators are used to combine conditional statements. They allow us to check multiple conditions at once.

Common Logical Operators:

  • and : Returns True if both conditions are true
  • or : Returns True if at least one condition is true
  • not : Reverses the Boolean value

Example:

# Logical Operators
a = True
b = False

print(a and b)   # Output: False (both conditions are not true)
print(a or b)    # Output: True (at least one condition is true)
print(not a)     # Output: False (reverses the Boolean value of a)

4. Assignment Operators

Assignment operators are used to assign values to variables. These operators are used to store data in variables in various ways.

Common Assignment Operators:

  • = : Assigns value
  • += : Adds and assigns the value
  • -= : Subtracts and assigns the value
  • *= : Multiplies and assigns the value
  • /= : Divides and assigns the value
  • //= : Floor divides and assigns the value
  • %= : Modulus and assigns the value
  • **= : Exponentiation and assigns the value

Example:

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

x -= 2  # x = x - 2
print(x)  # Output: 6

x *= 4  # x = x * 4
print(x)  # Output: 24

x /= 6  # x = x / 6
print(x)  # Output: 4.0

5. Bitwise Operators

Bitwise operators are used to perform operations on binary numbers (bits). These operators work on integers and perform bit-level operations.

Common Bitwise Operators:

  • & : AND
  • | : OR
  • ^ : XOR
  • ~ : NOT (inverts bits)
  • << : Left shift
  • >> : Right shift

Example:

# Bitwise Operators
a = 10  # Binary: 1010
b = 4   # Binary: 0100

print(a & b)  # Output: 0 (AND operation)
print(a | b)  # Output: 14 (OR operation)
print(a ^ b)  # Output: 14 (XOR operation)
print(~a)     # Output: -11 (NOT operation)
print(a << 1) # Output: 20 (Left shift)
print(a >> 1) # Output: 5  (Right shift)

6. Membership Operators

Membership operators are used to test if a value or variable is found in a sequence (like a list, tuple, or string).

Common Membership Operators:

  • in : Returns True if a value is found in the sequence
  • not in : Returns True if a value is not found in the sequence

Example:

# Membership Operators
fruits = ["apple", "banana", "cherry"]

print("apple" in fruits)  # Output: True
print("orange" not in fruits)  # Output: True

7. Identity Operators

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

Common Identity Operators:

  • is : Returns True if both variables point to the same object
  • is not : Returns True if both variables do not point to the same object

Example:

# Identity Operators
a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a is b)     # Output: False (a and b are not the same object)
print(a is c)     # Output: True (a and c are the same object)
print(a is not b) # Output: True (a and b are not the same object)