if, for, and whileControl flow is where programming starts to feel powerful. Until now, most programs have been a straight line: Python runs your code from top to bottom, one statement at a time. Control flow gives you the ability to make decisions and repeat work—two features that unlock almost everything you think of as “real” programming.
In this tutorial, you’ll learn the three core control-flow tools in Python:
if statements for making decisions.for loops for iterating over items in an iterable (like a list).while loops for repeating work while a condition remains true.Along the way, you’ll also learn one of Python’s most important rules: indentation is part of the language. It’s not just style—it defines structure.
[ Control flow section overview in your editor/notebook]
if statement: making decisionsAn if statement tells Python: “Only run this block of code when a condition is true.”
Let’s start with a simple example:
a = True
if a:
print("It is true")
Code language: PHP (php)
Here, a is a Boolean value (True or False). The line if a: checks whether a is true. Since it is true, Python prints the message.
Now change a to False:
a = False
if a:
print("It is true")
Code language: PHP (php)
This time, nothing prints. That’s the point of if: the code inside the if block runs only when the condition is true.
[ Output showing print happens only when a is True]
ifPython uses a colon (:) and indentation to define blocks of code. When you write:
if a:
print("It is true")
Code language: PHP (php)
the colon after if a: means: “A block is starting.” The indented line underneath it belongs to that block.
You can put multiple statements inside the same if block by indenting them at the same level:
a = True
if a:
print("It is true")
print("Also print this")
Code language: PHP (php)
Both lines are in the if block, so both run when a is True.
Now notice what happens when you outdent a line:
a = False
if a:
print("It is true")
print("Also print this")
print("Always print this")
Code language: PHP (php)
Even though a is False, the last line still prints. Why? Because it’s not indented, so it does not belong to the if block.
This is a core idea in Python: indentation controls structure. A single extra space (or a missing indent) can completely change how your program behaves.
[Visual indentation highlighting in the editor]
else blockSometimes you want to do one thing if the condition is true, and a different thing otherwise. That’s what else is for.
a = False
if a:
print("It is true")
else:
print("It is false")
Code language: PHP (php)
Read this as: “If a is true, run the first block. Otherwise, run the else block.”
If you set a = True, then the if block runs. If you set a = False, then the else block runs.
[ Output for both True and False cases]
if statements: when indentation gets deepPython allows you to place an if inside another if. This is called nesting.
a = True
b = True
if a:
print("a is true")
if b:
print("Both are true")
Code language: PHP (php)
Here’s what’s happening:
a is True, Python enters the first block.b.b is also True, it prints “Both are true.”You can nest even further:
a = True
b = True
c = True
if a:
if b:
if c:
print("All three are true")
Code language: PHP (php)
This works, but notice how quickly the indentation increases. If any one of these becomes False, you never reach the deepest line.
Deep nesting can make code harder to read and maintain. One reason loops exist is to help you avoid overly indented, overly complex logic when repetition is involved.
[ Example of nested blocks and how the indentation grows]
for loop: repeating work over itemsA loop is Python’s way of repeating a block of code. A for loop specifically repeats work for each item in an iterable.
An iterable is something you can iterate over—meaning Python can give you its items one by one. A list is a common iterable.
Let’s create a list:
numbers = [1, 2, 3, 4, 5]
Now iterate over it:
numbers = [1, 2, 3, 4, 5]
for item in numbers:
print(item)
Code language: PHP (php)
This reads as: “For each item in numbers, print that item.”
When you run this, it prints 1, then 2, then 3, and so on until the list ends.
In for item in numbers:, the name item is not special. It’s just a variable name you chose. You can call it whatever makes sense:
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
Code language: PHP (php)
This is often clearer because the variable name describes what it represents.
A common beginner mistake is changing the loop variable name but forgetting to update the name inside the loop:
numbers = [1, 2, 3]
for number in numbers:
print(item) # NameError, because item doesn't exist here
Code language: PHP (php)
If you define number, use number inside the loop.
[ for loop output in your notebook]
in: membership vs for syntaxYou’ve now seen in used in two different ways, and it’s important not to confuse them.
in as a membership testThis form asks: “Is this value inside this collection?”
numbers = [1, 2, 3, 4, 5]
print(4 in numbers)
Code language: PHP (php)
This prints True because 4 is in the list.
in as part of for loop syntaxIn a for loop, in is part of the loop structure:
for item in numbers:
print(item)
Code language: PHP (php)
This doesn’t return True or False. Instead, it means: “Take items from the iterable one by one.”
Even though both use the word in, they do different jobs. The key is the context:
value in collection evaluates to a Boolean.for variable in iterable: is a loop statement.while loop: repeat until a condition becomes falseA while loop repeats a block of code as long as a condition is true.
Here’s a simple example:
a = 0
while a < 5:
print(a)
a = a + 1
Code language: PHP (php)
This prints:
a to 1a to 2Once a becomes 5, the condition a < 5 becomes false, and the loop stops.
while loopsNotice this line:
a = a + 1
Without it, a would stay 0 forever, a < 5 would remain true forever, and your loop would never end. That would print 0 endlessly—an infinite loop.
Many programmers have made this mistake at least once. When you write a while loop, always ask yourself:
“Where does the condition change so the loop can eventually stop?”
[while loop output and the changing value of a]
for vs while?Both for and while repeat work, but they fit different situations.
A for loop is a natural fit when you already have items to iterate over, such as a list of numbers, filenames, or lines in a dataset. Python handles moving from one item to the next automatically.
A while loop is a natural fit when the loop depends on a condition that changes over time—especially when that condition is updated by the code inside the loop. In the example above, the loop continues until a reaches 5, and the loop body is responsible for moving a forward.
If you find yourself writing very deeply nested if statements, it’s often a sign that a loop (or a different structure) could make the program clearer. Loops are one of the main tools that prevent code from becoming overly indented and hard to follow.
Try running the examples above in your Python environment and making small changes. Set a, b, or c to False and observe which lines do and do not run. Then modify the list in the for loop and confirm that Python still prints every item.
Finally, in the while loop, change the condition from a < 5 to a < 3 and notice how the output changes. After that, change the increment from a = a + 1 to a = a + 2 and see how the loop behaves. These tiny experiments build the intuition you need to write correct control flow in real programs.
[ Your practice cell with modifications and outputs]
Control flow is the foundation for writing programs that behave intelligently.
An if statement lets your code make decisions, and Python’s indentation defines exactly what belongs to those decisions. A for loop lets you process items one by one from an iterable, and the loop variable name is yours to choose. A while loop repeats work until a condition becomes false, but it depends on you to update something so it can eventually stop.
Once you’re comfortable with these three tools, you can write programs that respond to input, handle real-world data, and automate tasks—without the code turning into a tangled mess of repeated lines and deep indentation.