FizzBuzz in Python
The classic screening exercise for loops and conditionals.
for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
How it works
- Loops over the numbers 1 through 100 with
range. - Multiples of 3 print Fizz; multiples of 5 print Buzz.
- Multiples of both print FizzBuzz; everything else prints the number.
Keywords and builtins used here
elifelseforifprintrange
The run, in numbers
- Lines
- 9
- Characters to type
- 134
- Tokens
- 55
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 37 seconds.
Step 5 of 7 in Flow control, step 20 of 72 in Language basics.