Q:

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

belongs to collection: Python List Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Program to add an element at specified in... >>
<< Python | Program for Adding, removing elements in ...