CS Engineering Gyan

Operators in Python

In the previous chapter, we looked at how variables store values and how Python converts values between different data types. Storing values is only useful if a program can actually do something with them, and that is exactly the role operators play. Operators are the special symbols and keywords Python uses to perform calculations, make comparisons, combine conditions, and manipulate values at the level of individual bits.

An operator is a symbol that tells Python to perform a specific operation on one or more values, referred to as operands. Python organizes its operators into several distinct categories based on the kind of operation they perform, and understanding each category clearly makes it much easier to read and write expressions confidently, whether you are calculating a total, comparing two values, or checking whether a complex condition is true.

In this tutorial, you will learn about all six major categories of Python operators: arithmetic, assignment, comparison, logical, identity, and bitwise operators, with worked examples for each one.


Types of Python Operators

Python's operators can be grouped into six broad categories, based on the kind of task they are designed to perform. The diagram below summarizes these six categories before each one is explained individually with examples in the sections that follow.

Types of Python Operators Arithmetic Operators Assignment Operators Comparison Operators Logical Operators Identity Operator Bitwise Operators

Arithmetic Operators

Arithmetic operators perform standard mathematical calculations on numeric values, such as addition, subtraction, multiplication, and division. Python also provides a few less common but very useful arithmetic operators, including floor division, which divides and rounds down to the nearest whole number, and the modulus operator, which returns the remainder of a division.

Example

views_day1 = 1500

views_day2 = 400

print(views_day1 + views_day2)

print(views_day1 - views_day2)

print(views_day1 * 2)

print(views_day1 / 4)

print(views_day1 // 4)

print(views_day1 % 4)

Output

1900

1100

3000

375.0

375

0

Here, the CS Engineering Gyan channel's daily view counts are combined and manipulated using every core arithmetic operator. Notice that regular division using the forward slash always returns a float, even when the result divides evenly, while floor division using a double forward slash returns a whole number by rounding down.


Assignment Operators

Assignment operators are used to assign values to variables, and Python also provides a set of compound assignment operators that combine a calculation with an assignment in a single step, updating a variable based on its own current value.

Example

subscribers = 60000

subscribers += 500

print(subscribers)

subscribers -= 200

print(subscribers)

subscribers *= 2

print(subscribers)

Output

60500

60300

120600

The += operator here adds 500 to the current value of subscribers and stores the result back into the same variable, which is considerably shorter than writing subscribers = subscribers + 500 in full. The same shorthand pattern applies to subtraction, multiplication, division, and every other arithmetic operation.


Comparison Operators

Comparison operators, sometimes called relational operators, compare two values and always produce a Boolean result, either True or False, depending on whether the comparison holds. These operators are especially important for writing conditions used in decision-making statements.

Example

video1_views = 5200

video2_views = 4800

print(video1_views > video2_views)

print(video1_views == video2_views)

print(video1_views != video2_views)

print(video1_views <= 5200)

Output

True

False

True

True

Notice the difference between a single equals sign, which assigns a value, and a double equals sign, which compares two values for equality. Confusing these two is one of the most common mistakes beginners make when first learning comparison operators.


Logical Operators

Logical operators combine multiple conditions together, allowing a program to check whether several conditions are true at once, whether at least one of several conditions is true, or to reverse the result of a single condition entirely. Python uses the plain English words and, or, and not for this purpose, rather than special symbols.

Example

subscribed = True

notifications_on = False

print(subscribed and notifications_on)

print(subscribed or notifications_on)

print(not notifications_on)

Output

False

True

True

The and operator only produces True when both conditions are True, the or operator produces True when at least one condition is True, and the not operator simply reverses whatever Boolean value follows it.


Identity Operators

Identity operators check whether two variables actually refer to the exact same object in memory, rather than simply checking whether their values look equal. Python provides two identity operators, is and is not, which are especially useful when working with mutable objects like lists.

Example

playlist_a = ["Arrays", "Pointers"]

playlist_b = ["Arrays", "Pointers"]

playlist_c = playlist_a

print(playlist_a == playlist_b)

print(playlist_a is playlist_b)

print(playlist_a is playlist_c)

Output

True

False

True

Here, playlist_a and playlist_b contain identical values, so the equality operator returns True, but they are actually two completely separate list objects stored at different locations in memory, so the identity operator returns False. Since playlist_c was assigned directly from playlist_a, both names refer to the exact same underlying object, so the identity check between them returns True.


Bitwise Operators

Bitwise operators work directly on the individual binary digits that make up an integer, performing operations such as AND, OR, XOR, and bit shifting. These operators are used far less frequently in everyday programming than the other categories, but they remain important for tasks involving low-level data manipulation, such as working with flags or optimizing certain calculations.

Example

a = 6

b = 3

print(a & b)

print(a | b)

print(a ^ b)

print(a << 1)

print(a >> 1)

Output

2

7

5

12

3

Here, a and b are treated as their underlying binary representations, 110 and 011, and each bitwise operator compares or shifts these bit patterns directly. The AND operator keeps only the bits that are set in both numbers, the OR operator keeps bits set in either number, the XOR operator keeps bits that differ between the two numbers, and the shift operators move every bit left or right by the specified number of positions.


Summary Table of Python Operator Categories

Category Purpose Example Symbols
Arithmetic Perform mathematical calculations +, -, *, /, //, %
Assignment Assign or update a variable's value =, +=, -=, *=
Comparison Compare two values and return True or False ==, !=, >, <
Logical Combine or reverse Boolean conditions and, or, not
Identity Check whether two variables refer to the same object is, is not
Bitwise Operate directly on binary bit patterns &, |, ^, <<, >>

Advantages and Limitations of Python's Operators

Advantages Limitations
Compound assignment operators keep code short and readable. Confusing = and == is a very common source of beginner errors.
Logical operators use plain English words, making conditions easy to read. Identity operators are easy to misuse in place of equality checks.
Bitwise operators allow efficient, low-level manipulation when genuinely needed. Bitwise operations can be difficult to read without a solid grasp of binary numbers.

Best Practices While Using Python Operators


Common Mistakes Beginners Make

Mistake Correct Practice
Using a single equals sign when a comparison was intended. Remember that = assigns a value, while == compares two values for equality.
Using is to compare values instead of checking object identity. Use == for comparing values, and reserve is specifically for checking whether two names refer to the same object.
Assuming regular division always returns a whole number. Remember that the forward slash always returns a float, while floor division rounds down to a whole number.
Confusing bitwise operators with logical operators. Remember that and, or, and not work on Boolean values, while &, |, and ^ work directly on binary bit patterns.

Frequently Asked Interview Questions

  1. What is an operator in Python?
    An operator is a symbol that tells Python to perform a specific operation, such as a calculation or comparison, on one or more operands.
  2. What are the main categories of operators in Python?
    The main categories are arithmetic, assignment, comparison, logical, identity, and bitwise operators.
  3. What is the difference between the / and // operators?
    The / operator performs regular division and always returns a float, while // performs floor division and returns a whole number rounded down.
  4. What is the difference between = and == in Python?
    The = operator assigns a value to a variable, while == compares two values and returns True or False.
  5. What do the logical operators and, or, and not do?
    and returns True only when both conditions are true, or returns True when at least one condition is true, and not reverses a single Boolean value.
  6. What is the difference between == and is in Python?
    == checks whether two values are equal, while is checks whether two variables refer to the exact same object in memory.
  7. What do bitwise operators work on?
    Bitwise operators work directly on the individual binary digits of an integer, rather than on its overall value.
  8. What does a compound assignment operator like += do?
    A compound assignment operator like += updates a variable by performing a calculation using its current value and then storing the result back into the same variable.

Summary

Python organizes its operators into six clear categories, each designed for a specific kind of task. Arithmetic operators handle calculations, assignment operators store and update variable values, and comparison operators produce Boolean results used throughout decision-making code.

Logical operators combine or reverse conditions using readable English words, identity operators check whether two variables genuinely refer to the same underlying object, and bitwise operators provide direct access to an integer's binary representation for more specialized, low-level tasks. Together, these six categories give a Python program everything it needs to calculate, compare, and combine values in almost any situation.

With operators covered, you are now ready to explore control statements in Python, learning how conditions built using comparison and logical operators can actually control which parts of a program run, using tools like if, else, and loops.


← Previous: Variables & Data Types Next: Control Statements →

Home Visit Our YouTube Channel