Python | Programa para declarar e imprimir una lista

Aquí, vamos a aprender a declarar una lista y cómo imprimir los elementos de la lista en Python ?

Declarar una lista e imprimir los elementos / objetos de la lista en Python.

Programa:

# 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

salida

    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


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *