Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Using Python Conditionals

The majority of programs that we write will need to be more than a single sequential path of execution. We usually work with data that isn't always the same, and occasionally requires us to do different things based on that data. To achieve this we need to utilize conditionals. In this hands-on lab, we'll work through the conditional logic for the popular interviewing Fizz-Buzz problem by creating a script that will prompt the user for a number and then print out either the number, "Fizz", "Buzz", or "FizzBuzz" depending on whether the number meets one of the Fizz-Buzz requirements. To feel comfortable completing this lab you'll want to know how to do the following: * Utilize conditionals. Watch the "The `if` and `else` Statements" and "Handling Multiple Cases with `elif`" videos from the Certified Entry-Level Python Programmer Certification course. * Handle user input. Watch the "The `input` Function" video from the Certified Entry-Level Python Programmer Certification course.

Google Cloud Platform icon
Lab platform
Lab Info
Level
Beginner
Last updated
Sep 20, 2025
Duration
30m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Create the fizz-buzz-item.py Script, Make It Executable with python3.7, and Accept User Input

    We'll create a fizz-buzz-item.py right in our home directory (~), and we want to make sure that we can run it directly, so that we're not completely tied to the path of our python3.7 binary. We can do this by writing our shebang properly.

    Let's create the file and set the shebang:

    ~/fizz-buzz-item.py

    #!/usr/bin/env python3.7
    
    # Python implementation here
    

    With the file created, we need to also make sure that it's executable. We'll do that using chmod:

    $ chmod u+x ~/fizz-buzz-item.py
    

    Next, we'll prompt the user for a value, convert it to an int, and store it off in a variable:

    ~/fizz-buzz-item.py

    #!/usr/bin/env python3.7
    
    value = int(input("Enter an integer value: "))
    
  2. Challenge

    Print "FizzBuzz" if the Value Is a Multiple of Three and Five

    With the value read, we can create our conditional. Since we have a case that should trigger if the value is a multiple of three and five, then we'll want that branch to be before the only multiple of three or only multiple of five branches, because either of those would also be true. Let's create our if statement and print "FizzBuzz" if the condition is met, then add an else to just print the value otherwise:

    ~/fizz-buzz-item.py

    #!/usr/bin/env python3.7
    
    value = int(input("Enter an integer value: "))
    
    if value % 5 == 0 and value % 3 == 0:
        print("FizzBuzz")
    else:
        print(value)
    

    Now we can run the script to make sure that what we've written up to this point is working properly:

    $ ./fizz-buzz-item.py
    Enter an integer value: 15
    FizzBuzz
    $ ./fizz-buzz-item.py
    Enter an integer value: 4
    4
    $
    
  3. Challenge

    Print "Fizz" if the Value Is a Multiple of Three, and "Buzz" if it's a Multiple of Five

    To handle the other two cases of the Fizz-Buzz problem we'll need to utilize elif statements for each of the individual comparisons that we used in our if statement. Let's add those now:

    ~/fizz-buzz-item.py

    #!/usr/bin/env python3.7
    
    value = int(input("Enter an integer value: "))
    
    if value % 5 == 0 and value % 3 == 0:
        print("FizzBuzz")
    elif value % 3 == 0:
        print("Fizz")
    elif value % 5 == 0:
        print("Buzz")
    else:
        print(value)
    

    Now we can run the script to make sure that what we've written up to this point is working properly:

    $ ./fizz-buzz-item.py
    Enter an integer value: 15
    FizzBuzz
    $ ./fizz-buzz-item.py
    Enter an integer value: 4
    4
    $ ./fizz-buzz-item.py
    Enter an integer value: 6
    Fizz
    $ ./fizz-buzz-item.py
    Enter an integer value: 10
    Buzz
    $
    
About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight