Python is a stubbornly literal child. It only does exactly what you tell it to do, nothing more. If your logic is boring, it will happily follow you into chaos. That’s where conditionals and loops come in: the backbone of decision-making and repetition in code. If you don’t understand these, you’re not programming, you’re just typing.
Conditional Statements in Python
Conditionals are your “if this then that” brain logic, written in code. They let the program make decisions instead of running blindly.
In plain language:
- If it’s raining, take an umbrella.
- Otherwise, go outside and enjoy life.
In Python:
x = 10
if x > 5:
print(“x is greater than 5”)
elif x == 5:
print(“x equals 5”)
else:
print(“x is less than 5”)
Think of conditionals as mental filters. Wrong condition = wrong outcome. So don’t be lazy: think before you compare things
Loops in Python
Loops let you run code repeatedly, instead of manually repeating yourself like a broken speaker. Humans get bored repeating tasks, but computers don’t. That’s their whole charm. They’ll run the same line a billion times without complaining, crying, or asking for Wi-Fi.
There are two main loop types in Python:
1. for Loop
Use for loops when you know how many times you want to repeat something or you’re looping through a collection like a list, string, or range. Think of it like opening a box of donuts and examining each one. Yes, every single one. Quality control.
Example with range():
for i in range(1, 6):
print(“Loop number:”, i)
That prints:
Loop number: 1
Loop number: 2
…
Loop number: 5
You didn’t type “Loop number” five times. Efficient. Adulting level ++.
Looping through a list (because real programs handle real data)
Code:
fruits = [“apple”, “mango”, “banana”]
for fruit in fruits:
print(“Eating”, fruit)
The loop grabs each item one at a time and uses it. Think of it as “autopilot mode” for data.
Nested for loops (loop inside loop)
Useful for grids, patterns, or torturing yourself:
for i in range(3):
for j in range(2):
print(i, j)
Just don’t overuse nested loops unless you enjoy slow code. Keep them like hot sauce: powerful, but don’t drown your food in it.
2. while Loop
Use the while loop when you don’t know how many times to repeat something, but you have a condition.
It keeps running until the condition becomes false.
Example:
counter = 1
while counter <= 5:
print(counter)
counter += 1
This is like telling yourself,
“I’ll keep scrolling YouTube Shorts until my battery hits 0%.”
That’s exactly a while loop in real life.
Infinite Loop (the horror story)
If you forget to update the condition, your program loops forever:
while True:
print(“Help, I’m stuck!”)
Congrats. You just built a code treadmill. The program will run until your laptop begs for mercy.
Loop Control Statements (a.k.a. escape hatches)
Sometimes you don’t want to finish the full loop.
break: yeets you out of the loop
for num in range(10):
if num == 5:
break
print(num)
continue: skips the current loop iteration and moves on
for num in range(6):
if num == 3:
continue
print(num)
Think of break as cancelling plans.
And continue as ignoring someone mid-conversation but still hanging around.
Quick Summary
Loops ≠ fancy.
Loops = survival.
They:
- save time
- reduce code repetition
- automate boring tasks
- prevent your sanity from evaporating
You master loops, suddenly you’re not just “learning Python” — you’re controlling workflows like a tiny dictator of logic. In a good way. Probably.
The next natural step? Combining loops + conditionals + data structures. That’s where Python stops being “cute beginner coding” and becomes “wow, I actually built something.”
Keep going — the logic gym doesn’t close.