If you just landed on this site looking for Python training, then you came to the correct spot! This post is the continuation of several posts on learning to code with Python. The start of this series can be found here.

In this post we’ll explore the power of if, elif and else while coding with Python. The best place to start reading is always the official Python documentation. Start by reading up on the if, elif, and else statements.

After reading the Python docs, it is time to start coding! The scripts below will help you understand more about making choices within your Python code.

Lab 3.1 – Using if and elif

The following code creates a list containing five items (strings). Some of these strings are more ferocious than the others. Our goal, is to provide the user some kind of heads up concerning the monster that was chosen from our “monster pit”. Consider the following code:

#!/usr/bin/python3
"""Russell Zachary Feeser || Monster Generator - monstermash01.py"""

# imports from standard library
# required to make random choices from list
import random

def main():
    """runtime code"""

    # define list of 'monsters'
    monsterpit = ["bunny", "puppy", "dragon", "orc", "kitten"]

    # choose a random monster from the list
    monster_choice = random.choice(monsterpit)

    # if the monster is an orc or dragon
    # give a warning to the user
    if monster_choice == "orc" or monster_choice == "dragon":
        print("Do not forget your sword! You are about to face a", monster_choice)
    # if the monster is anything else
    # give a lesser warning to the user 
    else:
        print("You will be fine! You are about to face a", monster_choice)

if __name__ == "__main__":
    main()

Lab 3.2 – Using if, elif, and else

The next script includes an elif block. Think of this as providing a secondary course of action before reaching the else ‘catch all’ block. There is no limit to the number of elif blocks you create.

#!/usr/bin/python3
"""Russell Zachary Feeser || Monster Generator - monstermash02.py"""

# imports from standard library
# required to make random choices from list
import random

def main():
    """runtime code"""

    # define list of 'monsters'
    monsterpit = ["bunny", "puppy", "dragon", "orc", "kitten"]

    # choose a random monster from the list
    monster_choice = random.choice(monsterpit)

    # if the monster is an orc or dragon
    # give a warning to the user
    if monster_choice == "orc" or monster_choice == "dragon":
        print("Do not forget your sword! You are about to face a", monster_choice)
    # if the monster chosen is a bunny
    elif monster_choice == "bunny":
        print("Do not forget a carrot!!  You are about to face a", monster_choice)
    # if the monster chosen is anything else
    # give a lesser warning to the user
    else:
        print("You will be fine! You are about to face a", monster_choice)

if __name__ == "__main__":
    main()

So, you’re looking for a Python trainer?

Impressed? If not, click around my site. You’ll find lots of posts and videos on everything from Python to Ansible to telecom. If you are a telecom or other corporation looking to automate your processes, or in need of Python training, be sure to reach out! When I’m not maintaining my blog, I am on the road teaching classes to Fortune 500s like you! You can always email me (I hide my email address on most of the photos on my site), or use the website to contact me directly.