Data Science: Python interview questions: Fizz Buzz

I am going to start with a simple exercise called the Fizz Buzz exercise that is too easy to get asked at interview for data science but is a useful starting place for me. I have done a bit of Python in the form of a Udemy course but of course you forget all the content so fast, so I thought recording these exercises might be a good way to discipline myself.

This link describes the exercise and gives a solution. “Write a simple program that prints the numbers from 1 to N. But for multiples of three print “Fizz” instead of the number. For the multiples of five print “Buzz”. And, for multiples of both three and five print, “FizzBuzz”.”

This exercise does not rely on any of the libraries such as pandas or numpy, so is a good starting point.

I didn’t know that the % sign in Python gives you remainder of when you divide one number by another. E.g. 10%2 would give an output of 0.

First I wrote down how to do this with an if statement for a single number that is defined:

Next, I wrote a loop without using a function for numbers 1 to 16:

Then I used a function which probably isn’t necessary:

Calling the function finally: