Welcome back to our Python adventure! đ
Do you want to make decision in the code? Today weâll learn how to make your programs think and repeat actions automatically.
So far, weâve covered variables and basic operations. But what makes programming truly powerful is the ability to create logicâmaking your code decide what to do based on different situations.
Before We Start
Make sure you have your Python environment ready from our first episode. If you need a refresher on where to practice, check back here â Episode 1
Today weâre diving into two fundamental concepts that will transform your Python skills: conditional statements (if/else) and loops (for/while).
The Art of Decision Making: If Statements
Programming is essentially about making decisions. Should I do this or that? Should I repeat this action? Should I stop here?
In Python, we use if statements to create this decision-making logic.
Letâs start with the basic structure:
if condition:
# do this
else:
# do that insteadNotice something crucial: Python uses indentation to group code together. This is different from many other programming languages!
What is indentation? Itâs the empty space at the beginning of a line. In Python, you create this space by pressing the Tab key once at the start of each line that belongs to your if statement.
if age >= 18:
print(âYou can vote!â) # â This line starts with a Tab
print(âWelcome to democracy!â) # â This line also starts with a Tab
else:
print(âToo young to voteâ) # â This line starts with a Tab too
Important rule: All lines that belong to the same block must have the same indentation. Python will give you an error if the indentation is inconsistent.
Your First Decision
Letâs try a simple example:
age = 25
if age >= 18:
print(âYou can vote!â)
else:
print(âToo young to voteâ)
Hereâs what happens:
Python checks if the variable
ageis greater than or equal to 18If true, it prints âYou can vote!â
If false, it prints âToo young to voteâ
Multiple Conditions with elif
Sometimes you need more than just two options. Enter elif (short for âelse ifâ):
temperature = 22
if temperature > 30:
print(âItâs hot outside!â)
elif temperature > 15:
print(âPerfect weatherâ)
else:
print(âBring a jacketâ)
This creates a chain of decisions that Python checks in order, stopping at the first true condition.
The Power of Repetition: Loops
What if you want your program to repeat an action multiple times? Thatâs where loops come in.
Python has two main types of loops: for loops and while loops.
For Loops: When You Know How Many Times
A for loop is perfect when you know exactly how many times you want to repeat something.
# Count from 1 to 10
for i in range(1, 11):
print(fâNumber: {i}â) # â Notice the Tab indentation here
The range(1, 11) function creates numbers from 1 to 10 (notice that 11 is excludedâPython stops before the end number).
Whatâs that fâ{}â thing? This is called an f-string (formatted string), and itâs one of Pythonâs coolest features!
Instead of writing:
print(âNumber: â + str(i)) # Old, complicated way
You can write:
print(fâNumber: {i}â) # New, elegant way
The f before the quotes tells Python this is a formatted string. Inside the curly braces {}, you can put any variable, and Python will automatically convert it to text and insert it into the string.
More f-string examples:
name = âAliceâ
age = 25
print(fâHello {name}, you are {age} years oldâ)
# Prints: Hello Alice, you are 25 years old
price = 19.99
print(fâThe item costs ${price}â)
# Prints: The item costs $19.99
Hereâs another example:
# Print each character in a string
name = âPythonâ
for letter in name:
print(fâLetter: {letter}â) # â Tab indentation + f-string magic!
While Loops: When You Donât Know When to Stop
A while loop continues as long as a condition remains true:
countdown = 5
while countdown > 0:
print(fâCountdown: {countdown}â) # â Tab + f-string
countdown = countdown - 1 # â Tab indentation. Decreasing by 1 the counter
print(âBlast off! đâ) # â Back to no indentation (outside the loop)
Important: Make sure your while loop has a way to eventually become false, otherwise it will run forever and I am pretty sure your laptop wonât approve!
Putting It All Together: Our Exercise
Letâs create a program that determines if a number is even or odd:
# Get a number (weâll use a variable for now)
number = 42
if number % 2 == 0:
print(fâ{number} is evenâ)
else:
print(fâ{number} is oddâ)
The % operator gives you the remainder after division. If dividing by 2 gives no remainder (== 0), the number is even.
Letâs make it more interesting by checking multiple numbers:
# Check numbers from 1 to 10
for num in range(1, 11):
if num % 2 == 0:
print(fâ{num} is evenâ)
else:
print(fâ{num} is oddâ)
Pro Tips
Indentation Matters: Python uses indentation instead of curly braces like other languages. Make sure your indented blocks are consistent (use either spaces or tabs, not both).
Testing Conditions: You can use various comparison operators:
==(equal to)!=(not equal to)>(greater than)<(less than)>=(greater than or equal)<=(less than or equal)
Combining Conditions: Use and and or to combine multiple conditions:
age = 20
has_license = True
if age >= 18 and has_license:
print(âYou can drive!â)
Your Challenge
Try creating a program that:
Uses an
ifstatement to check somethingUses an
elsestatement for the alternativeIncludes at least one loop (either
fororwhile)
For example, you could create a program that counts from 1 to 20 and prints âFizzâ for even numbers and âBuzzâ for odd numbers.
Wrap-up
Today youâve learned the foundation of programming logic:
â If statements â Making decisions in your code
â Elif and else â Handling multiple conditions
â For loops â Repeating actions a specific number of times
â While loops â Repeating until a condition changes
These tools let your programs become truly interactive and intelligent. In the next episode, weâll explore how to get input from users and make our programs even more dynamic.
Keep practicing, and remember: every expert programmer started exactly where you are now! đ
Final Reminder: Use the # symbol to add comments to your code as you experiment. It helps you remember what each part does and makes learning much easier.
I know weâve dove quite a lot in this short series until now, especially if you havenât ever tried this tool before. For this reason, I will try to create kind of an âexercise-postâ next week to help you fix the concepts in your mind.
Let me know if you like the idea đĄ


