Welcome! In programming, just like playing a favorite song on repeat, loops execute code repeatedly. Here, we'll explore the "For Loop" in Python, an iteration construct over sequences such as lists or strings.
Imagine a train journey: the train represents our loop, stopping at each station. Each station represents an item on its route, which is the iterable
.
Like replaying a song or game level, a loop continually executes a block of code until a defined condition is met. It's akin to saying, "Keep the popcorn machine running as long as the popcorn keeps popping!"
A Python For Loop looks like this:
In this construct, for
and in
are keywords. The variable
holds the current item in each iteration, while iterable_object
can be a list, string, or any object that provides an item sequentially.
Let's print all elements of a list:
This code will print every planet from the list (, , , ...), each on a separate line.
