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.
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.
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.
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)
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 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.
subscribers = 60000 subscribers += 500 print(subscribers) subscribers -= 200 print(subscribers) subscribers *= 2 print(subscribers)
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, 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.
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)
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 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.
subscribed = True notifications_on = False print(subscribed and notifications_on) print(subscribed or notifications_on) print(not notifications_on)
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 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.
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)
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 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.
a = 6 b = 3 print(a & b) print(a | b) print(a ^ b) print(a << 1) print(a >> 1)
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.
| 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 | 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. |
| 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. |
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.