How to use “for” loop and “range” function in python ?

In this post, we will look how to use for loop in python and usage of range function.

To understand both of these parameters lets have a look at an example.

FOR loop and range function usage

In the example below, we can create an array is created called “numbers” and the items of the array is printed using for loop.

And similarly we have used range function to print the characters starting from 6 till 11 i.e. 6, 7, 8, 9, and 10.

user@ngelinux$ cat loop.py 
#!/usr/bin/python
numbers=[1,2,3,4,5]
for a in numbers:
    print(a)

for b in range(6,11):
  print(b)

Output

Lets have a look at the output of above command.

user@ngelinux$ ./loop.py 
1
2
3
4
5
6
7
8
9
10
user@ngelinux$ 

Leave a Reply

Your email address will not be published.