You are on page 1of 18

Python:Module 3

Tuples
Dictionaries
Tuples
A Tuple is a collection which is ordered and unchangeable/
immutable

Usually initialised inside parenthesis,()


We can access tuple elements
in two ways
1).0.....n indexing
We can access tuple in ordinary indexing method

Eg:
t = (3,56,7,46,7)
This tuple has 5 elements,therefore index starts from 0 and ends
in 4
So print(t[3]) will print 46 as usual ;)
2).-ve indexing
A tuple element can be accesed using -ve indexing
Eg:t = (3,4,6,7,8,9,5)
print(t[-1]) will print 5 and print(t[-7]) will print 3 as usual in python
arrays
Dictionaries
Dictionaries are collections of data, which are unorganized and
mutable

It consists of key-value pairs

Key-value pairs are seperated using colon(:) and after each key-
value they are seperated using comma(,)

We can initialize dictionaries inside curly braces({})


Format:
d={
<key>: <value>,
<key>: <value>,
.
.
.
<key>: <value>
}
Also
We can construct dictionaries using builtin dict() function
Eg:
d = dict([
(<key>, < value>),
(<key>, < value>),
.
.
.
(<key>,< value>)
])
Accessing Dictionary Elements
We can access elements in dictionary using ‘key’ speci ed for
the value.
Eg:
D={
‘M’: ‘F’,
‘K’: ‘G’,
‘T’: ‘Y’
}
print(D[‘M’]):Will print F

fi
Adding values to dictionary
We can add values to dictionaries using the format,

Dict_name[key] = value
Updating value in dictionaries
We can update values in dictionaries using the format,
Dict_name[existing_key] = value
Deleting values in dictionaries
We can delete a value in dictionaries by accessing the key of the
value with given format
del Dict_name[key]

#Hint
del is a keyword here
Creating Empty Dictionaries
We can create empty dictionaries just by simply assigning a
dictionary with empty curly braces

Eg: P = {}
#Boom..! We just created a empty dictionary
Later we can add multiple entries to dictionary by specifying the
key.
Like P[‘F’] = ‘T’
Therefore value T is added to the dictionary with key F
One Key, but multiple values
We can add multiple values to the dictionary with inside a single
key
Taking the previous example:
P[‘S’] = [‘Dog’, ‘Cat’, ‘Bird’]
Here The above datas are added to Dictionary P with Key S
print(P[‘S’][0]) will print Dog
Here common indexing method is used.
Also we can use -ve indexing

Also we can add key-value pairs inside a key


Taking the previous example
P[‘O’] = { ‘z’ : ‘m’, ‘c’ : ‘j’}
Here inside P dictionary two key-value pairs are added under the
key O
To print the rst pair: we can use
print(P[‘O’][‘z’]).
So it will print m
Thus by accessing the outer key and inner key we can print the
key-value pairs inside a key
fi
Restrictions in Dictionary Key
• Any type of value can be used as key
• Types and functions can also be used as key
• A key should be immutable, hence we can use tuple as key
• List or another dictionary cannot be used as key because
they are mutable
Built-in Functions and Operators of Dictionary
‘in’ and ‘not in’ operator
• in operator gives true if a speci ed key is present in the dictionary else false
• not in operator gives true if a speci ed key is not present in the dictionary
else false

Built-in functions
• dict_name.clear():to clear a dictionary
• dict_name.get(key):returns the value if the key is present in the dictionary
• dict_name.items():returns a list of key value pairs
• dict_name.keys():returns a list of keys inside the dictionary
• dict_name.values():returns a list of values inside the dictionary
• dict_name.pop(key):removes the speci ed key and its pair
• dict_name.popitem():removes the last key value pair inside the dictionary
• dict_name.update(dict_name):merges with speci ed dictionary
fi
fi
fi
fi
Thank you :)

You might also like