Handouts
Handouts
needed
In [2]: #list in python are collection of data (may not be of the same type)
#creating a list
l1 = [] #will create empty list
l2 = list() #will create empty list
l3 = [1, 5, 7.6, 'hello']
#adding element to the list
l1.append(4)
l1.append(3)
l2.append(10)
l3.append(50)
print(l1, len(l1))
print(l2, len(l2))
print(l3, len(l3)) #len(list_name) will return the length of list i.e. number of elements
in it
[4, 3] 2
[10] 1
[1, 5, 7.6, 'hello', 50] 5
3 3
[5, 7.6]
1 5 7.6 hello 50
Hello
[1]
In [9]: #to create a copy of the list
l2_copy = l2.copy()
print(l2, l2_copy)
[10] [10]
[]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [12]: #dictionary in python can be thought same as the unordered map in java
#collection of (key, value) pairs
#keys of the dictionary has to immutable, for example list can not be used as key
abc
{'name': 'def', 'rollNo': 123, 'marks': 95.5, 'subjects': ['english', 'maths', 'science']}
{'name': 'ghi', 'rollNo': 124}
name : def
rollNo : 123
marks : 95.5
subjects : ['english', 'maths', 'science']
In [17]: #alternative way for iterating
for key, value in d1.items():
print(key, ':', value)
name : def
rollNo : 123
marks : 95.5
subjects : ['english', 'maths', 'science']
2
{'name': 'def', 'rollNo': 123} length: 2
In [ ]: #To download .ipynb notebook, right click the following link and click save as
https://ninjasfiles.s3.amazonaws.com/0000000000003218.ipynb