Dictionaries in Python
Dictionaries-
A dictionary is a group of elements arranged in the form of key-value pairs. In the dictionary the first element is considered as key and the next element is considered as value. The key and value are separated by colon(:),
Syntax:
dict1={key1:value1,key2:value2,key3:value3,…..}
Ex:-
dict1={“bname”:”c”,”aname”:”dennisritche”,”pyear”:1976}
Here dict1 is name of the dictionary, bname,aname,pyear are key elements and “c”,”dennisritche”, 1976 are values to the key elements respectively.
Operations on dictionaries:-
>>> dict1={"bname":"C","aname":"Dennis","pyear":1976}
>>> dict1
{'bname': 'C', 'aname': 'Dennis', 'pyear': 1976}
>>> dict1["bname"]
'C'
>>> dict1["aname"]
'Dennis'
len(dictionaryname)- returns the number of elements in the dictionary.
>>> n=len(dict1)
>>> n
3
copy()-copy the contents of one dictionary into another.
>>> dict2=dict1.copy()
>>> dict2
{'bname': 'C', 'aname': 'Dennis', 'pyear': 1976}
items()-display the items in the dictionary
>>> dict1.items()
dict_items([('bname', 'C'), ('aname', 'Dennis'), ('pyear', 1976)])
keys()-display the keys in the dictionary
>>> dict1.keys()
dict_keys(['bname', 'aname', 'pyear'])
values()-display the elements assigned for the keys in the dictionary
>>> dict1.values()
dict_values(['C', 'Dennis', 1976])
update({key:value})- add the item to the dictionary
>>> dict1.update({"cname":"KBN"})
>>> dict1
{'bname': 'C', 'aname': 'Dennis', 'pyear': 1976, 'cname': 'KBN'}
pop(itemname)-remove the item from the dictionary.
>>> dict1.pop('aname')
'Dennis'
For better understanding watch the video
University coursework often involves detailed research, critical analysis, and proper academic referencing. Students studying in different universities may also need to follow specific formatting and submission guidelines. Exploring coursework help uk can provide useful learning resources and guidance for handling challenging academic topics. Well-organized examples and clear explanations make it easier to understand assignment requirements, develop stronger arguments, and improve overall academic writing skills while managing tight submission deadlines.
ReplyDelete