Introduction:
In this lesson, we’re going to dive into one of the most important concepts in programming: the if
statement. This type of statement helps your program make decisions, just like you do in everyday life! You’ll learn how to set up an if
statement in Python and use it to check if something is true or false.
Real-World Example:
Think about how you make decisions in real life. Let’s say you’re deciding whether to take an umbrella when you go out:
- If it’s raining, you take an umbrella.
- Else (if it’s not raining), you don’t.
Your brain is making a decision based on a condition: is it raining? This is very similar to what an if
statement does in Python.
What is an if
Statement in Programming?
An if
statement is a way for your program to make decisions based on conditions, just like you do. It checks if something is true, and if it is, it does one thing. If it’s not true, the program can do something else (using the else
clause).
In Python, an if
statement looks like this:
if condition: # code to run if the condition is true else: # code to run if the condition is false
Let’s break it down:
if
: This keyword introduces the condition you want to check.- Condition: The condition is something that can either be true or false (e.g.,
is it raining?
). else
: This keyword is used to give your program an alternative action if the condition is false.
Example in Python:
Here’s an example using a simple quiz question:
answer = "yes" if answer == "yes": print("Correct!") else: print("Wrong answer.")
Let’s break it down:
if answer == "yes":
– This line checks if the answer is exactly"yes"
. The==
operator checks if two values are equal.print("Correct!")
– If the answer is"yes"
, the program prints"Correct!"
.else:
– If the answer is anything other than"yes"
, the program moves to theelse
block.print("Wrong answer.")
– If the answer is not"yes"
, it prints"Wrong answer."
Why Are if
Statements Useful?
If
statements are super useful because they allow your program to make decisions and react differently based on what’s happening. Think of them like traffic lights:
- If the light is green, you go.
- Else, if the light is red, you stop.
Without if
statements, your program wouldn’t be able to react to different situations. Whether you’re building a game, an app, or any program that needs to respond to user input, if
statements are essential.
Real-Life Example:
Here’s how if
statements are just like making decisions in everyday life.
Imagine this scenario:
- You’re heading out to school.
- If it’s cold outside, you wear a jacket.
- Else, you don’t.
In Python, that might look something like this:
temperature = "cold" if temperature == "cold": print("Wear a jacket!") else: print("No need for a jacket.")
- The
if
statement checks if thetemperature
is equal to"cold"
. - If that’s true, the program will print
"Wear a jacket!"
. - If the temperature isn’t cold, the
else
clause kicks in, and the program prints"No need for a jacket."
Setting Up an if
Statement in Python:
Let’s summarise how to set up an if
statement in Python:
- Start with the
if
keyword, followed by the condition you want to check. - After the condition, add a colon (
:
), and on the next line, indent your code using 4 spaces or a tab. - Use the
else
clause if you want to provide an alternative action when the condition isn’t met.
Here’s a simple format:
if condition: # do this if the condition is true else: # do this if the condition is false
Making Decisions with !=
(Not Equal)
So far, we’ve checked if something is equal to a value using ==
. But sometimes, you want to check if something is not equal to a value. This is where the !=
operator comes in.
For example:
name = "Charlie" if name != "Bob": print("You're not Bob!") else: print("Hello, Bob!")
- The
!=
operator checks if two values are not equal. - In this case, since the name is
"Charlie"
, the program prints"You're not Bob!"
, because"Charlie"
is not equal to"Bob"
.
Conclusion:
An if
statement is like asking a question in your program. You’re telling your code, "If this condition is true, do one thing, but if it’s false, do something else." This makes your program smarter and able to respond to different situations, just like how you make decisions in real life.
Now that you understand how if
and else
work, you can use them to make your programs more interactive and dynamic. In the next lesson, we’ll learn even more ways to compare values!
Predict:
Look at the following code. What do you think will be printed? Make a prediction!
name = "Alice" if name == "Bob": print("Hello, Bob!") else: print("You’re not Bob!")
Hint: The ==
operator checks if two values are exactly the same. Will the if
statement print the message for Bob, or the message from the else
clause?
Run:
Now let’s run some code and see how the if
and else
statements work when checking equality (==
) and inequality (!=
).
Explanation:
- The first
if
statement checks ifusername
is exactly'admin'
. If it is, the program prints a welcome message; if not, it denies access. - The second
if
statement checks ifpassword
is not equal to's3cr3t'
. If it’s different, the program prints that the password is invalid; otherwise, it prints that the password is accepted.
Investigate:
Now let’s explore how if
statements behave when comparing different values. Run the code below, and then try changing the value of favourite_food
to see what happens.
Instructions:
- Try changing
favourite_food
from'pizza'
to something else, like'burger'
or'sushi'
. - What do you observe when the comparison is true (
==
) versus when it’s false?
Modify:
The following code has a small mistake. It’s supposed to check whether someone’s name is "Charlie" or not, but there’s an issue with how the else
clause is used. Can you fix it?
Hint: Double-check the if
condition. Should it be using !=
or ==
?