Boolean expressions
Comparisons produce True or False. Control flow depends on these values, because a program has to know whether it should keep going the same way or choose something else.
At this stage, think of a comparison as a question the program asks itself. The answer is not a sentence, just one of two values, and that small answer decides what happens next.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.If and else
Use a conditional to select one path through a program. This is the point where Python starts making choices instead of simply running top to bottom.
Python checks the condition after if. If it is true, the indented block runs. Otherwise, Python skips it and runs the else block. The indentation is part of the structure, so keep all lines in the same block aligned.
This pattern is useful whenever there are only two realistic outcomes, such as wet or dry, valid or invalid, or warm enough or not warm enough. The main skill here is reading the condition carefully before you run the code.
- value is below 5
- take the else branch
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Elif branches
elif lets you test more than two outcomes. It is the middle ground between a simple yes-or-no choice and a larger decision tree.
When there are several possible answers, think about which one should be checked first. The order matters, because Python stops as soon as it finds a true condition.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.For loops
A for loop visits every item in a collection. Instead of writing the same line again and again, you let Python move through the sequence for you.
This is one of the most common patterns in programming. It shows up whenever you want to display a list, total a set of numbers, or apply the same rule to each item.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.While loops
A while loop continues while its condition is true. It is the right tool when you do not know the number of repetitions in advance, but you do know the condition that should eventually stop the loop.
The line count -= 1 is what moves this loop toward its stopping point. Without a change like that, the condition can stay true forever. Before running a while loop, identify the value that will eventually make its condition false.
That habit protects you from infinite loops and makes the code easier to reason about. If you can point to the value that changes each time, you are already partway to understanding the loop.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Break and continue
continue skips one iteration; break finishes a loop early. Both are ways of adjusting the normal flow when a special case appears.
These tools are most useful when a loop has one rule for the common case and a separate rule for an exception. They are powerful, but they should still feel easy to explain in plain language.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.