Home » Python programs

Python | Program to declare and print a list

Here, we are going to learn how to declare a list and how to print the list elements in Python?
Submitted by IncludeHelp, on July 20, 2018

Declare a list and print the elements/objects of the list in Python.

Program:

# declaring list with integer elements
list1 = [10, 20, 30, 40, 50]

# printing list1
print "List element are: ", list1


# printing elements of list1 by index
print "Element @ 0 index:", list1[0]
print "Element @ 1 index:", list1[1]
print "Element @ 2 index:", list1[2]
print "Element @ 3 index:", list1[3]
print "Element @ 4 index:", list1[4]


# declaring list with string elements
list2 = ["New Delhi", "Mumbai", "Chennai", "calcutta"]

# printing list2
print "List elements are: ", list2

#printing elements of list2 by index
print "Element @ 0 index:",list2 [0]
print "Element @ 1 index:",list2 [1]
print "Element @ 2 index:",list2 [2]
print "Element @ 3 index:",list2 [3]
print " " # prints new line

# declaring list with mixed elements
list3 = ["Amit Shukla", 21, "New Delhi", 9876543210]

#printing list3
print "List elements are: ", list3

# printing elements of list3 by index
print "Element @ 0 index (Name) :", list3[0]
print "Element @ 1 index (Age ) :", list3[1]
print "Element @ 2 index (City) :", list3[2]
print "Element @ 3 index (Mob.) :", list3[3]
print "" # prints new line

Output

    List element are:  [10, 20, 30, 40, 50]
    Element @ 0 index: 10
    Element @ 1 index: 20
    Element @ 2 index: 30
    Element @ 3 index: 40
    Element @ 4 index: 50
    List elements are:  ['New Delhi', 'Mumbai', 'Chennai', 'calcutta']
    Element @ 0 index: New Delhi
    Element @ 1 index: Mumbai
    Element @ 2 index: Chennai
    Element @ 3 index: calcutta
 
    List elements are:  ['Amit Shukla', 21, 'New Delhi', 9876543210]
    Element @ 0 index (Name) : Amit Shukla
    Element @ 1 index (Age ) : 21
    Element @ 2 index (City) : New Delhi
    Element @ 3 index (Mob.) : 9876543210





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



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.