Lesson 146 of 1570
Python Loops & Conditionals — Let AI Draft, You Decide
If-statements and loops are where programs come alive. You'll write both kinds, then see where AI autocomplete helps and where it lies.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1If this, then that
- 2if/else
- 3for loop
- 4while loop
Concept cluster
Terms to connect while reading
Section 1
If this, then that
A conditional lets your program make choices. An if-statement runs a block of code only when a condition is True. An else runs when it is False. An elif is 'else if' — check another condition.
Python uses indentation, not braces. Four spaces is the standard.
score = 87
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "try again"
print(f"You got a {grade}.")Loops: doing a thing many times
for-loops are for 'I know how many times.' while-loops are for 'until X happens.'
# for-loop over a range of numbers
for i in range(5): # 0, 1, 2, 3, 4
print(f"Step {i}")
# for-loop over a list
names = ["Ava", "Ben", "Cy"]
for name in names:
print(f"Hi, {name}!")
# while-loop: run until a condition turns False
countdown = 3
while countdown > 0:
print(countdown)
countdown -= 1
print("Go!")Truthiness — Python's clever shortcut
In Python, empty containers are False. This makes if-statements read like English.
# These values are all 'falsy': they act like False in conditions
for value in [0, "", [], None, False]:
if not value:
print(f"{value!r} is falsy")
# Everything else is 'truthy'
items = ["apple", "banana"]
if items: # same as: if len(items) > 0
print("We have items!")Mini-exercise: FizzBuzz
- 1Write a loop from 1 to 20
- 2If the number is divisible by 3, print 'Fizz'
- 3If divisible by 5, print 'Buzz'
- 4If divisible by both, print 'FizzBuzz'
- 5Otherwise print the number
Compare the options
| Task | Autocomplete is great | Autocomplete often lies |
|---|---|---|
| Standard FizzBuzz | Yes | No |
| Domain-specific rules | Rarely | Often — invents fake field names |
| While-loop termination | Sometimes | Forgets to decrement the counter |
Key terms in this lesson
Big idea: conditionals and loops let the computer make millions of small decisions. AI drafts them fast — but you are the one who owns whether the logic is actually right.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Python Loops & Conditionals — Let AI Draft, You Decide”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Builders · 30 min
Bash and Shell With AI
The terminal is where real work happens. Pipes, variables, and loops in bash are a superpower — and AI is surprisingly good at shell one-liners.
Builders · 25 min
What Does AI-Assisted Coding Even Mean?
AI-assisted coding is not magic and not cheating. It is a new way of working where a model drafts, you decide. Let's draw a map before we start building.
Builders · 30 min
Your First Copilot-Style Completion
Let's actually feel what autocomplete is like. Write a comment, pause, and watch a full function appear. Then learn what to do next.
