= 20
temperature
# Your code here
# Use an if-else statement to print weather advice
Quick Reference
Hereβs a quick reference for the basic control flow structures weβll be using:
# If statement
if condition:
# code to run if condition is True
# For loop
for item in sequence:
# code to run for each item
# While loop
while condition:
# code to run while condition is True
Hereβs our course cheatsheet on control flows:
Feel free to refer to this cheatsheet throughout the exercise if you need a quick reminder about syntax or functionality.
Exercise Overview
In this Coding Colab, youβll practice using basic control flow structures in Python. Work through these simple tasks with your partner, discussing your approach as you go.
Part 1: If Statements (20 minutes)
Task 1: Simple Weather Advice
Write a program that gives weather advice based on temperature:
- Set a variable
temperature = 20
- Use an if-else statement to print advice:
- If temperature is above 25, print βItβs a hot day, stay hydrated!β
- Otherwise, print βEnjoy the pleasant weather!β
Task 2: Grade Classifier
Create a program that assigns a letter grade based on a numerical score:
- Set a variable
score = 85
- Use if-elif-else statements to print the letter grade:
- 90 or above: βAβ
- 80-89: βBβ
- 70-79: βCβ
- 60-69: βDβ
- Below 60: βFβ
= 85
score
# Your code here
# Use if-elif-else statements to print the letter grade
Part 2: For Loops (20 minutes)
Task 3: Counting Sheep
Write a program that counts from 1 to 5, printing βsheepβ after each number:
- Use a for loop with the range() function
- Print each number followed by βsheepβ
# Your code here
# Use a for loop to count sheep
Task 4: Sum of Numbers
Calculate the sum of numbers from 1 to 10:
- Create a variable
total = 0
- Use a for loop with the range() function to add each number to
total
- After the loop, print the total
= 0
total
# Your code here
# Use a for loop to calculate the sum
Part 2.5: Two Ways to Think About the Same Problem (15 minutes)
Great job with those for loops! Now letβs connect what you just learned with the list comprehensions you practiced yesterday. You now know two different ways to solve the same problems - both are correct and useful in different situations.
Task 5: Counting Sheep - Both Ways
Letβs revisit the sheep counting problem and see how both approaches work:
# Method 1: Traditional for loop (what you just practiced)
print("Method 1 - Traditional Loop:")
for i in range(1, 6):
print(f"{i} sheep")
print("\nMethod 2 - List Comprehension + Loop:")
# Method 2: List comprehension (what you learned yesterday) + loop
= [f"{i} sheep" for i in range(1, 6)]
sheep_list for sheep in sheep_list:
print(sheep)
print("\nMethod 3 - List Comprehension Only:")
# Method 3: Just create the list and print it
= [f"{i} sheep" for i in range(1, 6)]
sheep_list print(sheep_list)
Method 1 - Traditional Loop:
1 sheep
2 sheep
3 sheep
4 sheep
5 sheep
Method 2 - List Comprehension + Loop:
1 sheep
2 sheep
3 sheep
4 sheep
5 sheep
Method 3 - List Comprehension Only:
['1 sheep', '2 sheep', '3 sheep', '4 sheep', '5 sheep']
Task 6: Sum of Numbers - Both Ways
Now letβs see different approaches to calculating sums:
# Method 1: Traditional loop with accumulator (what you just practiced)
= 0
total for i in range(1, 11):
+= i
total print(f"Method 1 - Traditional loop sum: {total}")
# Method 2: List comprehension + built-in sum function
= [i for i in range(1, 11)]
numbers = sum(numbers)
total print(f"Method 2 - Comprehension + sum(): {total}")
# Method 3: Even more concise (advanced Python)
= sum(range(1, 11))
total print(f"Method 3 - Built-in functions: {total}")
Method 1 - Traditional loop sum: 55
Method 2 - Comprehension + sum(): 55
Method 3 - Built-in functions: 55
Practice: Choose Your Approach
Now itβs your turn! Try solving this problem using both approaches:
Challenge: Create a list containing the squares of numbers 1 through 5
# Approach 1: Traditional loop
= []
squares_traditional # Your code here - use a for loop to add squares to the list
print("Traditional approach:", squares_traditional)
Traditional approach: []
# Approach 2: List comprehension
# Your code here - use a list comprehension to create squares
# print("Comprehension approach:", squares_comprehension)
Discussion Questions
With your partner, discuss:
- Which approach felt more natural to you for each problem?
- When might you prefer the traditional loop approach?
- When might list comprehensions be better?
- What are the advantages of knowing both approaches?
Key Takeaways
- Both approaches are correct - it depends on the situation and your preference
- Traditional loops are great when you need:
- Complex logic or conditions
- Step-by-step processing
- To print or process items one at a time
- List comprehensions are excellent when you need to:
- Transform data into a new list
- Apply simple operations to sequences
- Write more concise, readable code
You now have multiple tools in your Python toolkit! π οΈ
Part 3: While Loops (15 minutes)
Task 7: Countdown
Create a simple countdown program:
- Set a variable
countdown = 5
- Use a while loop to print the countdown from 5 to 1
- After each print, decrease the countdown by 1
- When the countdown reaches 0, print βBlast off!β
= 5
countdown
# Your code here
# Use a while loop for the countdown
Conclusion and Discussion (5 minutes)
With your partner, briefly discuss:
- Which control structure (if, for, or while) did you find easiest to use?
- Can you think of a real-life situation where you might use each of these control structures?
Remember, itβs okay if you donβt finish all tasks. The goal is to practice and understand these concepts. Good luck and enjoy coding together!