Implement an if Statement
In this step, you will learn how to use if
statements in Python to control the flow of your program. An if
statement allows you to execute a block of code only if a certain condition is true.
The basic syntax of an if
statement is:
if condition:
## Code to execute if the condition is true
The condition
is a Boolean expression that evaluates to either True
or False
. If the condition is True
, the code inside the indented block is executed. If the condition is False
, the code inside the block is skipped.
Let's create a simple example to demonstrate how if
statements work. You will modify the conditions.py
script you created in the previous step.
- Open the
conditions.py
file in VS Code.
- Modify the code to include the following:
x = 5
y = 10
if x < y:
print("x is less than y")
This code checks if x
is less than y
. If it is, it prints the message "x is less than y".
Save the changes and run the script again:
python ~/project/conditions.py
You should see the following output:
x is less than y
This output shows that the code inside the if
statement was executed because the condition x < y
was true.
You can also add an else
clause to an if
statement. The else
clause allows you to execute a different block of code if the condition is false.
The syntax of an if-else
statement is:
if condition:
## Code to execute if the condition is true
else:
## Code to execute if the condition is false
Let's modify the conditions.py
script to include an else
clause.
- Open the
conditions.py
file in VS Code.
- Modify the code to include the following:
x = 15
y = 10
if x < y:
print("x is less than y")
else:
print("x is greater than or equal to y")
This code checks if x
is less than y
. If it is, it prints the message "x is less than y". Otherwise, it prints the message "x is greater than or equal to y".
Save the changes and run the script again:
python ~/project/conditions.py
You should see the following output:
x is greater than or equal to y
This output shows that the code inside the else
clause was executed because the condition x < y
was false.
Finally, you can add an elif
(else if) clause to an if
statement. The elif
clause allows you to check multiple conditions in a sequence.
The syntax of an if-elif-else
statement is:
if condition1:
## Code to execute if condition1 is true
elif condition2:
## Code to execute if condition1 is false and condition2 is true
else:
## Code to execute if both condition1 and condition2 are false
Let's modify the conditions.py
script to include an elif
clause.
- Open the
conditions.py
file in VS Code.
- Modify the code to include the following:
x = 10
y = 10
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x is equal to y")
This code checks if x
is less than y
. If it is, it prints the message "x is less than y". If x
is greater than y
, it prints the message "x is greater than y". Otherwise, it prints the message "x is equal to y".
Save the changes and run the script again:
python ~/project/conditions.py
You should see the following output:
x is equal to y
This output shows that the code inside the else
clause was executed because both conditions x < y
and x > y
were false.
if
statements are essential for creating programs that can make decisions and respond to different situations. In the next step, you will learn how to use the and
and or
operators to combine multiple conditions in your if
statements.