DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1
Grade is F
>>>

For Loops


The for statement allows you to create a loop that
continues to iterate through the code a specific number
of times. It is also referred to as a counting loop and can
work through a sequence of items, such as a list or other
data objects. The for loop is heavily used to parse
through data and is likely to be your go-to tool for
working with data sets. A for loop starts with the for
statement, followed by a variable name (which is a
placeholder used to hold each sequence of data), the in
keyword, some data set to iterate through, and then
finally a closing colon, as shown in this example:


>>> dataset=(1,2,3,4,5)
>>> for variable in dataset:
... print(variable)
...
1
2
3
4
5

The for loop continues through each item in the data set,
and in this example, it prints each item. You can also use
the range() function to iterate a specific number of
times. The range() function can take arguments that let
you choose what number it starts with or stops on and
how it steps through each one. Here is an example:


>>> for x in range(3):
... print(x)
...
0
Free download pdf