Python List


  • A list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created.
  • The items in the list are separated with the comma (,) and enclosed with the square brackets [].

Characteristics of Lists

  • The lists are ordered.
  • The element of the list can access by index.
  • The lists are the mutable type.
  • The lists are mutable types.
  • A list can store the number of various elements.

=======================================================================
Program 1:

print("#example 1")

l1=["john", 102,"prabhu"]
l2=[1,2,3,4,5,6]
print("type of l1 is :",type(l1))
print("type of l2 is :",type(l2))
print("===================================================")

print("#example 2")
a = [1,2,"Peter",4.50,"Ricky",5,6]  
b = [1,2,5,"Peter",4.50,"Ricky",6] 
print("checking the order of lists a,b:",a==b)
print("===================================================")


print("#example 3")
a = [1,2,"Peter",4.50,"Ricky",5,6]  
b = [1,2,"Peter",4.50,"Ricky",5,6]  
print("checking the order of lists a,b:",a==b)
print("===================================================")



OUTPUT:

#example 1
type of l1 is : <class 'list'>
type of l2 is : <class 'list'>
===================================================
#example 2
checking the order of lists a,b: False
===================================================
#example 3
checking the order of lists a,b: True
===================================================


=======================================================================
Program 2:


dept=["cse",400,"hadoop",1000]
print("dept details")
print("                                                                                                      ")
print("dept name is : %s,\ndept name is : %s\n" %(dept[0],dept[2]))
print("dept id is : %d,\ndept is is : %d\n" %(dept[1],dept[3]))
OUTPUT:
dept details
                                                                                                      
dept name is : cse,
dept name is : hadoop
dept id is : 400,
dept is is : 1000


=======================================================================

Program 3:

emp = ["John", 102,"USA"]     
Dep1 = ["CS",10]  
Dep2 = ["IT",11]    
HOD_CS = [10,"Mr. Holding"]    
HOD_IT = [11, "Mr. Bewon"] 
print("printing employee data...") 
print("Name : %s, ID: %d, Country: %s"%(emp[0],emp[1],emp[2])) 
print("                                                                                                         ")
  
print("printing departments...")   
print("Department 1:\nName: %s, ID: %d\nDepartment 2:\nName: %s, ID: %s"%(Dep1[0],Dep2[1],Dep2[0],Dep2[1]))   
print("                                                                                                         ")

print("HOD Details ....")    
print("CS HOD Name: %s, Id: %d"%(HOD_CS[1],HOD_CS[0]))  
print("                                                                                                         ")
 
print("IT HOD Name: %s, Id: %d"%(HOD_IT[1],HOD_IT[0]))    
print(type(emp),type(Dep1),type(Dep2),type(HOD_CS),type(HOD_IT)) 
print("                                                                                                         ")


OUTPUT:

printing employee data...
Name : John, ID: 102, Country: USA
                                                                                                         
printing departments...
Department 1:
Name: CS, ID: 11
Department 2:
Name: IT, ID: 11
                                                                                                         
HOD Details ....
CS HOD Name: Mr. Holding, Id: 10
                                                                                                         
IT HOD Name: Mr. Bewon, Id: 11
<class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'>
=======================================================================