TL;DR:
A variable stores a value under a name. A for loop repeats a block, and if/elif/else chooses between blocks.
The modulo operator
%gives the remainder, son % 3 == 0means n is divisible by 3.In FizzBuzz, check the combined case (divisible by 15) first, or the others will catch it.
FizzBuzz is a classic first program because it uses almost everything a beginner has just learned: variables, a loop, and several conditions. The task: for each number from 1 to 15, print Fizz if it is divisible by 3, Buzz if divisible by 5, FizzBuzz if divisible by both, and otherwise the number itself.
The idea in plain words
A variable is a name for a value: count = 3 stores 3 under the name count. A loop repeats a block of code. A conditional runs a block only when a test is true. And the modulo operator, %, gives the remainder after division: 7 % 3 is 1, because 7 is 2 threes with 1 left over, and 9 % 3 is 0.
A remainder of 0 means the first number divides evenly. So n % 3 == 0 tests whether n is a multiple of 3. Notice the two equals signs: one = assigns a value, while == compares two values.
Why the order of conditions matters
Python checks if, elif and else branches from top to bottom and runs only the first one that is true. A number like 15 is divisible by 3 and by 5. If you test for 3 first, 15 would print Fizz and never reach the Buzz test. So test the most specific case, divisible by both, before the others. Since a number divisible by 3 and 5 is divisible by 15, test n % 15 == 0 first.
Worked example: FizzBuzz from 1 to 15
for n in range(1, 16):
if n % 15 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)Two details first. range(1, 16) gives the numbers 1 up to 15, because the end value is not included. And the loop variable n takes the next value on each pass. Now trace it.
n | n % 15 | n % 3 | n % 5 | Printed |
|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 |
2 | 2 | 2 | 2 | 2 |
3 | 3 | 0 | 3 | Fizz |
4 | 4 | 1 | 4 | 4 |
5 | 5 | 2 | 0 | Buzz |
6 | 6 | 0 | 1 | Fizz |
7 | 7 | 1 | 2 | 7 |
8 | 8 | 2 | 3 | 8 |
9 | 9 | 0 | 4 | Fizz |
10 | 10 | 1 | 0 | Buzz |
11 | 11 | 2 | 1 | 11 |
12 | 12 | 0 | 2 | Fizz |
13 | 13 | 1 | 3 | 13 |
14 | 14 | 2 | 4 | 14 |
15 | 0 | 0 | 0 | FizzBuzz |
The printed lines are 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz. That is 15 lines. Count the Fizz-type results as a check: multiples of 3 up to 15 are 3, 6, 9, 12, 15, which is 5 lines; multiples of 5 are 5, 10, 15, which is 3 lines; and 15 is in both lists, so 5 + 3 − 1 = 7 lines are not plain numbers, leaving 15 − 7 = 8 plain numbers: 1, 2, 4, 7, 8, 11, 13, 14. They are all there.
Using a variable to keep a total
Variables also remember things between loop passes. This program adds up the numbers 1 to 15:
total = 0
for n in range(1, 16):
total = total + n
print(total)It prints 120, since 15 × 16 ÷ 2 = 120. Each pass takes the old value of total, adds n, and stores the new value back under the same name.
What goes wrong with the wrong order
To see why the order matters, swap the first two tests so that n % 3 == 0 comes before n % 15 == 0. For 15 the first test succeeds (15 % 3 is 0), so the program prints Fizz and never checks the rest. The last line becomes Fizz instead of FizzBuzz. Nothing crashes, and the other 14 lines are still right, which is why this kind of bug is easy to miss unless you check the edges.
This is a good habit to build early. A program that runs without an error is not necessarily correct. Compare the output with what you worked out by hand, especially for the special cases.
How to check your answer
Count the lines of output. Fifteen numbers should give fifteen lines.
Check the edges: the first line (1) and the last (FizzBuzz).
Check one multiple of 3, one of 5 and one of both by hand.
Change the range to
range(1, 31)and see that 30 prints FizzBuzz.
Common mistakes
Using
=where you meant==, which is a syntax error in a condition.Testing 3 before 15, so FizzBuzz never prints.
Writing
range(1, 15)and missing 15.Forgetting the colon at the end of the
for,if,eliforelseline, or mis-indenting the block under it.Putting the variable in quotes, which prints the letter n instead of its value.
Where you will use it next
Everything in larger programs is built from these pieces. Next you can wrap this logic in a function, as in recursion explained with factorials, and then learn to run a program that talks to the web: how a web page gets to your screen.
Frequently asked questions
Why does Python use indentation?
Indentation is how Python knows which lines belong inside a loop or condition. Keep it consistent, with four spaces.
What if I get an error?
Read the message from the bottom, as explained in the article on error messages, and check the line it points to.
