Python loops – Examples and Techniques

Filed Under: Python

In this tutorial, we’ll look at the different Python loops and explore them in detail with examples and techniques. We’ll also answer some of the common loop related examples here.

How to work with loops in Python?

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

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

1. Jobet says:

Had to change the below code when trying to print the list of integers as –

for person,age, hobby in zip(names,ages,hobbies):
print(person + ” is \t “,age,” years old and his/her hobby is ” + hobby)

2. kowsi says:

why we use str(index),str(value) to print that values in enumerate

1. Ari says:

We uses use str(index) because we must convert (index) into a string before we can use the “+” operator to concatenate it to other strings

We uses use str(value) to because we must convert (value) into a string before we can use the “+” operator to concatenate it to other strings — in case (value) is not a string.

3. Babanna S Duggani says:

Thanks Imtiaz for sharing such a detailed tutorials on Python with examples for each construct. I wanted to learn python for long time and this material helped me a lot in hands on learning with detailed explanation, Hoping to see many more articles in future.