Home » 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL








Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.