Python Dictionary Operations, Assignments of Computer Science

The creation, indexing, operators, and functions of dictionaries in Python. It also covers string methods such as insertion and update. examples of each operation and function. useful for students learning Python programming and specifically dictionaries.

Typology: Assignments

2021/2022

Available from 11/30/2022

adarsh-kashyap-1
adarsh-kashyap-1 🇮🇳

6 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab Assignment (DICT)
Realize the following with related to DICT in Python
1. Creation of dict.
dic = {"Fruit":"lichi","Animal":"cow"} #creation of a dictionary
2. Indexing of dict
# print(dic[0])#keyerror indexing is not possible normally
3. Dictionary operators with examples
Del
del dic["Animal"] #will delete dictionary key==Animal
in not in
print("Animal" in dic) #returns False as it has been deleted
print("Animal" not in dic)#returns True
4. Dictionary functions
len ()
max ()
Min ()
sum ()
dicty={1:4,2:5,3:7,4:8}
print(len(dicty),max(dicty),min(dicty),sum(dicty))#4 3 1 6output
Date:- 27/06/22
pf3

Partial preview of the text

Download Python Dictionary Operations and more Assignments Computer Science in PDF only on Docsity!

Lab Assignment (DICT)

Realize the following with related to DICT in Python

1. Creation of dict.

dic = {"Fruit":"lichi","Animal":"cow"} #creation of a dictionary

2. Indexing of dict

print(dic[0])#keyerror indexing is not possible normally

3. Dictionary operators with examples

  • Del del dic["Animal"] #will delete dictionary key==”Animal”
  • in not in print("Animal" in dic) #returns False as it has been deleted print("Animal" not in dic)#returns True

4. Dictionary functions

  • len ()
  • max ()
  • Min ()
  • sum () dicty={1:4,2:5,3:7,4:8}

print(len(dicty),max(dicty),min(dicty),sum(dicty))# 4 3 1 6→output

5. String methods

  • Insertion method #insertion can be done by SubscriptNotation dic = {'Fruit': 'lichi', 'friend': 'Shubham'} dic["FvtNo"] =3 #it will insert new key and value pair dic["FvtNo"] =33 #it will update existing value
  • Update method #The update() method updates the dictionary with the elements from another dictionary object dic = {'Fruit': 'lichi', 'friend': 'Shubham'} dic2 ={11:"eleven",12:"twelve"} dic.update(dic2) print(dic) #output {'Fruit': 'lichi', 'friend': 'Shubham', 'FvtNo': 33, 11: 'eleven', 12: 'twelve'}
  • Get method dic2={'Fruit': 'lichi', 'friend': 'Shubham', 'FvtNo': 3, 11: 'eleven'} print(dic2.get("Fruit"))#output=’lichi print(dic2.get("Colour","Blue")) #if key does not exist already provide 2nd arg value
  • Deletion method
  • Key, values and Item method dic2={'Fruit': 'lichi', 'friend': 'Shubham', 'FvtNo': 3, 11: 'eleven', 12: 'twelve'} del dic2["Fruit"] print(dic2.keys())#dict_keys(['friend', 'FvtNo', 11, 12]) print(dic2.values())#dict_values(['Shubham', 3, 'eleven', 'twelve']) print(dic2.items())#dict_items([('friend', 'Shubham'), ('FvtNo', 3), (11, 'eleven'), (12, 'twelve')])