Welcome to Our Tutorials! Let`s continue.
<What You'll Get Here
We continue with the next lesson.
We study some more python. Ready to dive in? 🚀
Conditional statements
Python Tutorial 4: Conditional Statements (if, elif, else)
Program Description
Programs often need to make decisions based on certain conditions. Conditional statements allow your code to execute different blocks of instructions depending on whether a condition is true or false. The `if`, `elif` (else if), and `else` statements are the tools for this in Python.
Illustration
(Imagine an illustration showing a flowchart with decision points (diamonds) leading to different paths or actions based on a condition being true or false.)
Tutorial Sections: Making Decisions!
Section 1: The `if` Statement
Goal: Execute code only if a condition is true.
Explanation: The `if` statement is the simplest conditional. If the condition following `if` is true, the indented code block below it is executed. Otherwise, it's skipped.
Code:
temperature = 25
if temperature > 20:
print("It's a warm day!")
What it means: Since `temperature` (25) is greater than 20, the condition is true, and the message "It's a warm day!" will be printed.
Section 2: The `else` Statement
Goal: Execute code if the `if` condition is false.
Explanation: The `else` statement provides an alternative block of code to run if the `if` condition is false. It must come immediately after an `if` (or `elif`) block.
Code:
temperature = 18
if temperature > 20:
print("It's a warm day!")
else:
print("It's not too warm.")
What it means: Since `temperature` (18) is not greater than 20, the `if` condition is false. The `else` block is executed, and "It's not too warm." is printed.
Section 3: The `elif` Statement
Goal: Check multiple conditions in sequence.
Explanation: The `elif` (short for "else if") statement allows you to check additional conditions if the previous `if` or `elif` conditions were false. You can have multiple `elif` blocks.
Code:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: Below C")
What it means: The code checks the score against multiple ranges. Since 85 is not >= 90, the first `if` is false. Since 85 is >= 80, the first `elif` is true, and "Grade: B" is printed. The remaining `elif` and `else` blocks are skipped.
Section 4: Combining Conditions
Goal: Use logical operators to check multiple criteria at once.
Explanation: You can combine conditions using logical operators: `and`, `or`, and `not`.
Code:
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive.")
if age < 18 or not has_license:
print("You cannot drive.")
What it means: The first `if` checks if both conditions (`age >= 18` and `has_license`) are true. The second `if` checks if at least one condition (`age < 18` or `not has_license`) is true. In this case, "You can drive." will be printed.
You've now mastered conditional statements! This is a powerful tool for creating programs that can respond differently based on various situations. Practice writing `if`, `elif`, and `else` statements to control the flow of your programs.