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 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:
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:
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.
For: F - For each, O - Object, R - Repeatedly.
Term
What is a for loop?
Click to flip
Term
What does the range function do?
Click to flip
Term
What is the syntax of a for loop?
Click to flip
A control structure for executing a block of code multiple times, iterating over a sequence.
A built-in Python function that generates a sequence of numbers, commonly used for iterations in loops.
The process of executing a block of code repeatedly, once for each item in a sequence.
An ordered collection of items, like lists, strings, or any range of numbers.
The 'for' loop in Python is a powerful construct that allows you to iterate over sequences, enabling efficient execution of code blocks multiple times.
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:
for variable in sequence:
# code blockrange() 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.