Professional Documents
Culture Documents
Module3 Minor
Module3 Minor
What is the use of data structures ?What are different data structures
used in Python?
• Data structures are used for the efficient organization of data in
memory so that the data can be accessed efficiently.
• Different data structures are List, Tuple, Sets, Dictionary
• These are used to represent collection of data
1. List
What are lists? What are the properties?
• Lists are used to store a collection of data, in a particular order
• List is created/ defined as follows
L1= [ 23, 15, 34.88, “acv”, 34]
• Here, L1 is a list consists of data items/ elements of different types
• 23,15 and 34 are integers, 34.88 is floating point, “acv” is string literal
• One important property of lists is that they are mutable . It means
that we can easily modify the contents in a list
• L1[2]= 10 will replace 34.88 by 10
What are list operations?
1. To access elements in a list, use indexing and subscripting
• L1=[2, 4, 13, 17.8, 99, -56]
• To access the item 17.8, use the position(index). Indexing starts from
0, so position of 17.8 is 3
>>>print( L1[3])
• Ans: 17.8
• A special type of indexing is negative indexing. It is used to access
elements from the end of list.
L1=[2, 4, 13, 17.8, 99, -56]
L1[0:3] returns elements 2,4 and 13. Here though last index value
is 3, the corresponding value is omitted from the result.
Newlist=[]
For element in V: - Take every element in list
if ‘b’ in element: if b is present in it
newlist.append( element) put that vehicle in newlist
comprehension
z = x. difference(y)
print(z)
o/p: { ‘cherry’, ‘banana’}
Those elements present in x but not in y
4. Dictionary
• Lists and tuple organizes elements by their position
• This is useful when we want to locate elements in order- 1st, 2nd,3rd,…
• In some situations, position of data is not significant, but data is
associated with some other element.
• Eg: ( Arun: 15, Gayathri: 16, Govind: 19)
( ‘Name’:’Arun’, ‘Roll no’: 15, ‘Marks’: 50)
-Related information
- Association between keys and values
• Dictionary is a data structure that stores data as ( key, value) pairs
• These pairs are called entries
• Entries are enclosed in {}, each key and its value is separated by
colon(:)