Lecture - 4 (Python) E-Notes
Lecture - 4 (Python) E-Notes
Lecture – 4
Collections In Python
List In Python
List is used to represent group of elements into a single entity (or) object.
We can perform the operations on the elements of the list object by using ‘indexes’.
We can create the list object by using “list()” function and by using square brackets “[ ]”.
• Tuple objects are immutable objects i.e., once if you create tuple object with same element, later we can’t
modify the elements of that object.
print(tuple1)
count = 0
for i in tuple1:
Output:-
tuple1[0] = 10
tuple1[0] = 20
tuple1[0] = 30
tuple1[0] = 40
tuple1[0] = 50
tuple1[0] = 60
Example Application – 6
print(tuple1)
count = 0
for i in tuple1:
List Tuple
List objects are mutable objects. Tuple objects are immutable objects.
Iterating the list is slower. Iterating tuple is faster.
List objects are not used as a key for The tuple objects which contain
the dictionary immutable objects can be used as a key
for the dictionary.
If the data changes frequently then it is If the data doesn’t change then it is
recommended to represent the data by recommended to represent the data by
using list. using tuple.
Dictionary In Python
• Dictionary is used to represent group of key, value pairs into a single entity.
• .
Example Application - 8
#Dictionary Demo..
dict1={
'empid':'SPI1003',
'empname':'Brijesh Mishra',
'department':'Development',
'salary':50000
}
for k,v in dict1.items():
print(k,":",v).