Python | Program to declare and print a list

Declaring & Printing a List: In this tutorial, we will learn how to declare a list and how to print the list elements in Python. By IncludeHelp Last updated : June 21, 2023

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

Declaring and printing lists in Python

To declare a Python list, you have to provide the list elements inside square brackets separated by commas. And, to print the list there are multiple ways such as printing the complete list, printing the elements using the indices, and itertating over the elements using the loop. Consider the below example to declare the lists and print the lists with their elements.

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

Printing the list iterating over the elements

You can also print a list by iterating over the list elements using the for loop. Consider the below-given program in which we are declaring a list and printing the elements.

Program

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

# Iterating over the elements
for x in list1:
    print(x)

Output

10
20
30
40
50

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





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