Previous | Next


Looping is about how you can repeat a block of statements until some condition is met. With a for loop, the mechanism for repeating a set of statements allows execution to continue until it exhausts all of your list’s data. List will be discussed later in this tutorial.

for <element> in <sequence>:
  <for-processing code>

Will will cover sequence object (e.g list, tuples, dictionary) soon. But, let’s try the following sample code with a simple list:

numbers = [0,1,2,3,4,5,6,7,8,9]
for x in numbers:
  print x

The result:

0
1
2
3
4
5
6
7
8
9

The for loop will iterate every element in the list object (numbers) and print its value. It will stop when it reach the last element in the list. you can use the for loop to print all the element in the sting as well. Take a look of the following example:

name = 'askyb'
for x in name
  print x

The result:

a
s
k
y
b

 


Previous | Next