Course Content
Conditional Statements in Python
Conditional Statements in Python
Using Comparison Operators in Python
Now, let's get into the details of what you can actually include within those conditions.
Comparison operators are useful for assessing the values of variables. Their result is always a boolean value, which can be either True
or False
.
You can compare complex mathematical expressions, elements of different data structures, strings, and even boolean values. As we already know how to compare numbers from previous chapters, let's explore other types of comparisons:
Example 1: Comparing strings to determine workout types
favorite_activity = "Yoga" current_activity = "Running" if favorite_activity != current_activity: print("This is not your favorite activity, but keep going!")
This checks if two strings are not equal (exact match, case sensitive).
Example 2: Comparing boolean values to check activity completion
# Check if the user completed their morning routine morning_run_completed = True if morning_run_completed: print("Great job on completing your morning run!")
This example evaluates whether a specific activity was completed (in this case, a morning run). Instead of writing if morning_run_completed == True
, we use if morning_run_completed
because the value of morning_run_completed
is already a boolean (True or False). Writing == True
is redundant and less concise. Python allows such simplifications to make the code cleaner and easier to read.
Example 3: Checking if a number falls within a range
Your app can help users track their physical activity. This task checks whether the user's daily step count falls within a recommended range and provides feedback on their progress.
daily_steps = 7500 # Step count range check if 5000 <= daily_steps <= 10000: print("You're on track with your step count!")
Swipe to start coding
Your task is to check whether the street temperature is in the ideal range for running. If the temperature is between 10 and 20 degrees Celsius (inclusive), print a message indicating that the conditions are optimal for running.
- Use an
if
statement to check whetherstreet_temperature
is between10
and20
degrees Celsius, inclusive. - If the condition is met, assign
True
torunning_temp
; otherwise, keep itFalse
. - Also, if
hydration_goal_met
isTrue
, print a confirmation message.
Solution
Thanks for your feedback!
Using Comparison Operators in Python
Now, let's get into the details of what you can actually include within those conditions.
Comparison operators are useful for assessing the values of variables. Their result is always a boolean value, which can be either True
or False
.
You can compare complex mathematical expressions, elements of different data structures, strings, and even boolean values. As we already know how to compare numbers from previous chapters, let's explore other types of comparisons:
Example 1: Comparing strings to determine workout types
favorite_activity = "Yoga" current_activity = "Running" if favorite_activity != current_activity: print("This is not your favorite activity, but keep going!")
This checks if two strings are not equal (exact match, case sensitive).
Example 2: Comparing boolean values to check activity completion
# Check if the user completed their morning routine morning_run_completed = True if morning_run_completed: print("Great job on completing your morning run!")
This example evaluates whether a specific activity was completed (in this case, a morning run). Instead of writing if morning_run_completed == True
, we use if morning_run_completed
because the value of morning_run_completed
is already a boolean (True or False). Writing == True
is redundant and less concise. Python allows such simplifications to make the code cleaner and easier to read.
Example 3: Checking if a number falls within a range
Your app can help users track their physical activity. This task checks whether the user's daily step count falls within a recommended range and provides feedback on their progress.
daily_steps = 7500 # Step count range check if 5000 <= daily_steps <= 10000: print("You're on track with your step count!")
Swipe to start coding
Your task is to check whether the street temperature is in the ideal range for running. If the temperature is between 10 and 20 degrees Celsius (inclusive), print a message indicating that the conditions are optimal for running.
- Use an
if
statement to check whetherstreet_temperature
is between10
and20
degrees Celsius, inclusive. - If the condition is met, assign
True
torunning_temp
; otherwise, keep itFalse
. - Also, if
hydration_goal_met
isTrue
, print a confirmation message.
Solution
Thanks for your feedback!