Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
5.6. Try It Yourself
Interactive Audio Lesson
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountToday, we will work on a task using a for loop. Can someone tell me what a for loop is?
It's used to iterate over a sequence, right?
Exactly! So, let's use a for loop to print numbers from 1 to 10. Remember, we can use the range function to set our sequence. Who can show me how to set this up?
We can use for i in range(1, 11): and then print i.
Great job! Can anyone tell me how the range function works in this case?
It generates numbers starting from 1 up to but not including 11.
Correct! Remember, the syntax is crucial. Recap: for is the keyword, followed by a variable, then in and the sequence. Now, let's run the code together!
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountNow let's shift our focus to while loops. Who can remind us how a while loop operates?
It keeps looping as long as the condition is true.
Correct! Let's calculate the sum of the first 10 natural numbers using a while loop. What should our condition be?
We start at 0 and loop until our count reaches 10!
Perfect! Can someone write the code structure we will need?
We can start with count = 0 and sum = 0, then use while count < 10: to add count to sum and increment count.
Great teamwork! This exercise is allowing us to practice variable handling and looping. Now let's code this step by step.
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountNext, let's talk about the continue statement. Who remembers its function?
It skips the current iteration and continues with the next one.
Exactly! Let’s use continue to print only even numbers from 1 to 20. How can we set this up with a for loop?
We can check if a number is odd and use continue to skip printing it.
Well said! So, let’s write a for loop: for i in range(1, 21):. How do we check for even numbers?
We can use if i % 2 != 0: to check for odd numbers.
Perfect execution! After applying continue, how does that change our output?
Overview
Medium Summary
In this section, learners will engage with practical exercises designed to deepen their understanding of for loops and while loops through interactive programming challenges, including tasks involving ranges and conditional statements.
Detailed Summary
Detailed Summary
The 'Try It Yourself' section is designed to provide students with opportunities to apply their understanding of loops in Python through practical exercises. Learners are guided to implement a for loop to print numbers from 1 to 10, utilize a while loop to calculate the sum of the first 10 natural numbers, and employ a continue statement to print only even numbers from 1 to 20. These exercises encourage the application of theoretical concepts covered in the earlier parts of the chapter, reinforcing the material through active engagement.
Audio Book
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free account- Print all numbers from 1 to 10 using a for loop.
Detailed Explanation
In this task, you're asked to use a for loop to print the numbers sequentially from 1 to 10. A for loop iterates over a sequence, and in this case, we will use the range() function to create that sequence. The range(1, 11) will generate numbers starting from 1 up to (but not including) 11, which gives us the numbers from 1 to 10. The basic structure will look like this:
for num in range(1, 11):
print(num)Examples & Analogies
Imagine you're counting your favorite toys. You want to show your friend how many you have, so you start from 1 and count up to 10 out loud, one by one. This is just like using a for loop to print numbers; you go through each number in order, like counting your toys.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free account- Write a program to calculate the sum of first 10 natural numbers using a while loop.
Detailed Explanation
In this exercise, you need to calculate the sum of the first ten natural numbers: 1, 2, 3, ..., up to 10. A while loop will be suitable for this. Start by initializing a counter variable to 1 and a sum variable to 0. Then, as long as the counter is less than or equal to 10, you will add the counter value to the sum and increment the counter by 1. The structure will look like this:
count = 1
sum = 0
while count <= 10:
sum += count
count += 1
print(sum)Examples & Analogies
Think about collecting stickers. You have 10 stickers, and you want to know how many you have in total as you collect them one by one. Each time you get a sticker, you add its number to your total count. The while loop helps you continue this process until you've counted all 10 stickers.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free account- Print only even numbers from 1 to 20 using continue.
Detailed Explanation
This task asks you to print only the even numbers from 1 to 20. You can use a for loop combined with the continue statement. In this loop, you will check if a number is odd. If it is, you will use continue to skip the current iteration and move to the next number. The structure will look like this:
for num in range(1, 21):
if num % 2 != 0:
continue
print(num)Examples & Analogies
Imagine you're sorting a box of fruits and you only want to keep the apples while discarding the pears. As you take each fruit out of the box, if it’s a pear (an odd number of fruits), you ignore it and move on to the next one. This is similar to how the continue statement works – you skip over what you don't want and keep focusing on the even numbers.
--
Key Concepts
Core takeaways and short definitions to help you quickly recall the key ideas from this section.
For Loop: A construct to iterate over a sequence.
While Loop: Repeats actions as long as a condition remains true.
Continue Statement: Skips the rest of the loop's current iteration.
Range Function: Generates number sequences used in for loops.
Examples
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
Stories
Flash Cards
Glossary
For Loop
A loop that iterates over a sequence, allowing execution of code multiple times.
While Loop
A loop that repeats as long as a specified condition is true.
Continue Statement
A control statement that skips the current iteration of the loop and proceeds to the next.
Range Function
A built-in function that generates a sequence of numbers, often used in loops.