Iteration & Loops
In this chapter, we are going to explore iteration and loops in Python.
Loops are used in computer programming to automate repetitive tasks.
Types of Loops in Python
In python, there are two types of loops.
For Loop
In Python, the most common form of iteration is the “for” loop.
A for
loop is used for iterating over a sequence of items.
The general syntax for creating a for
loop is:
for index in sequence:
statement
Here, a sequence can be a list, tuple, dictionary, set, or string.
Note: The for loop does not require an iterating/ indexing variable to set beforehand. The indexing can be defined in a loop.
Looping Through a List
The “for” loop allows you to iterate over all items of a list, such that you can do whatever you want with each item.
For instance, let’s create a list and print the square value of each element.
Get hands-on with 1200+ tech skills courses.