Back to Python
2026-02-125 min read

Types of Python Operators

Learn Types of Python Operators step by step with clear examples and exercises.

Why This Matters

Welcome to this detailed guide on Python operators! This tutorial is designed to help you understand and master the various types of operators available in Python, with a focus on practical depth, real-world examples, and common mistakes that can occur during coding. Let's dive into the world of Python operators!

Why This Matters

Python operators are essential for writing efficient and effective code. They allow you to perform various operations such as arithmetic, comparison, assignment, logical, and bitwise operations. Understanding Python operators is crucial for solving complex problems, debugging your code, and preparing for interviews or exams.

Prerequisites

Before diving into Python operators, it's essential to have a basic understanding of the following:

  • Familiarity with Python syntax and variables
  • Understanding of data types in Python (e.g., integers, floats, strings)

Core Concept

Python offers several categories of operators, each serving a unique purpose. Let's explore them one by one.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. Here are the most common arithmetic operators in Python:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • //: Floor division (returns the largest whole number)
  • %: Modulus (remainder of division)
  • **: Exponentiation (raising a number to a power)**

Worked Example

a = 5

b = 3

print(a + b) # Output: 8

print(a - b) # Output: 2

print(a * b) # Output: 15

print(a / b) # Output: 1.666666666667 (float)

print(a // b) # Output: 1 (integer division)

print(a % b) # Output: 2 (remainder of division)

print(a b) # Output: 125 (exponentiation)


### Comparison Operators

Comparison operators are used to compare two operands and return a boolean value (True or False). Here are the comparison operators in Python:

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

Worked Example

a = 5

b = 3

print(a == b) # Output: False

print(a != b) # Output: True

print(a < b) # Output: True

print(a > b) # Output: False

print(a <= b) # Output: True

print(a >= b) # Output: False


### Assignment Operators

Assignment operators are used to assign values to variables. Here are the most common assignment operators in Python:

- `=`: Simple assignment
- `+=`: Addition assignment (equivalent to `x = x + y`)
- `-=`: Subtraction assignment (equivalent to `x = x - y`)
- `*=`: Multiplication assignment (equivalent to `x = x * y`)
- `/=`: Division assignment (equivalent to `x = x / y`)
- `//=`: Floor division assignment (equivalent to `x = x // y`)
- `%=`: Modulus assignment (equivalent to `x = x % y`)
- `**=`: Exponentiation assignment (equivalent to `x = x ** y`)

Worked Example

a = 5

b = 3

a += b # Equivalent to a = a + b

print(a) # Output: 8

a -= b # Equivalent to a = a - b

print(a) # Output: 5


### Logical Operators

Logical operators are used to combine conditional statements. Here are the logical operators in Python:

- `and`: Returns True if both conditions are True
- `or`: Returns True if at least one condition is True
- `not`: Negates a boolean value (True becomes False, and vice versa)

Worked Example

x = 5

y = 10

print(x < y and x > 3) # Output: True

print(x < y or x > 20) # Output: True

print(not (x == 5)) # Output: False


### Bitwise Operators

Bitwise operators are used to perform operations on the individual bits of integers. Here are the bitwise operators in Python:

- `&`: Bitwise AND (returns a bit if both corresponding bits are set)
- `|`: Bitwise OR (returns a bit if at least one corresponding bit is set)
- `^`: Bitwise XOR (returns a bit if only one corresponding bit is set)
- `~`: Bitwise NOT (inverts all the bits of an integer)
- `<<`: Bitwise left shift (shifts bits to the left)
- `>>`: Bitwise right shift (shifts bits to the right)

Worked Example

x = 60 # binary: 111100

y = 13 # binary: 1101

print(x & y) # Output: 12 (binary: 1100)

print(x | y) # Output: 61 (binary: 111101)

print(x ^ y) # Output: 49 (binary: 101001)

print(~x) # Output: -61 (binary: 10000011)

print(x << 1) # Output: 120 (binary: 11110000)

print(x >> 1) # Output: 30 (binary: 00111100)

Worked Example

Let's consider a simple problem where we need to find the largest of three numbers using Python operators.

Problem: Find the largest of three numbers

num1 = 25

num2 = 34

num3 = 18

if num1 > num2 and num1 > num3:

print(f"{num1} is the largest number.")

elif num2 > num1 and num2 > num3:

print(f"{num2} is the largest number.")

else:

print(f"{num3} is the largest number.")


In this example, we use comparison operators to compare the three numbers and logical operators to determine which one is the largest.

Common Mistakes

  • Forgetting to include the assignment operator (e.g., x = x + 1 instead of x += 1)
  • Using the wrong operator for a specific task (e.g., using == when you meant to use =)
  • Neglecting to handle edge cases in logical expressions (e.g., considering only positive numbers)
  • Mixing up bitwise and arithmetic operators (e.g., using & instead of *)

Practice Questions

  1. Write a Python script that calculates the sum, difference, product, and quotient of two numbers using arithmetic operators.
  2. Write a Python script that checks if a number is even or odd using comparison operators.
  3. Write a Python script that finds the largest of four numbers using logical operators.
  4. Write a Python script that swaps the values of two variables without using a temporary variable.
  5. Write a Python script that calculates the factorial of a number using recursion and multiplication operators.

FAQ

What is the difference between == and = in Python?

  • == is the equality operator, which checks if two operands are equal.
  • = is the assignment operator, which assigns a value to a variable.

How do I perform bitwise operations on floating-point numbers in Python?

  • Python does not support bitwise operations on floating-point numbers directly. You can convert them to integers first and then perform bitwise operations.

What is the purpose of the // operator in Python?

  • The // operator performs floor division, which returns the largest whole number that, when multiplied by the divisor, does not exceed the dividend.

How do I find the remainder of a division operation in Python?

  • You can use the modulus operator (%) to find the remainder of a division operation.

What is the difference between < and <= in Python?

  • < checks if the left operand is less than the right operand, while <= checks if the left operand is less than or equal to the right operand.
Types of Python Operators | Python | XQA Learn