typestar

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

  1. Loops over the numbers 1 through 100 with range.
  2. Multiples of 3 print Fizz; multiples of 5 print Buzz.
  3. Multiples of both print FizzBuzz; everything else prints the number.

Keywords and builtins used here

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.

Type this snippet

Step 5 of 7 in Flow control, step 20 of 72 in Language basics.

← Previous Next →

FizzBuzz in other languages