Professional Documents
Culture Documents
Tuples
Dictionaries
Tuples
A Tuple is a collection which is ordered and unchangeable/
immutable
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
Key-value pairs are seperated using colon(:) and after each key-
value they are seperated using comma(,)
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
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 :)