Home »
Python »
Python programs
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?
Submitted by IncludeHelp, on July 20, 2018
Given a list and we have to print its all elements using FOR and IN loop in Python.
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
Example/Programs: Declaring and printing a list
# 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 »
ADVERTISEMENT
ADVERTISEMENT