Python Operators and Expressions: The Complete Beginner's Guide

You have variables. You have values stored in them. Now you need to do something with those values — calculate, compare, combine, and update them. That is exactly what operators are for: arithmetic, comparison, logical, and assignment — plus the precedence rules that keep expressions predictable.

Chapter 3 of 20 · Beginner · 30 min · Python Programming Course

You have variables. You have values stored in them. Now you need to do something with those values — calculate, compare, combine, and update them. That is exactly what operators are for.

An operator is a symbol that tells Python to perform a specific operation on one or more values. You already know most of them from school. Python adds a few more that turn out to be extraordinarily useful once you start writing real programs.

This chapter covers all four categories of operators you will use constantly: arithmetic, comparison, logical, and assignment. It also covers operator precedence — the rules Python uses to decide which operation to perform first when an expression has multiple operators. Misunderstanding precedence is responsible for a surprising number of bugs in beginner code.

What you will learn in this chapter

By the end of this tutorial you will be able to:

  • Use all seven arithmetic operators including floor division and modulus
  • Write comparison expressions that return True or False
  • Combine multiple conditions using logical operators and, or, not
  • Update variables efficiently with augmented assignment operators
  • Understand operator precedence and predict expression results correctly
  • Avoid the three most common operator mistakes beginners make
  • Write expressions that combine multiple operator types cleanly

Estimated time: 30 minutes reading + 15 minutes practice

What is an expression in Python?

Before operators, understand what an expression is. An expression is any combination of values, variables, and operators that Python can evaluate to produce a result.

5 + 3              # expression — evaluates to 8
age > 18           # expression — evaluates to True or False
x * 2 + 1          # expression — evaluates to a number
a > 0 and b < 10   # expression — evaluates to True or False

Every expression produces exactly one value when Python evaluates it. That value can be stored in a variable, printed directly, or used inside a condition. This is a fundamental concept — everything on the right side of = is an expression Python evaluates before storing the result.

result = 5 + 3      # Python evaluates 5 + 3 first, then stores 8
print(10 > 5)       # Python evaluates 10 > 5 first, then prints True

Arithmetic operators — calculating with Python

These are the operators that perform mathematical calculations. Python has seven of them.

a = 10
b = 3

print(a + b)    # 13   — addition
print(a - b)    # 7    — subtraction
print(a * b)    # 30   — multiplication
print(a / b)    # 3.3333... — division (always returns float)
print(a // b)   # 3    — floor division (rounds down to nearest int)
print(a % b)    # 1    — modulus (remainder after division)
print(a ** b)   # 1000 — exponentiation (10 to the power of 3)

Output:

13
7
30
3.3333333333333335
3
1
1000

Four of these are straightforward. Three deserve specific attention.

Division always returns a float

This is the most common arithmetic surprise for beginners:

print(10 / 2)    # 5.0 — not 5
print(9 / 3)     # 3.0 — not 3
print(7 / 2)     # 3.5

In Python 3, the / operator always returns a float — even when the result is a whole number. If you need an integer result, use floor division // instead.

print(10 // 2)   # 5   — integer
print(7 // 2)    # 3   — rounds DOWN, not rounds to nearest
print(-7 // 2)   # -4  — still rounds down (toward negative infinity)

The rounding direction of // catches people. It always rounds toward negative infinity — meaning 7 // 2 is 3 (rounds down from 3.5) and -7 // 2 is -4 (rounds down from -3.5, which means away from zero).

Modulus — the remainder operator

Modulus (%) returns the remainder after division. It looks obscure but you will use it constantly.

print(10 % 3)    # 1  — 10 divided by 3 is 3 remainder 1
print(15 % 5)    # 0  — 15 divides evenly by 5, no remainder
print(7 % 2)     # 1  — 7 divided by 2 is 3 remainder 1

The most common real use — checking even or odd:

number = 42
if number % 2 == 0:
    print("Even")
else:
    print("Odd")
# Output: Even

Any number modulo 2 returns either 0 (even) or 1 (odd). This pattern appears in dozens of real programs — filtering lists, alternating styling, pagination logic, and more.

Other practical uses of modulus:

# Check if a number is divisible by another
print(100 % 25 == 0)    # True — 100 is divisible by 25

# Get the last digit of a number
print(12345 % 10)       # 5

# Wrap around a range (useful for circular structures like clocks)
hour = 14
print(hour % 12)        # 2 — converts 14:00 to 2 on a 12-hour clock

Exponentiation — powers in Python

print(2 ** 10)     # 1024
print(3 ** 3)      # 27
print(16 ** 0.5)   # 4.0 — square root (raising to the power of 0.5)
print(2 ** -1)     # 0.5 — negative exponent

Python's ** operator replaces Math.pow() from other languages. It works with fractional and negative exponents too, making it useful for mathematical programs.

Comparison operators — asking True or False questions

Comparison operators do not calculate a new value. They evaluate a condition and return exactly one of two results: True or False.

a = 10
b = 3

print(a == b)    # False  — equal to
print(a != b)    # True   — not equal to
print(a > b)     # True   — greater than
print(a < b)     # False  — less than
print(a >= 10)   # True   — greater than or equal to
print(a <= 9)    # False  — less than or equal to

Output:

False
True
True
False
True
False

These operators become the foundation of everything you write from Day 8 (conditionals) onwards. Every if statement, every while loop condition, every filter on a list — they all depend on comparison operators producing True or False.

The single most important rule: = vs ==

This distinction causes more beginner bugs than almost anything else in Python:

age = 18       # assignment — stores the value 18 in the variable age
age == 18      # comparison — asks "is age equal to 18?" returns True

= is not a question. It is a command: "store this value here." == is a question: "are these two values equal?"

# Wrong — this causes a SyntaxError in Python 3
# if age = 18:
#     print("Adult")

# Correct
age = 18
if age == 18:
    print("Adult")

Every time you write an if statement, your instinct should be to ask: "am I comparing here or assigning?" If comparing, use ==. Always.

Comparing different types

Python compares values strictly by type in some cases:

print(5 == 5)       # True
print(5 == 5.0)     # True  — int and float comparison works
print(5 == "5")     # False — int and string are never equal
print(0 == False)   # True  — False equals 0 in Python
print(1 == True)    # True  — True equals 1 in Python

The last two lines surprise most beginners. Booleans in Python are a subclass of integers — True is literally 1 and False is literally 0. This matters when you use booleans in arithmetic (which is valid Python).

Logical operators — combining conditions

Logical operators let you combine multiple comparison expressions into a single condition.

age = 25
has_id = True
is_member = False

print(age >= 18 and has_id)           # True  — both conditions true
print(age >= 18 and is_member)        # False — one condition false
print(age >= 18 or is_member)         # True  — at least one true
print(not is_member)                  # True  — flips False to True
print(age > 30 or is_member)          # False — both false

Output:

True
False
True
True
False

and — both must be true

and returns True only when every condition on both sides is True. One False anywhere makes the whole expression False.

# Only enters if both conditions pass
age = 22
income = 50000

if age >= 18 and income >= 30000:
    print("Loan approved")
else:
    print("Loan denied")
# Output: Loan approved

Short-circuit evaluation: Python stops evaluating as soon as it finds a False in an and expression. If the first condition is False, Python does not check the second one — the result is already determined.

or — at least one must be true

or returns True when at least one condition is True. Both can be True. Only returns False when every condition is False.

is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("No work today")
else:
    print("Work day")
# Output: No work today

Short-circuit evaluation: Python stops evaluating as soon as it finds a True in an or expression.

not — flips the boolean

not reverses a boolean value. True becomes False. False becomes True.

is_logged_in = False

if not is_logged_in:
    print("Please log in to continue")
# Output: Please log in to continue

not reads naturally in code and is often cleaner than writing == False:

# Both do the same thing — not is cleaner
if not is_logged_in:       # preferred
    pass
# if is_logged_in == False:  # works but verbose

Combining all three logical operators

age = 20
has_ticket = True
is_banned = False

# Complex condition: right age, has ticket, not banned
can_enter = age >= 18 and has_ticket and not is_banned
print(can_enter)   # True

When combining multiple logical operators, use parentheses to make the grouping explicit and your intent clear:

# Ambiguous — what groups with what?
# result = a > 0 and b > 0 or c > 0

# Clear — intent is obvious (example with concrete values)
a, b, c = 1, 2, -1
result = (a > 0 and b > 0) or c > 0
print(result)

Assignment operators — updating variables cleanly

You already know that = assigns a value. Python also has augmented assignment operators that combine an arithmetic operation with assignment in one step.

score = 0

score += 10    # same as: score = score + 10
print(score)   # 10

score += 5
print(score)   # 15

score -= 3     # same as: score = score - 3
print(score)   # 12

score *= 2     # same as: score = score * 2
print(score)   # 24

score //= 5    # same as: score = score // 5
print(score)   # 4

score **= 3    # same as: score = score ** 3
print(score)   # 64

The full set of augmented assignment operators:

OperatorExampleEquivalent to
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 2x = x * 2
/=x /= 2x = x / 2
//=x //= 2x = x // 2
%=x %= 3x = x % 3
**=x **= 2x = x ** 2

You will use += and -= constantly — in loops for counting, in accumulators for summing, and in score-tracking programs.

Operator precedence — which operation runs first

When an expression has multiple operators, Python follows a strict order of evaluation called operator precedence. Getting this wrong produces incorrect results with no error message — the most dangerous kind of bug.

print(2 + 3 * 4)      # 14, not 20
print((2 + 3) * 4)    # 20
print(2 ** 3 + 1)     # 9, not 16
print(2 ** (3 + 1))   # 16

Note: ** binds tighter than + on the first line, and exponentiation is right-associative when chained — so 2 ** 3 ** 2 means 2 ** (3 ** 2), which is 2 ** 9 == 512 in Python.

Python's precedence order from highest to lowest (practical summary):

PriorityOperatorDescription
1 (highest)**Exponentiation
2+x, -xUnary positive / negative
3*, /, //, %Multiplication, division, floor division, modulus
4+, -Addition, subtraction
5==, !=, <, >, <=, >=Comparisons
6notLogical NOT
7andLogical AND
8 (lowest)orLogical OR

The practical rule: If you are not certain about precedence, use parentheses. They always override the default order and they make your intent obvious to anyone reading the code.

# Are these the same? No.
print(True or False and False)     # True  — and runs before or
print((True or False) and False)   # False — parentheses change the order

A complete working program

Here is a program combining all four operator types in a realistic scenario — a simple exam result evaluator:

# Exam result evaluator
student_name = "Riya Kapoor"
marks_obtained = 78
total_marks = 100
passing_marks = 40
distinction_marks = 75

# Arithmetic
percentage = (marks_obtained / total_marks) * 100
marks_needed_for_distinction = distinction_marks - marks_obtained

# Comparisons
has_passed = marks_obtained >= passing_marks
has_distinction = marks_obtained >= distinction_marks

# Augmented assignment
bonus_marks = 0
if has_passed:
    bonus_marks += 5

final_score = marks_obtained + bonus_marks

# Output
print(f"=== Result Card: {student_name} ===")
print(f"Marks:       {marks_obtained}/{total_marks}")
print(f"Percentage:  {percentage}%")
print(f"Passed:      {has_passed}")
print(f"Distinction: {has_distinction}")
print(f"Bonus marks: {bonus_marks}")
print(f"Final score: {final_score}")

Output:

=== Result Card: Riya Kapoor ===
Marks:       78/100
Percentage:  78.0%
Passed:      True
Distinction: True
Bonus marks: 5
Final score: 83

Every operator type from this chapter appears in this program. Read it line by line and confirm you understand what each operator is doing before moving forward.

Three operator mistakes that catch every beginner

Mistake 1: using = instead of == in conditions

# Wrong — SyntaxError in Python 3
# if score = 100:
#     print("Perfect")

# Correct
score = 100
if score == 100:
    print("Perfect")

Mistake 2: forgetting that / always returns float

total = 10
split = total / 2
print(split + 1)    # 6.0, not 6

# If you need an integer
split = total // 2
print(split + 1)    # 6

This matters when you pass the result to a function that expects an integer, or when you use it as a list index.

Mistake 3: misreading precedence in logical expressions

Python supports chained comparisons like 1 < x < 10, and they work correctly — but they are easy to misread next to and/or expressions.

x = 5
if 1 < x < 10:
    print("Between 1 and 10")

# This is invalid — incomplete expression after and
# if x > 1 and < 10:
#     pass

# Explicit form — always clear
if x > 1 and x < 10:
    print("Still between 1 and 10")

Practice: operators and expressions

Exercise 1 — arithmetic drill: Without running the code first, write down the result of each expression. Then verify:

print(17 % 5)
print(2 ** 8)
print(15 // 4)
print(100 / 10)
print(7 % 7)

Exercise 2 — even or odd: Write a program that stores any integer in a variable and uses the modulus operator to print whether it is even or odd. Test it with at least three different values.

Exercise 3 — grade evaluator: A student scored 82 out of 100. Using comparison and logical operators, write expressions that check: whether the student passed (passing mark is 50); whether the student got a distinction (distinction mark is 75); whether the student passed but did not get a distinction.

Exercise 4 — precedence prediction: Predict the output of each line before running it:

print(3 + 4 * 2)
print((3 + 4) * 2)
print(2 ** 3 ** 2)
print(10 - 3 - 2)
print(True and False or True)
print(not True or True and False)

Exercise 5 — score tracker: Start with score = 0. Use augmented assignment operators to: add 25 points; double the score; subtract 10 points; divide by 4 using floor division. Print the score after each operation.

See all Python practice exercises with solutions

What comes next — Day 4: strings and string methods

You now have the full toolkit for working with numeric data — calculating results, comparing values, combining conditions, and updating variables efficiently. The next chapter shifts focus to text.

Day 4 covers string creation, indexing and slicing, the string methods you will use constantly, f-strings in depth, concatenation and repetition, and common string operations in real programs.

Continue to Day 4: Strings and String Methods

Chapter navigation

Frequently asked questions: Operators and expressions

What are operators in Python?

Operators are symbols that tell Python to perform a specific operation on one or more values. Python has four main categories: arithmetic operators for calculations, comparison operators that return True or False, logical operators for combining conditions, and assignment operators for storing and updating values.

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

= is the assignment operator — it stores a value in a variable: age = 25. == is the equality comparison operator — it checks whether two values are equal and returns True or False: age == 25. Using = inside an if condition is a syntax error in Python 3.

What does the % operator do in Python?

The % operator is the modulus operator. It returns the remainder after dividing the left number by the right number. 10 % 3 returns 1 because 10 divided by 3 is 3 with a remainder of 1. Its most common use is checking whether a number is even (number % 2 == 0) or odd (number % 2 == 1).

What is operator precedence in Python?

Operator precedence is the order in which Python evaluates operators when an expression contains more than one. Exponentiation runs first, then multiplication and division, then addition and subtraction, then comparisons, then logical operators. When in doubt, use parentheses to make the evaluation order explicit.

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

/ is regular division and always returns a float: 10 / 2 returns 5.0. // is floor division and returns an integer by dividing and rounding down: 10 // 2 returns 5. Use // when you need a whole number result — for list indexes, loop counts, and integer arithmetic.

What are augmented assignment operators?

Augmented assignment operators combine an arithmetic operation with assignment in one step. x += 5 is shorthand for x = x + 5. They exist for all arithmetic operators: +=, -=, *=, /=, //=, %=, and **=. They are most commonly used in loops for counting and accumulating values.

How does and differ from or in Python?

and returns True only when both conditions are True — one False makes the whole expression False. or returns True when at least one condition is True — only returns False when both conditions are False. Both operators use short-circuit evaluation, meaning Python stops checking as soon as the result is determined.

What is an expression in Python?

An expression is any combination of values, variables, and operators that Python can evaluate to produce a single result. Examples include 5 + 3, age > 18, and a > 0 and b < 10. Expressions can be assigned to variables, passed to functions, or used inside conditions.