For in Python
Here’s the Python translation of the Go “For” example, formatted in Markdown suitable for Hugo:
Python offers several ways to create loops, with the for
loop being the most common. Let’s explore different types of for
loops in Python.
When you run this program, you’ll see:
Python’s for
loop is quite versatile. It can iterate over any sequence (list, tuple, dictionary, set, or string) or other iterable objects. The range()
function is commonly used to generate a sequence of numbers.
The while
loop in Python is equivalent to the condition-only for
loop in some other languages. It will continue looping until the condition becomes false or a break
statement is encountered.
The continue
statement is used to skip the rest of the code inside the loop for the current iteration and move to the next iteration.
We’ll see some other forms of iteration later when we look at list comprehensions, generators, and other data structures.