Python List
A list in python is used to store the
sequence of various types of data.
A list can be defined as a collection of
values or items of different types.
Python list are mutable type.
The item in the list are separated with
comma(,) and enclosed with the square
brackets[].
List Declaration: -
# a simple List
list1 = [1,2,”python”,”Program”,12.3]
list2 =
[“Army”,”Navy”,”Airforce”,”Police”]
# print the list
print(list1)
print(list2)
#printing the type of list
print(type(list1))
print(type(list2))
o/p: -
[1, 2, 'Python', 'Progam', 12.3]
['Army', 'Navy', 'Airforce', 'Police']
<class 'list'>
<class 'list'>
Characteristics of Lists: -
The list has some characteristics which
are going to see below.
The lists are ordered sequence.
The elements of the list can
access by index.
The lists are the mutable type.
A list can store the number of
various elements.
Ordered List checking: -
#example
a = [1,2,"Ram",3.50,"Rahul",5,6]
b = [1,2,5,"Ram",3.50"Rahul",6]
a == b
o/p: - False
#example
a = [1,2,"Ram",3.50,"Rahul",5,6]
b = [1,2,"Ram",3.50,"Rahul",5,6]
a == b
O/p: - True
Lists permanently preserve the
element’s order.
Example: -
#list example in details
emp = [“John”, 102, “USA”]
Dep1 = [“CS”,10]
Dep2 =[“IT”,11]
HOD_CS = [10,”Mr.Rahul”]
HOD_IT = [11,”Mr.Balu”]
print(“printing employee data…”)
Print(“Name : %s, ID: %d, Country:
%s” %(emp[0],emp[1],emp[2])
print(“printing departments...”)
print(“Department1:\nName: %s.ID:
%d\n Department2:\nName:%s, ID:
%s”%
(Dep1[0],Dep2[1],Dep2[0],Dep2[1]))
print(“HOD Details…”)
print(“CS HOD Name: %s, id: %d”
% (HOD_CS[1],HOD_CS[0]))
print(“IT HOD Name: %s, id: %d %
(HOD_IT[1],HOD_IT[0]))
o/p: -?
ERROR!
printing employee data…
Traceback (most recent call last):
File "<string>", line 10, in <module>
NameError: name 'Print' is not defined.
Did you mean: 'print'?