For Each Loop Example in Python

Python for each loop example: Here, we are going to implement a program that will demonstrate examples/use of for each loop. By Pankaj Singh Last updated : April 13, 2023

Why For Each Loop is Used?

The for each loop is mainly used to traverse the elements of container type of data type such as sets, lists, dictionaries, etc.

For Each Loop Example in Python

The following example shows the usage of for each loop in Python.

Problem Statement

In this example, we have a list of fruits, we have to print its type and individual values (elements) using for each loop.

Python Solution

# declare and initialize a list 
fruits = ["apple","mango","guava","grapes","pinapple"]

# pritning type of fruits
print (type(fruits))

# printing value 
for fruit in fruits:
    print(fruit)

Output

<class 'list'>
apple
mango
guava
grapes
pinapple

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.