Learn Python in 30 Days — Day 4: Operators & Expressions
Learn Python in 30 Days — Day 4: Operators & Expressions
Welcome to Day 4 of the Learn Python in 30 Days series!
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Yesterday, you learned how to get input from users and perform simple math.
Today, we’re diving deeper into operators the symbols and keywords that let you combine values and make decisions in your code.
By the end of this lesson, you’ll understand:
-
How to perform arithmetic using operators
-
How to compare values
-
How to combine conditions using logical operators (
and,or,not)
Step 1 – Arithmetic Operators (numbers in, number out)
Arithmetic operators perform maths. Most return a number (int or float).
Key details
-
/always does real (float) division in Python 3, even for two integers. -
//is floor division: it rounds down to the nearest integer (towards negative infinity).-
7 // 3 == 2, but-7 // 3 == -3(because it floors down).
-
-
%(modulus) pairs with//:a == (a // b) * b + (a % b). Great for even/odd checks:n % 2 == 0. -
**is exponent:2 ** 10is1024. -
Order of operations (highest to lowest, simplified):
-
** -
unary
+and-(e.g.,-x) -
*,/,//,% -
+,-
Use parentheses to make intent clear:2 + 3 * 4is14, but(2 + 3) * 4is20.
-
Augmented assignment (handy shorthand):
Step 2 – Comparison Operators (questions that answer True/False)
These test relationships between values and return a Boolean: True or False.
Key details
-
=assigns;==compares. Mixing them up is the #1 beginner error. -
Comparisons work on many types (numbers, strings, etc.).
-
String comparisons are case-sensitive and lexicographical:
"apple" < "Banana"isFalsebecause uppercase letters come before lowercase in Unicode. A common fix is to compare lowercased versions:s1.lower() == s2.lower().
-
-
Chained comparisons read like maths and are more accurate:
-
Comparisons return booleans, which you can store:
Step 3 – Logical Operators (combine conditions)
Logical operators work with booleans to build more expressive rules.
Short-circuiting (important behaviour)
-
andstops early if the left side isFalse(because the whole thing can’t be True). -
orstops early if the left side isTrue(because the whole thing is already True).
This matters if the right-hand side is expensive (e.g., a function call) or has side effects.
Readability tips
-
Prefer positive conditions when possible:
-
Group with parentheses to make intent crystal clear, even if you know the precedence (
not>and>or).
Step 4 – Putting It All Together
Let’s build a realistic gatekeeper that shows how arithmetic (via comparisons) and logical ops work together:
What you’ve used here
-
Comparisons:
age >= 18 -
Logical combos:
(A and B) or (C and B) -
notto invert a condition -
Input normalisation with
.strip().lower()so users can type “Yes”, “ yes ”, etc.
Step 5 – Extra Patterns You’ll Use All the Time
Even / odd check (modulus)
Range checks (chained comparisons)
Safe division (avoid divide-by-zero)
Common Mistakes to Avoid
-
=vs==:=assigns,==compares. -
Forgetting float behaviour:
/returns a float; use//for floor division. -
Confusing precedence: add parentheses for clarity when mixing
and,or,not. -
Case-sensitive string comparisons: normalise with
.lower()before comparing user input. -
Negation traps:
not a or bis not the same asnot (a or b). Parentheses matter.
Challenge: “Expression Tester”
Write a short program that:
-
Asks the user for two numbers.
-
Prints out the results of different expressions using arithmetic, comparison, and logical operators.
Next up: Day 5 – Conditionals
Tomorrow (Day 5) we’ll explore if, elif, and else the building blocks of decision making in Python.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days




Comments
Post a Comment