Python Operators for Beginners: Arithmetic, Logical and More

Learn what basic Python operators are and how to use them: arithmetic, comparison, logical, and assignment operators, with simple examples.
If variables are nouns, operators are the verbs of code. Without them, you couldn't create a login system, a search engine, or a price filter.
Arithmetic Operators
Arithmetic operators allow you to perform basic mathematical calculations.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3.3333 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 10 ** 3 | 1000 |
// | Floor division | 10 // 3 | 3 |
Difference between / and //
It's important to note the difference: / always returns a decimal number (float), while // rounds down to the nearest integer.
print(10 / 3) # 3.3333333333333335 (float)
print(10 // 3) # 3 (int, discards the decimal)
print(-10 // 3) # -4 (rounds down!)
Assignment Operators
These operators are used to assign a value to a variable. Most combine an arithmetic operation with an assignment in a single step.
Assuming we start with x = 10:
| Operator | Example | Equivalent to | Final Result of x |
|---|---|---|---|
= | x = 5 | x = 5 | 5 |
+= | x += 3 | x = x + 3 | 13 |
-= | x -= 2 | x = x - 2 | 8 |
*= | x *= 4 | x = x * 4 | 40 |
/= | x /= 2 | x = x / 2 | 5.0 |
%= | x %= 3 | x = x % 3 | 1 |
//= | x //= 3 | x = x // 3 | 3 |
**= | x **= 2 | x = x ** 2 | 100 |
Building Blocks: Comparison Operators
Before delving into pure logic, we need to compare data. These operators always return a Boolean value (True or False).
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 10 > 20 | False |
< | Less than | 10 < 20 | True |
>= | Greater or equal | 18 >= 18 | True |
<= | Less or equal | 15 <= 10 | False |
Difference between = and ==
This is one of the most common beginner mistakes:
=is the assignment operator:x = 5(we give x the value 5).==is the comparison operator:x == 5(we ask if x equals 5).
The 3 Pillars of Logic: and, or, not
This is where Python shines in terms of readability. Other languages use && or ||, but in Python we use English words, which makes the code read like natural language.
The and operator
Returns True only if both conditions are true. It's a strict filter.
age = 25
has_license = True
# To drive, you need both
can_drive = age >= 18 and has_license
print(f"Can drive?: {can_drive}") # True
The or operator
Returns True if at least one of the conditions is true. It's a flexible filter.
is_weekend = True
is_on_vacation = False
# We rest if it's weekend OR if we're on vacation
rest = is_weekend or is_on_vacation
print(f"Rest today?: {rest}") # True
The not operator
Inverts the Boolean value: turns True into False and vice versa.
door_locked = True
if not door_locked:
print("The door is open, you can enter")
else:
print("The door is locked, you need the key")
Truth Table
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
Short-Circuit Evaluation
Python is smart and lazy (in a good way). This optimizes performance:
-
In an
and: If the first condition isFalse, Python doesn't look at the second, because it knows the final result will beFalseanyway. -
In an
or: If the first condition isTrue, Python ignores the second, because it already knows the result will beTrue.
# Safety example
divisor = 0
# Without short-circuiting, this would cause an error (division by zero)
# But since divisor != 0 is False, Python doesn't evaluate the second part.
result = divisor != 0 and (10 / divisor > 1)
print(result) # False (no errors)
Operator Precedence
Just like in mathematics (where multiplication comes before addition), logic has a hierarchy. The order of evaluation is:
-
not(Highest priority) -
and -
or(Lowest priority)
Pro Tip: Always use parentheses to avoid confusion and make your code readable for others.
# What do you think this returns?
result = True or False and False
# First evaluates: False and False -> False
# Then: True or False -> True
print(result) # True
# Much better with parentheses:
result = (True or False) and False # False
Special Operators: is and in
Python has two additional very powerful operators:
-
in: Checks if an element is within a collection (list, string, etc.). -
is: Checks if two variables point to the same object in memory (identity).
fruits = ["apple", "banana", "grape"]
print("apple" in fruits) # True
print("pear" in fruits) # False
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True (same content)
print(a is b) # False (two different lists in memory)
Practical Challenge: "The Club Guardian"
Write a script that decides whether a person can enter a VIP club. The rules are:
-
Must be over 18 years old.
-
Must have a valid ticket OR be on the guest list.
-
Must not be blacklisted (banned).
Test variables:
age = 20
has_ticket = False
is_invited = True
is_banned = False
# Your logic here
# can_enter = ...
Solution:
can_enter = age >= 18 and (has_ticket or is_invited) and not is_banned
print(f"Can enter?: {can_enter}") # True
-
More about operators in Python: Hektor Docs - Operators in Python.
-
More about operators in Python from W3School: W3School - Python Operators.
Ready to continue? Now that you master operators, learn how to make decisions with conditional structures in Python.
Want to put what you learned to the test? Use DivZone AI to generate and test your own exercises with operators.