Python- Day 16-IF Statements
Once you understand conditional tests, you can start using if
statements to control the flow of your Python programs.
There are different types of if
statements and the one you choose depends on how many conditions you want to check.
Simple if
Statements:
A simple if
statement tests one condition and performs an action based on the result:
if conditional_test:
# Do something
The condition is evaluated as either True
or False
.
If it's True
, the code inside the indented block is executed. Otherwise, Python skips that block.
For example, you can check if a person is old enough to vote:
age = 19
if age >= 18:
print("You are old enough to vote!")
This prints the message because the condition (age >= 18
) is True
.
if-else
Statements :
In many cases, you’ll want to take different actions depending on whether a condition passes or fails.
This is where an if-else
structure is useful:
age = 17
if age >= 18:
print("You are old enough to vote!")
else:
print("Sorry, you are too young to vote.")
Here, if the condition (age >= 18
) is False
, Python will execute the else
block instead, printing a different message.
if-elif-else
Chain:
When you need to test multiple conditions, use an if-elif-else
chain. Python evaluates each condition in order and executes the block of the first test that passes:
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $25.")
else:
print("Your admission cost is $40.")
>>
Your admission cost is $25.
Here, the second test (age < 18
) passes, so the message "Your admission cost is $25." is printed.
Multiple elif
Blocks:
You can add more elif
blocks to handle more complex conditions.
For example, if you want to add a senior discount:
age = 70
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
else:
price = 20
print(f"Your admission cost is ${price}.")
In this case, if the person is 65 or older, the price will be $20, as specified in the else
block.
Omitting the else
Block:
You don’t always need an else
block.
If you have specific conditions to test, you can replace the else
block with another elif
statement:
age = 70
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >= 65:
price = 20
print(f"Your admission cost is ${price}.")
This approach ensures that every block of code is executed only when the correct condition is met, offering more control over the behavior of your program.
Testing Multiple Conditions:
Sometimes, you want to check multiple conditions independently.
In these cases, use multiple if
statements without elif
or else
:
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
In this example, Python checks each condition separately, so it adds both mushrooms and extra cheese to the pizza.
If you used an if-elif-else
structure instead,
Python would stop checking after the first condition passes:
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
elif 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
elif 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
This code would only add mushrooms and skip the rest,
because Python stops checking after the first condition that is True
.
Use a simple
if
statement when you need to test one condition.Use
if-else
when you want one block of code to run if the condition passes, and a different block if it fails.Use
if-elif-else
when you need to check multiple conditions.Use multiple
if
statements when more than one condition needs to be checked and multiple actions may be required.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!