Python | Program to print a list using 'FOR and IN' loop

Here, we are going to learn how to print elements/objects of a list using FOR and IN loop in Python? By IncludeHelp Last updated : June 21, 2023

print a list using 'FOR and IN' loop

Given a Python list and we have to print its all elements using FOR and IN loop.

'FOR and IN' loop

FOR and IN constructs as loop is very useful in the Python, it can be used to access/traverse each element of a list.

Syntax of for ... in loop

for variable in list_name:
    Statements

Python program to print a list using 'FOR and IN' loop

# Declaring a list
list = [10, 20, 30, 40, 50]

# printing without using FOR and IN
print("List elements are: ", list)
print() # prints new line

# printing using FOR and IN
print("List elements are: ")

for L in list:
	print(L)

print() # prints new line

# calculating Sum of all elements
sum = 0 
for L in list:
	sum += L 
print("Sum is: ", sum)

Output

List elements are:  [10, 20, 30, 40, 50]
 
List elements are: 
10
20
30
40
50
 
Sum is:  150

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





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