Course Content
Conditional Statements in Python
Conditional Statements in Python
Syntax of if Statement
The if
statement is a foundational tool in Python that allows us to make decisions in our code.
Let's break down the key components of the if statement syntax:
- Keyword
if
: this keyword begins the conditional statement. Remember, it is case-sensitive—usingIf
instead ofif
will cause an error; - Condition: the condition is an expression that evaluates to
True
orFalse
. In this example, we check if a variable meets a certain requirement; - Colon
:
: the colon signals that the following indented block belongs to theif
statement; - Indented Code Block: this block contains the code that runs only if the condition is
True
.
Example 1: Standard Execution
steps_taken = 7500 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
Explanation
- The condition
steps_taken < step_goal
checks if the number of steps taken is less than the daily goal; - If the condition is True, the code inside the
if
block is executed; - The message uses an f-string to dynamically include the number of steps remaining, making the output user-friendly and precise.
Example 2: When Nothing Executed
steps_taken = 10000 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
In this case, the condition steps_taken < step_goal
evaluates to False
because steps_taken
is equal to step_goal
. Since the condition is not met, the code block inside the if
statement is not executed, and nothing is printed to the console. This demonstrates that the code only runs when the condition evaluates to True
.
The image depicts the flow of an if statement:
- Condition Check: the program evaluates whether the condition is
True
orFalse
; - Execution: if the condition is
True
, the indented code block runs. Otherwise, the program skips it.
Swipe to begin your solution
Your Fitness Tracker needs to do more than just check steps! This time, it will motivate users to complete their workout by comparing their calories burned against daily goals.
Fill in the blanks in the code you've already been given.
Once you've completed this task, click the button below the code to check your solution.
Solution
Thanks for your feedback!
Syntax of if Statement
The if
statement is a foundational tool in Python that allows us to make decisions in our code.
Let's break down the key components of the if statement syntax:
- Keyword
if
: this keyword begins the conditional statement. Remember, it is case-sensitive—usingIf
instead ofif
will cause an error; - Condition: the condition is an expression that evaluates to
True
orFalse
. In this example, we check if a variable meets a certain requirement; - Colon
:
: the colon signals that the following indented block belongs to theif
statement; - Indented Code Block: this block contains the code that runs only if the condition is
True
.
Example 1: Standard Execution
steps_taken = 7500 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
Explanation
- The condition
steps_taken < step_goal
checks if the number of steps taken is less than the daily goal; - If the condition is True, the code inside the
if
block is executed; - The message uses an f-string to dynamically include the number of steps remaining, making the output user-friendly and precise.
Example 2: When Nothing Executed
steps_taken = 10000 step_goal = 10000 if steps_taken < step_goal: print(f"Keep going! You need {step_goal - steps_taken} more steps to reach your goal.")
In this case, the condition steps_taken < step_goal
evaluates to False
because steps_taken
is equal to step_goal
. Since the condition is not met, the code block inside the if
statement is not executed, and nothing is printed to the console. This demonstrates that the code only runs when the condition evaluates to True
.
The image depicts the flow of an if statement:
- Condition Check: the program evaluates whether the condition is
True
orFalse
; - Execution: if the condition is
True
, the indented code block runs. Otherwise, the program skips it.
Swipe to begin your solution
Your Fitness Tracker needs to do more than just check steps! This time, it will motivate users to complete their workout by comparing their calories burned against daily goals.
Fill in the blanks in the code you've already been given.
Once you've completed this task, click the button below the code to check your solution.
Solution
Thanks for your feedback!