Python - Decision Making

Hello there, future Python wizards! Today, we're going to embark on an exciting journey into the world of decision-making in Python. As your friendly neighborhood computer science teacher, I'm here to guide you through this adventure step by step. So, grab your virtual wands (keyboards), and let's dive in!

Python - Decision Making

Types of Decision Making Statements in Python

In Python, we have several ways to make decisions in our code. Think of these as different spells in your programming spellbook. Each has its own unique power and use case. Let's take a look at the main types:

Statement Description
if Executes a block of code if a condition is true
if...else Executes one block if a condition is true, another if it's false
if...elif...else Checks multiple conditions and executes the corresponding block
nested if Uses if statements inside other if statements

Now, let's explore each of these magical incantations in detail!

Single Statement Suites

Before we dive into the complex stuff, let's start with something simple. In Python, we can use a single-line statement after our if condition. It's like a quick spell that doesn't need a lot of preparation.

x = 10
if x > 5: print("x is greater than 5")

In this example, if x is indeed greater than 5 (which it is), Python will print the message. It's short, sweet, and to the point!

But remember, while this is handy for quick checks, it's not always the best for readability, especially as your spells (I mean, programs) get more complex.

if...else statement

Now, let's level up our decision-making skills with the if...else statement. This is like giving your code two paths to choose from.

age = 20

if age >= 18:
    print("You are old enough to vote!")
else:
    print("Sorry, you're too young to vote.")

Let's break this down:

  1. We start with a variable age set to 20.
  2. The if statement checks if age is greater than or equal to 18.
  3. If it is (which in this case, it is), the first print statement runs.
  4. If it's not, the code under else would run instead.

This is super useful when you want your code to do one thing in one case, and something else in all other cases.

Nested if statements

Sometimes, life (and code) isn't just about one decision. Sometimes, we need to make decisions based on the outcome of other decisions. This is where nested if statements come in handy.

temperature = 25
humidity = 60

if temperature > 20:
    if humidity > 50:
        print("It's warm and humid.")
    else:
        print("It's warm but not too humid.")
else:
    print("It's not very warm today.")

In this weather-predicting spell:

  1. We first check if the temperature is above 20.
  2. If it is, we then check the humidity.
  3. Depending on the humidity, we print different messages.
  4. If the temperature isn't above 20, we skip the humidity check entirely.

This is like having a decision tree in your code. It allows for more complex logic and more specific outcomes.

The Magical elif

Now, let's introduce a powerful addition to our decision-making toolkit: the elif statement. Short for "else if", this allows us to check multiple conditions in sequence.

grade = 85

if grade >= 90:
    print("A - Excellent!")
elif grade >= 80:
    print("B - Very Good!")
elif grade >= 70:
    print("C - Good")
elif grade >= 60:
    print("D - Satisfactory")
else:
    print("F - Needs Improvement")

Here's how this grading spell works:

  1. We start with a grade of 85.
  2. Python checks each condition in order.
  3. It stops at the first true condition (grade >= 80 in this case).
  4. If none of the conditions are true, it would run the else block.

This is incredibly useful when you have multiple possible outcomes, and you want to check them in a specific order.

A Word on Indentation

Before we wrap up, let's talk about something crucial in Python: indentation. In many programming languages, you use curly braces {} to define blocks of code. But Python uses indentation.

if True:
    print("This is indented")
    print("This is also indented")
print("This is not indented")

The indented lines are part of the if block. The non-indented line is not. This makes Python code clean and readable, but it also means you need to be careful with your spacing!

Conclusion

And there you have it, young Pythonistas! You've learned the basics of decision-making in Python. From simple if statements to complex nested conditions, you now have the power to make your code react dynamically to different situations.

Remember, like any good wizard, practice makes perfect. Try combining these different types of decisions in your own code. Create a text-based adventure game, a simple quiz program, or even a basic AI that responds differently based on user input.

Happy coding, and may your decisions always be wise (and bug-free)!

Credits: Image by storyset