Python Conditional Statements

PythonPythonBeginner
Practice Now

Introduction

In the mystical realm of Fantasia Forest, a world filled with enchanting creatures and magic unbeknownst to most, there lies a grand opportunity for aspiring wizards. You are the latest apprentice of a renowned wizard, Aldar the Wise, known throughout the lands for his mastery over the elements and spells.

As part of your training, Aldar has set up a series of challenges that will test your logical prowess and understanding of the arcane scripts – Python conditional statements. Your goal is to navigate through these challenges by crafting spells (programs) that make decisions based on various conditions, as any skilled wizard must do.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") subgraph Lab Skills python/conditional_statements -.-> lab-271532{{"`Python Conditional Statements`"}} end

Deciphering the Book of Basics

In this step, you'll start by learning how to make decisions using if, elif, and else constructs in Python. Imagine you've discovered a passage in the Book of Basics that describes how to determine the type of a mystical creature encountered in the forest by observing its characteristics.

Here's your first spell in /home/labex/project/creature_type.py:

## creature_type.py
creature = "Dragon"

if creature == "Dragon":
    print("The creature is a fiery Dragon!")
elif creature == "Unicorn":
    print("The creature is a majestic Unicorn!")
else:
    print("The creature is of unknown origin.")

In this code, we check if the creature variable is a Dragon or a Unicorn, and print a message accordingly. If it's neither, we print that the creature is of unknown origin.

To run this Python script, use the following command in the /home/labex/project directory:

python creature_type.py

The expected result should be:

The creature is a fiery Dragon!

The Potion of Logical Operators

In the depths of Fantasia Forest, you are tasked with creating a potion that requires precise ingredients. Aldar has taught you the use of logical operators and, or, and not in your Python scripts.

For this step, you'll create a spell to help you decide what ingredients are needed based on the day's weather and humidity conditions.

Write the following spell in a file named /home/labex/project/potion_ingredients.py:

## Determine the needed potion ingredients
weather = "sunny"
humidity = "high"

if weather == "rainy" and humidity == "high":
    print("Add a sunflower petal to counter the rain.")
elif weather == "sunny" or humidity == "moderate":
    print("Add a drop of honey for sweetness.")
else:
    print("No special ingredients are needed today.")

Execute your spell with:

python /home/labex/project/potion_ingredients.py

You should see the message:

Add a drop of honey for sweetness.

Summary

In this lab, you've embarked on a fantastical journey through the Fantasia Forest, adopting the role of a wizard's apprentice to master Python conditional statements. We designed this lab with an engaging narrative to make learning programming concepts like if, elif, else, and logical operators (and, or, not) more captivating and relatable.

By immersing yourself into the world of magic, you have practiced controlling the flow of your Python spells and have become more proficient in decision-making constructs that are essential for any Python wizard. Your ability to write and understand conditional statements will serve you well as you continue to explore the charming and mysterious realm of programming.

Other Python Tutorials you may like