The Basic For Loop
The for loop is much simpler in Python than in most other languages
for count in range(0,5)
The function range( ) is called with three arguments:
range ( start ,end, step)
Making the loop interesting
Lets look at some different type of code
#!/usr/bin/env python
list = [2,4,6,8,10]
total = 0
for num in list:
total = total + num
print ("The total is: %d" % total)
So what does this code do? We have a list of items. These numbers are stored in a
large variable called an array. This array can hold many different values not just one.
As we go through the for loop, the loop first looks at the first item in the list then
the next then the next and so on, Each time the loop is repeated it looks at the next
item in the list.
For loops are available in most languages and are used when you know how many times you want to do something. The loops can go forwards as well as backwards
No comments:
Post a Comment