Created
February 10, 2025 01:32
-
-
Save shark0517/5e0401374958aff95b9f71dcce1b858e to your computer and use it in GitHub Desktop.
Homework Lesson 7 Loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Homework: Loops | |
# 🔥Read carefully until the end before you start solving the exercises🔥 | |
# Practice the Basics 💪🏻 | |
# You can uncomment or type the necessary code on each task | |
# --------------------------------------------------------------------- | |
# Task 1. Create a basic for loop | |
# Complete the following code in such a way that this loop prints the characters | |
# of `name` one at a time. | |
# name = "Joseph" | |
# for ??? in ???: | |
# print(???) | |
# Given name | |
name = "Joseph" | |
# Loop through each character in name and print it | |
for character in name: | |
print(character) | |
# --------------------------------------------------------------------- | |
# Task 2. Create a basic `for` loop with a counter | |
# Complete the following code in such a way that the loop increments the counter | |
# and prints the number of characters in `name`name at the end. | |
# name = 'Tom' | |
# counter = ??? | |
# for ??? in ???: | |
# counter = ??? | |
# This should print '3' | |
# print(counter) | |
name = 'Tom' | |
counter = 0 | |
# Loop through each character and count | |
for character in name: | |
counter += 1 | |
print("Character count:", counter) | |
# --------------------------------------------------------------------- | |
# Task 3. Create a basic 'while' loop | |
# Complete the following code in such a way that the loop exits after five iterations, without using break. | |
# 🔥 Hint: Think of it as: while counter is under 5, increment the counter and print its value🔥 | |
""" | |
This should print: | |
1 | |
2 | |
3 | |
4 | |
5 | |
""" | |
# counter = ??? | |
# while counter ???: | |
# counter = ??? | |
# print(counter) | |
counter = 1 | |
while counter <= 5: | |
print(counter) | |
counter += 1 | |
# --------------------------------------------------------------------- | |
# Task 4. Exit a loop using break 🛑 | |
# Take the previous example, and modify it so you exit the loop after five iterations, | |
# but this time do it using break. | |
# counter = ??? | |
# while ???: | |
# counter = ??? | |
# if ???: | |
# ??? | |
# print(counter) | |
counter = 0 | |
while True: | |
counter += 1 | |
if counter > 5: | |
break | |
print(counter) | |
# --------------------------------------------------------------------- | |
# Task 5. Range | |
# Remember that range(start, end, step) behaves somewhat like list slicing, so start is inclusive, | |
# end is exclusive, and step is optional. | |
# Figure out the values required for range() to generate the expected output. | |
# 0, 1, 2, 3, 4, 5 (use only one argument) | |
# range(???) | |
# 0, 1, 2, 3, 4, 5 (use two arguments: start and end) | |
# range(???) | |
# Odd numbers between 0 and 10: 1, 3, 5, 7, 9 | |
# range(???) | |
# Generates: 0, 1, 2, 3, 4, 5 | |
print(list(range(6))) | |
# Generates: 0, 1, 2, 3, 4, 5 | |
print(list(range(0, 6))) | |
# Generates odd numbers: 1, 3, 5, 7, 9 | |
print(list(range(1, 10, 2))) | |
# --------------------------------------------------------------------- | |
# Task 6. Using range() in a loop | |
# Remember that range() returns an iterable, so you will usually find it used in a for loop. | |
# Complete the following code so it prints the even numbers between 0 and 10; | |
# for ??? in range(???): | |
# print(???) | |
# Print even numbers between 0 and 10 | |
for number in range(0, 11, 2): | |
print(number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Homework: Loops | |
# 🔥Read carefully until the end before you start solving the exercises🔥 | |
# Practice the Basics 💪🏻 | |
# You can uncomment or type the necessary code on each task | |
# --------------------------------------------------------------------- | |
# Task 1. Create a basic for loop | |
# Complete the following code in such a way that this loop prints the characters | |
# of `name` one at a time. | |
# name = "Joseph" | |
# for ??? in ???: | |
# print(???) | |
# Given name | |
name = "Joseph" | |
# Loop through each character in name and print it | |
for character in name: | |
print(character) | |
# --------------------------------------------------------------------- | |
# Task 2. Create a basic `for` loop with a counter | |
# Complete the following code in such a way that the loop increments the counter | |
# and prints the number of characters in `name`name at the end. | |
# name = 'Tom' | |
# counter = ??? | |
# for ??? in ???: | |
# counter = ??? | |
# This should print '3' | |
# print(counter) | |
name = 'Tom' | |
counter = 0 | |
# Loop through each character and count | |
for character in name: | |
counter += 1 | |
print("Character count:", counter) | |
# --------------------------------------------------------------------- | |
# Task 3. Create a basic 'while' loop | |
# Complete the following code in such a way that the loop exits after five iterations, without using break. | |
# 🔥 Hint: Think of it as: while counter is under 5, increment the counter and print its value🔥 | |
""" | |
This should print: | |
1 | |
2 | |
3 | |
4 | |
5 | |
""" | |
# counter = ??? | |
# while counter ???: | |
# counter = ??? | |
# print(counter) | |
counter = 1 | |
while counter <= 5: | |
print(counter) | |
counter += 1 | |
# --------------------------------------------------------------------- | |
# Task 4. Exit a loop using break 🛑 | |
# Take the previous example, and modify it so you exit the loop after five iterations, | |
# but this time do it using break. | |
# counter = ??? | |
# while ???: | |
# counter = ??? | |
# if ???: | |
# ??? | |
# print(counter) | |
counter = 0 | |
while True: | |
counter += 1 | |
if counter > 5: | |
break | |
print(counter) | |
# --------------------------------------------------------------------- | |
# Task 5. Range | |
# Remember that range(start, end, step) behaves somewhat like list slicing, so start is inclusive, | |
# end is exclusive, and step is optional. | |
# Figure out the values required for range() to generate the expected output. | |
# 0, 1, 2, 3, 4, 5 (use only one argument) | |
# range(???) | |
# 0, 1, 2, 3, 4, 5 (use two arguments: start and end) | |
# range(???) | |
# Odd numbers between 0 and 10: 1, 3, 5, 7, 9 | |
# range(???) | |
# Generates: 0, 1, 2, 3, 4, 5 | |
print(list(range(6))) | |
# Generates: 0, 1, 2, 3, 4, 5 | |
print(list(range(0, 6))) | |
# Generates odd numbers: 1, 3, 5, 7, 9 | |
print(list(range(1, 10, 2))) | |
# --------------------------------------------------------------------- | |
# Task 6. Using range() in a loop | |
# Remember that range() returns an iterable, so you will usually find it used in a for loop. | |
# Complete the following code so it prints the even numbers between 0 and 10; | |
# for ??? in range(???): | |
# print(???) | |
# Print even numbers between 0 and 10 | |
for number in range(0, 11, 2): | |
print(number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment