Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, 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!
Now 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.
Next, 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?
For loop, do take note, through each value, it will float; while conditions stay so true, the loop keeps running, that's the cue.
Imagine a group of friends trying to count the stars. If they see an odd star shining, they skip saying it aloud, continuing on with the even ones, so all even stars are counted.
Famous rhyme for loop: 'For Flies, While Conditions, Continually Count!'
L.E.F.T - Loops Execute Flowing Tasks (to remember how loops operate).
Term
For Loop Definition
Click to flip
Term
While Loop Definition
Click to flip
Term
Continue Statement Function
Click to flip
A loop that iterates over a sequence, allowing execution of code multiple times.
A loop that repeats as long as a specified condition is true.
A control statement that skips the current iteration of the loop and proceeds to the next.
A built-in function that generates a sequence of numbers, often used in loops.
This section presents hands-on exercises to apply knowledge of loops in Python.
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.
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.