Skip to Content
Python for Beginners: Write Code That Actually Runs, Then Make It Better
Beginner
0 likes
Courses
Python for Beginners: Write Code That Actually Runs, Then Make It Better

Python Operators and Expressions

Arithmetic, comparison and logical operators, plus shortcuts like +=. After this, you can write expressions that calculate and compare values, and you'll know why 0.1 + 0.2 doesn't give exactly 0.3.

Course content

Sign in to save your progress.

Course Home

Python Operators and Expressions

An operator is a symbol that does something with values: add them, compare them, combine them. An expression is any piece of code that produces a value, like price * quantity. Most of what your programs "decide" or "calculate" is just expressions.

Skip ahead: If you know the difference between / and //, what % does, and why = is not ==, continue to "Getting Input and Printing Output in Python."

Why this matters

Operators look obvious because you know them from school math. Most of them are. But a few behave differently in Python, and those few are exactly where wrong totals, wrong discounts, and wrong conditions come from. The code runs fine. The answer is wrong. That is worse than an error.

Arithmetic operators

print(7 + 2)    # 9   addition
print(7 - 2)    # 5   subtraction
print(7 * 2)    # 14  multiplication
print(7 / 2)    # 3.5 division, always gives a float
print(7 // 2)   # 3   floor division, drops the decimal part
print(7 % 2)    # 1   remainder (modulo)
print(7 ** 2)   # 49  power

Two of these deserve attention. / always returns a float, even for 8 / 2, which gives 4.0. And % gives the remainder, which is more useful than it looks. number % 2 == 0 is how you check whether a number is even.

Python follows the normal math order: powers first, then multiplication and division, then addition and subtraction. When in doubt, use parentheses. They cost nothing and remove the doubt for the next reader.

print(10 + 5 * 2)     # 20
print((10 + 5) * 2)   # 30

Comparison operators

These compare two values and give back True or False:

age = 20
print(age == 18)   # False  equal to
print(age != 18)   # True   not equal to
print(age > 18)    # True
print(age >= 20)   # True
print(age < 18)    # False

⚠ Warning: One = stores a value. Two == compare values. Writing if age = 18: is a SyntaxError in Python, which is actually good: Python catches it for you. But mixing them up in your head will still confuse you when reading code, so get this one solid now.

Logical operators: and, or, not

These combine conditions. They are written as words in Python, not symbols.

age = 25
has_id = True

print(age >= 18 and has_id)    # True: both must be true
print(age < 18 or has_id)      # True: at least one is true
print(not has_id)              # False: flips the value

Shortcut assignment

total = 100
total += 50    # same as total = total + 50
total -= 20    # same as total = total - 20
print(total)   # 130

A real-world example: a discount

Say members get 10% off orders of 1,000 or more. Here is the calculation, step by step:

subtotal = 1250.00
is_member = True

gets_discount = is_member and subtotal >= 1000
discount = subtotal * 0.10 if gets_discount else 0
total = subtotal - discount

print("Discount:", discount)   # 125.0
print("Total:", total)         # 1125.0

The line with if ... else in the middle is a short form you will see again in the lesson on if statements. For now, read it as "10% of the subtotal if they qualify, otherwise zero."

A note on decimals

Try print(0.1 + 0.2). You get 0.30000000000000004. This is not a Python bug. Computers store most decimals in binary, which cannot represent 0.1 exactly. For display, use round(value, 2). For real money calculations in serious systems, people use Python's decimal module. For learning, round() is enough. Just do not be shocked when you see it.

print(0.1 + 0.2)              # 0.30000000000000004
print(round(0.1 + 0.2, 2))    # 0.3

Common mistakes

  • Expecting / to give a whole number. Use // if you really want to drop the decimals, and know that it rounds down.
  • Forgetting parentheses. price + tax * quantity is not (price + tax) * quantity.
  • Comparing floats with ==. 0.1 + 0.2 == 0.3 is False. Round first, or compare with a small tolerance.

💡 Tip: Use the interactive shell (python3 or py) as a calculator while learning. If you are unsure what an expression gives, type it and see. Testing beats guessing every time.

What happens if you skip this step

Your code runs, prints a number, and you trust it. Then someone checks the total by hand and it is off by a few pesos, or the discount applies to people who should not get it. No error message. Just a wrong answer and a lot of explaining to do.

Quick check: what is 17 // 5, and what is 17 % 5?

About

Arithmetic, comparison and logical operators, plus shortcuts like +=. After this, you can write expressions that calculate and compare values, and you'll know why 0.1 + 0.2 doesn't give exactly 0.3.

Duration
5 min
Last update
09/09/2026
Was this helpful?
​ 0 ​ 0

Comments (0)

Sign in to comment

No comments yet.