×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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 »

Advertisement
Advertisement

Related Programs

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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