Control Flow - Conditionals - Lab Answer Sheet - Colab
Control Flow - Conditionals - Lab Answer Sheet - Colab
Objectives
You will be able to:
Instructions
Let's use our knowledge of variables and conditionals to assign values based on different conditions. Follow the instructions below to
properly assign the values.
With the given code in the cell below, use what you know about numbers and conditionals to assign a value to my_number and follow the
directions given in the comments below. Use this as a guide to structuring the rest of the problems in this lab.
add Code add Text
number_50 = 50
my_number = None
if number_50 > 100:
# if number_50 is greater than 100, assign the `my_number` variable to the number 100
elif number_50 > 50:
# if number_50 is greater than 50, assign the `my_number` variable to the number 50
else:
# else assign the `my_number` variable to 0
Below, use conditionals to tell whether it is hot outside or not. If it is hot, assign the string "It is so hot out!" to the variable is_it_hot . If
it is not hot, assign the string "This is nothing! Bring on the heat." . For our purposes, anything over 80 degrees is considered hot.
temperature = 85
is_it_hot = temperature
if is_it_hot > 80:
print("It is so hot out!")
elif is_it_hot > 50:
print("This is nothing! Bring on the heat.")
# conditionals go here
It is so hot out!
Next, let's see what day of the week it is. There are 7 days in the week starting with Sunday at day 1 and ending with Saturday at day 7 . Use
conditional statements to assign the day of the week to the variable day_of_the_week based on the number below assigned to the variable
today_is . For example, if the day is 2 , we would assign day_of_the_week the value "Monday" .
today_is = 4
day_of_the_week = today_is
if day_of_the_week == 4:
print("Wednesday")
elif day_of_the_week == 1:
print("Sunday")
# conditionals go here
Wednesday
print(day_of_the_week)
None
Finally, let's take a string and see if it ends with a certain substring. If it does, assign the variable ends_with to True , and if it does not, assign
it to False . For example, if have the string "School" and we want to know if it ends with the sub-string "cool". In this case it does not, so, we
would assign False to the variable ends_with .
string = "Python"
sub_string = "on"
ends_with = sub_string
if ends_with == sub_string:
print("True")
https://colab.research.google.com/drive/1UXwcKMQPLeeSnzq_D0Y5xtvRvvOF4egX#printMode=true 1/2
2/4/25, 3:36 PM Control Flow: Conditionals - Lab Answer sheet - Colab
elif ends_with:
print("False")
# conditionals go here
True
Summary
Great! In this lab we saw how to use our knowledge of conditionals to selectively assign values based on a condition. We will start integrating
conditionals in many more ways in our code and we will start to see how useful they can become in more complex problems.
https://colab.research.google.com/drive/1UXwcKMQPLeeSnzq_D0Y5xtvRvvOF4egX#printMode=true 2/2