0% found this document useful (0 votes)
7 views2 pages

Control Flow - Conditionals - Lab Answer Sheet - Colab

This document is a lab exercise focused on using Python conditional statements to assign values based on specific conditions. It includes instructions for various tasks, such as determining if a number is greater than a threshold, checking the temperature, identifying the day of the week, and verifying if a string ends with a certain substring. The lab aims to enhance understanding of conditionals in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Control Flow - Conditionals - Lab Answer Sheet - Colab

This document is a lab exercise focused on using Python conditional statements to assign values based on specific conditions. It includes instructions for various tasks, such as determining if a number is greater than a threshold, checking the temperature, identifying the day of the week, and verifying if a string ends with a certain substring. The lab aims to enhance understanding of conditionals in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2/4/25, 3:36 PM Control Flow: Conditionals - Lab Answer sheet - Colab

keyboard_arrow_down Control Flow: Conditionals - Lab


Introduction
Now that you have been introduced to conditionals, let's put your knowledge to test and create some conditional statements that selectively
assign values to variables based on whether they pass the conditions we set.

Objectives
You will be able to:

Use Python conditional statements

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

You might also like