Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skills—perfect for learners of all ages.
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Listen to a student-teacher conversation explaining the topic in a relatable way.
Signup and Enroll to the course for listening the Audio Lesson
Today, we are going to dive into the 'for' loop. Can anyone tell me what a loop is in programming?
Isn't it a way to repeat a set of statements?
Exactly! Loops help us to execute code a specific number of times or for each item in a sequence. The 'for' loop is specifically for iterating over sequences like lists, strings, or ranges.
What does 'iterate' mean?
'Iterate' means to go through each item in a sequence one at a time. For example, if you have a list of numbers, a 'for' loop can help us access each number in turn. Let's look at the syntax.
The syntax is as follows: `for variable in sequence:` followed by the code block we want to execute. Remember, the 'variable' will take on each value in the 'sequence' during each iteration.
Can we see a code example?
"Sure! Here's a simple example:
Signup and Enroll to the course for listening the Audio Lesson
Now let's dig deeper into the range() function. Who can remind us what it does within a for loop?
It generates a sequence of numbers.
Correct! The function can be called in a few different ways: `range(n)` to get numbers from 0 to n-1, `range(start, stop)` to count from start to stop-1, or `range(start, stop, step)` to define how much to increment by each time. Could anyone give me an example?
What if we use `range(2, 10, 2)`?
"Great thought! This will give us even numbers from 2 to 8 if we loop through it. Like this:
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
In this section, you will learn how the 'for' loop functions in Python, particularly in looping through various sequences such as lists, strings, and ranges. You will also explore the usage of the 'range()' function to generate number sequences for iterations.
The 'for' loop is a fundamental control structure in Python that facilitates the execution of a group of statements multiple times. It iterates over a specified sequence, allowing for easier management of repetitive tasks in code. In this section, the primary focus will be on:
range()
function is widely utilized within 'for' loops to provide sequences of numbers.range(n)
: gives numbers from 0
to n-1
.range(start, stop)
: generates numbers from start
to stop-1
.range(start, stop, step)
: increments numbers by step
.The ability to iterate over various sequences, such as lists and strings, leads to concise code and less redundancy, ultimately making your code more efficient and maintainable.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
Used to iterate over sequences (like lists, strings, or ranges).
The for loop is a control structure that allows you to run a block of code repeatedly for each item in a sequence. A sequence can be a list, string, or even a range of numbers. The loop automatically takes each item one after the other without needing to manually specify the iteration.
Think of a for loop like a teacher going down a list of names to call students one by one to present their project. Instead of randomly picking students, the teacher sequentially checks off each name on the list.
Signup and Enroll to the course for listening the Audio Book
Syntax:
for variable in sequence:
# code block
The syntax starts with the keyword 'for', followed by a variable name that will represent each item in the sequence. The 'in' keyword indicates the sequence being iterated over, and the code block (indented under the loop) is what will be executed for each item. It’s important to ensure proper indentation, as this shows which code belongs to the loop.
Imagine a recipe with steps. The 'for' is like saying 'for each ingredient in the recipe, do the following'. Each ingredient has specific instructions, and you carry them out in a precise order (indented steps) because each step relates directly to the ingredient being checked.
Signup and Enroll to the course for listening the Audio Book
✅ Example:
for i in range(5):
print(i)
Output:
0
1
2
3
4
In this example, 'range(5)' generates a sequence of numbers from 0 to 4. The for loop assigns each number to the variable 'i' and executes the print function, which displays the value of 'i'. This continues until all numbers in the range have been processed.
Think of it like a countdown timer that goes from 4 to 0. Every second, the timer shows the next number down on the screen until it reaches zero. Just like the timer, the for loop counts through its list of numbers sequentially.
Signup and Enroll to the course for listening the Audio Book
🎯 range() Function
● range(n) – from 0 to n-1
● range(start, stop) – from start to stop-1
● range(start, stop, step) – increment by step
✅ Example:
for num in range(2, 10, 2):
print(num)
The 'range()' function is a built-in Python function that generates a sequence of numbers. You can specify the starting point, the endpoint, and the step size. In the example, 'range(2, 10, 2)' starts at 2, stops before 10, and counts by increments of 2, which results in printing even numbers: 2, 4, 6, and 8.
If you think about counting coins, consider that you only pick the even coins (2-cent, 4-cent, etc.) from the pile starting from 2 up to just before 10. You're selectively choosing the ones you want based on your specified criteria.
Learn essential terms and foundational ideas that form the basis of the topic.
Key Concepts
For Loop: A loop constructs that iterates over sequences.
Sequence: Any ordered collection like lists or strings.
Range Function: Generates a sequence of numbers for iterations.
Iteration: The act of looping through items in a sequence.
See how the concepts apply in real-world scenarios to understand their practical implications.
Using for loop to print numbers from 0 to 4:
for i in range(5):
print(i)
Using for loop with the range function for even numbers:
for num in range(2, 10, 2):
print(num)
Use mnemonics, acronyms, or visual cues to help remember key information more easily.
In Python, the for loop does roam, over lists and ranges, it finds a home.
Imagine a robot that needs to count to five. With a for loop, it easily moves one by one from zero to five.
To remember the for loop: F - Follows, O - Over, R - Ranges.
Review key concepts with flashcards.
Review the Definitions for terms.
Term: for Loop
Definition:
A control structure for executing a block of code multiple times, iterating over a sequence.
Term: range() Function
Definition:
A built-in Python function that generates a sequence of numbers, commonly used for iterations in loops.
Term: Iteration
Definition:
The process of executing a block of code repeatedly, once for each item in a sequence.
Term: Sequence
Definition:
An ordered collection of items, like lists, strings, or any range of numbers.