Introduction: How to Write, and Understand, the Fizzbuzz Coding Challenge in Python

The fizzbuzz challenge is a challenge where for each number from 1 to 100 if the number is divisible by 3 “fizz” is printed if the number is divisible by 5 “buzz” is printed, and if the number is divisible by 3 and 5 “fizzbuzz” is printed. This is a common challenge used by employers to test a person's coding skills and is one of the first things many new programmers learn to do. That being said it's always fun to find a better way to do things so our challenge today is going to be completing fizzbuzz in as few lines as possible.

Supplies

The only thing you need is the computer your reading this on and to be willing to learn

Step 1: Getting to the Online Ide

Our first step is to find an environment where we can run our program. If you have an IDE and python ready on your computer feel free to use that, but for ease of use I’ll be using an online IDE called online-python.com.

Step 2: Writing Our First Loop

Next, we have to decide how we are going to go over or “loop” over numbers 1 to 100. Python gives us two easy ways to do this, we will talk about one of them now and the other later. First, we have the while loop. The while loop first looks at its conditional statement ( in the example picture above the conditional statement is i < 101) and evaluates it then if that conditional is true the “body” of the statement (“i++” is the body in the image above) is executed and the conditional is called again. This continues until the conditional is false. We also have to initialize the variable that we are using in the conditional which is where the i=1 comes from. The last part of the while loop that we need is a way to increment or increase the value of i. We could do this in a couple of ways but the most common one is i+=1. This increases the value by 1.

Step 3: Adding Conditional Statements

Now we need to figure out what to do in the body to accomplish our goal. We could “hardcode” each value of i to do set behavior but there is a much easier way. We can use a conditional statement as we saw in the while loop in conjunction with an if statement to check to see if we want to do something. It works in a pretty simple way, if the conditional is true then the body of the statement executes, but unlike a while loop it only does this once. For example,  when we run the code above the while loop will run from 1 to 100 but the only time anything happens is when i == 2 or in English when the value of i is equal to 2.

Step 4: How to Tackle When to Use Fizz and When to Use Buzz

Now how do we implement the rules that we need? Luckily we have the modulus (%) operator. This operator lets us do division and tells us the remainder. For instance, 9%3 would equal 0 and 8%3 would equal 2. Using this we can check to see if a number is divisible by 3 or 5 by checking if the result of that number % 3 or 5 and checking to see if that result is equal to zero.

Step 5: Intro to "and" and "or"

You may have noticed that we still have one rule to implement. If the number is divisible by both 3 and 5 we need to print out “fizzbuzz”. This is where we need to learn about “and” and “or”. We can combine two conditional states with both of these keywords to do slightly different things. “And” will look at the conditionals on either side of it and if they are both true and only if they are both true, the body will execute. “Or” is similar but only one of the conditionals has to be true. Which one do you think we could use to print “fizzbuzz” when we need to? If you said “and” make sure to give yourself a gold star!

Step 6: Finishing Up Our First Iteration

One other thing we can do is change our second if statement to an “elif” statement. “Elif” is the combination of else and if. If we put an else statement at the end of an if statement, the body of that else statement gets executed if the preceding if statement is false. The “elif” statement extends that functionality and gives us a way to say, if that was false, test this. Lastly to make sure that we wrote everything right we can add an else statement that will print the value of i if none of our conditionals are true. You may notice the “str()” that wraps the i. This is needed to cast i to a string. Python doesn't make us tell the computer what “type” our variables are but we still need to be careful about what we put where. When we said “i=1” we told the computer that i is a number. Our print statements will only take a string. That's why we wrap what text we want to print with quotation marks. If we wrapped i in quotation marks i would be printed so we have to manually tell the computer that we want i to be a string by using str(). And that's it we have a working fizzbuzz program!

Step 7: String Concatenation

We aren't really done. There are still a few things that we can do to make our program even shorter. Making it shorter really doesn't have any advantage outside of being more impressive. For our first improvement, we are going to change the way we handle the “fizzbuzz” output. In python, strings can be added together. If we have the string “fizz” and the string “buzz” we can add them together, “fizz” + “buzz”, and the resulting string will be “fizzbuzz”. The way we will use this is by declaring a new string variable “partial” that is equal to an empty string or “”. Then if we use the += (this operator is just a shorter way of saying string1= string1 +  string2) we can add “fizz” to the empty string if the number is divisible by 3 and we add buzz to the empty string if the number is divisible by 5. This has the cool effect that if we change the elif statement where we check if i%5 to an if statement, fizzbuzz will be printed when the number is divisible by both 3 and 5. This is because we are executing the bodies of both if statements when we have back-to-back if statements. This does mean that we have to change the way that we handle the printing of the number. We can use an if statement to check if the partial string is empty, and if it is we can add i to partial. now we just have to print out the value of partial and we’re up and running. I know this didn't actually shorten the code, but it sets us up for a really important feature.

Step 8: The Second Way to Loop

Remember when I said we have two ways to loop over a set of numbers? Well, the second kind is a for loop. The for loop is really similar to the while loop but we don't have to initialize the value of our i and it increments itself! The for loop uses a thing called a “range” this range accepts some different values that change how we can use our for loop. The first value that we pass in is the value that we want to start with. If you don’t pass in that value, it will start at zero. In our case, we want to start at one. The second value we give it is the number we want to stop at plus one. It works the same as the conditional we used in the while loop, if we want to get to 100 we need the smallest whole number that is greater than 100. The second new thing is in. the in keyword is just used to iterate through the sequence of the range and it assigns the current number the sequence the loop is on to i.

Step 9: Moving to a Single Line

The last two steps are the biggest and most important. We have seen that we can add strings together so it shouldn't come as too big of a surprise when I tell you that we can multiply strings. If you take a string “test” and multiply it by 2 (“test”*2) The result is “testtest”. This behavior isn't really useful to our current problem, but something that is interesting is that if you multiply a string by a true conditional the result is the same as the original string, but if you multiply a string by a false conditional the result is “”.

Step 10: Finishing the One Liner

The other important thing that is important is that we can use our “or” keyword in a print statement. We can use “or” to print the number we are on if it is not divisible by 3 or 5 the same way that we used “else” in step 4. The last piece of the puzzle is pretty small. You can put your code on the same line. After the colon we can just add our print statement and bam there we are!