Python loop example - loops in python With Examples

In this tutorial you are going to learn about Python Loop Techniques. In previous tutorial we discussed about Python pass statement. If you are interested, you can click this link to read that tutorial.

Python Loop

We learned about Python loop before. But Python’s loop is more flexible than that of other language. We can do more interesting things here. Python’s for loop is versatile. We are going to see some examples about this.

Python Loop over a Sequence

This is a very common example of Python for loop. Suppose we have a sequence of items and we need to traverse the sequence one by one. We can use for loop like this:


#initialize a list
items = ["apple", 1, 4, "exit", "321"]
#for each item in the list traverse the list
for item in items:
        # print the item
        print (item),

The output of the following code will be


================== RESTART: /home/imtiaz/Desktop/ltech1.py ==================
apple 1 4 exit 321
>>>

Python Loop over a Sequence in Reverse Order

You can also print the previous example int reverse order. To do so, you have to use reversed() function. reversed() function reverse the order of a sequence. Have a look over the following code.


#initialize a list
items = ["apple", 1, 4, "exit", "321"]
#for each item in the list traverse the list
#before that reverse the order of the list
for item in reversed(items):
        # print the item
        print (item),

The output will be


================== RESTART: /home/imtiaz/Desktop/ltech2.py ==================
321 exit 4 1 apple
>>>

Python Loop over a Sequence in Sorted Order

You can also print the previous example int sorted order. To do so, you have to use sorted() function. sorted() function sort the order of a sequence. Have a look over the following code.


#initialize a list
items = [7, 1, 4, 9, 3]
#for each item in the sorted list, traverse the list
for item in sorted(items):
        # print the item
        print (item),

The output will be


================== RESTART: /home/imtiaz/Desktop/ltech4.py ==================
1 3 4 7 9
>>>

Enumerate Values and Corresponding Index

You can also enumerate values of a sequence along with their indexes. To do so, you have to use enumerate() function. The following code will help understand the thing.


#initialize a list
items = [7, 1, 4, 9, 3]
#for each item in the list traverse the list
for index,value in enumerate(items):
        # print the index along with their value
        print ("value of "+str(index)+" is = "+str(value))

The output will be

Python Loop

Traversing Two or More Sequences

Using python for loop you can traverse two or more sequences at the same time. For example, in one sequence you have a list of name and in another sequence you have the list of hobbies of the corresponding persons. So you have to print persons’ name along with their hobbies. So the following example will guide you to do this.


names = [ 'Alice', 'Bob', 'Trudy' ]
hobbies = [ 'painting', 'singing', 'hacking']
ages = [ 21, 17, 22 ]
#combine those list using zip() function
for person,age, hobby in zip(names,ages,hobbies):
        print (person+" is "+str(age)+"  years old and his/her hobby is "+hobby)

The output will be


Alice is 21  years old and his/her hobby is painting
Bob is 17  years old and his/her hobby is singing
Trudy is 22  years old and his/her hobby is hacking
>>>

If you practice more, day by day you will learn many Interesting things about python. That’s all about Python loop example. Hope that you understood well. For any query, please comment below.
#HappyCoding

By admin

Leave a Reply

%d bloggers like this: