Python Basics 2
Conditional Statements: The Art of Decision-Making in Python
Imagine a robot that has to choose its next move based on a series of clues. It might need to check if the room is bright enough for it to see, or if there’s an obstacle ahead. This decision-making process is called conditional logic, and in programming, we use “if” statements to implement this very logic!
In Python, conditional statements are key tools for making decisions based on certain conditions. Let’s break down the basics:
1. The Basic Structure:
if condition_true:
# Code to execute if the condition is true
else:
# Code to execute if the condition is false
condition_true
: This is a Boolean expression that evaluates to eitherTrue
orFalse
. It’s what we use to decide which code block should run.# Code to execute if the condition is true
: You put the statements that you want to be executed only if thecondition_true
evaluates toTrue
.else
: This optional block executes if the initialif
statement’scondition_true
wasFalse
.
2. Understanding the “If” Statement:
- Python compares different values using symbols like:
==
: checks for equality (e.g.,5 == 5
isTrue
)!=
: checks for inequality (e.g.,5 != 6
isTrue
)<
: checks for less-than>
: checks for greater-than
3. Combining “If” and “Else” to Get More Complex Decisions:
- You can have multiple
if
statements, each with its owncondition_true
. This allows you to create more intricate decision paths:
4. The Power of “elif” (Else If):
- Sometimes, your initial
if
condition might be false, but there are other possibilities you want to check with more specific conditions.
That’s whereelif
comes in:
5. The “elif” Statement’s Purpose:
- The
elif
statement allows you to check multiple conditions sequentially, giving more flexibility in your decision-making process, especially when the initial condition might be false.
6. Practice Makes Perfect!
- The best way to get comfortable with conditional statements is by practicing:
- Start with simple examples to solidify your understanding.
- Gradually increase the complexity of your decisions and explore more
if-elif-else
logic combinations.
Another Simple Example: Did a student pass?
Checking if a student passed, based on their points scored. Try chaning the number of points a student scored and see the result.
Explanation:
- Input:
points = int(70)
- This line inputs the points as an integer
- Conditional Statement:
if points >= 70:
: We use anif
statement to check if the entered points is greater than or equal to 70.
print("Congratulations! You passed!")
: If the condition is true (points ≥ 70), this message will be printed, indicating a passing points.else:
: If the condition is false, we enter anelse
block.print(f"You're missing some studying. Try again next time!")
: This message will be printed if the student did not pass the course.
- Result: This code gives us a basic structure to check for passing points based on a specific threshold (70). You can adjust this threshold (or check for other points) as needed!
How It Works: - The code checks whether the points
is greater than or equal to 70. - If the condition (points >= 70
) is true, it prints “Congratulations! You passed!”. - If the condition is false, it prints a message suggesting that the student needs to study harder and try again.
Additional Notes: - You can easily add more points and different logic for if
statements within this code block as needed. - This example demonstrates basic conditional statement functionality in Python.
Here are some more examples:
elif
Example: Clothing Selection
A slightly more complex example of using conditional statements using if
, elif
and else
.
Try running the code below by changing the input to size
variable.
- Explanation: We first try to match the user’s input to specific sizes (XS, S, M, L, XL, XXL)
elif
: If no match is found in theif
block, Python checks the next condition (elif
).- if the input does not match any condition,
else
is used to print the output.