Python Operators Explained: Arithmetic, Logical, and Comparison
Operators are the building blocks of any logic in programming. They allow you to perform calculations, make comparisons, and execute conditional checks in your code. Mastering Python operators is essential for writing dynamic scripts, algorithm design, and handling control flow.
This practical guide breaks down the core Python operators—arithmetic, comparison, and logical—with simple code examples you can test right away.
Key Takeaways
- Arithmetic operators handle standard mathematical calculations like addition, division, and modulo operations.
- Comparison operators evaluate conditions and always return a boolean value (
TrueorFalse). - Logical operators (
and,or,not) combine multiple boolean conditions. - Understanding operator precedence ensures your mathematical expressions calculate in the expected order.
1. Python Arithmetic Operators
Arithmetic operators perform mathematical calculations on numeric values.
| Operator | Name | Description | Example (x = 10, y = 3) | Result |
+ | Addition | Adds two values | x + y | 13 |
- | Subtraction | Subtracts second value from first | x - y | 7 |
* | Multiplication | Multiplies two values | x * y | 30 |
/ | Division | Divides first value by second (returns float) | x / y | 3.3333 |
% | Modulo | Returns remainder of division | x % y | 1 |
** | Exponentiation | Raises first value to power of second | x ** y | 1000 |
// | Floor Division | Divides and rounds down to nearest whole number | x // y | 3 |
Python
# Modulo example: Checking if a number is even or odd
number = 14
if number % 2 == 0:
print("Even Number") # Outputs: Even Number
2. Python Comparison Operators
Comparison operators evaluate the relationship between two values and always return either True or False.
==(Equal to): ReturnsTrueif both operands are equal (5 == 5$\rightarrow$True).!=(Not equal to): ReturnsTrueif operands are not equal (5 != 3$\rightarrow$True).>(Greater than): ReturnsTrueif left value is larger (10 > 5$\rightarrow$True).<(Less than): ReturnsTrueif left value is smaller (3 < 8$\rightarrow$True).>=(Greater than or equal to): (5 >= 5$\rightarrow$True).<=(Less than or equal to): (4 <= 2$\rightarrow$False).

3. Python Logical Operators
Logical operators combine conditional statements to control program decisions.
The and Operator
Returns True only if both conditions evaluate to True.
Python
age = 22
has_license = True
if age >= 18 and has_license:
print("Allowed to drive.")
The or Operator
Returns True if at least one condition evaluates to True.
Python
is_admin = False
is_editor = True
if is_admin or is_editor:
print("Access granted to dashboard.")
The not Operator
Reverses the boolean result of a condition (turns True into False and vice versa).
Python
logged_in = False
if not logged_in:
print("Please log in to continue.")
Operator Precedence in Python
When combining multiple operators in a single expression, Python evaluates them based on operator precedence (similar to standard algebra rules / PEMDAS):
- Parentheses
()(Highest precedence) - Exponentiation
** - Multiplication, Division, Modulo
*,/,//,% - Addition, Subtraction
+,- - Comparison Operators
==,!=,>,< - Logical Operators
not,and,or(Lowest precedence)
Python
# Example of operator precedence
result = 10 + 2 * 5 # Evaluates as 10 + 10 = 20 (Multiplication first)
result_with_parentheses = (10 + 2) * 5 # Evaluates as 12 * 5 = 60
Conclusion
Mastering Python operators allows you to perform precise calculations, formulate conditional statements, and streamline complex application logic. Whether you are building financial tools using arithmetic operators or regulating user authorization with logical checks, these core symbols form the backbone of Python programming.
Frequently Asked Questions
What is the difference between = and == in Python?
= is the assignment operator used to assign a value to a variable, whereas == is a comparison operator used to check if two values are equal.
What does the % (Modulo) operator do in Python?
The modulo operator calculates and returns the remainder after dividing the left number by the right number (e.g., 10 % 3 returns 1).
What is floor division in Python?
Floor division (//) divides two numbers and rounds the result down to the nearest whole integer, dropping any decimal places.

