0% found this document useful (0 votes)
18 views

'Name' 'Bob Marley' 'Age' 'Height' "5.6 FT" 'Hobby' 'Music': Print Type

This document discusses dictionaries in Python. It shows how to create empty dictionaries, add key-value pairs, access values using keys, update dictionaries by adding or removing keys and values, and get lists of keys and values. Examples include creating dictionaries to store bio data, credentials, and student data with names and ages mapped to integer keys. Functions demonstrated include print(), dict(), list(), get(), update(), del, len(), and keys().

Uploaded by

Biplab Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

'Name' 'Bob Marley' 'Age' 'Height' "5.6 FT" 'Hobby' 'Music': Print Type

This document discusses dictionaries in Python. It shows how to create empty dictionaries, add key-value pairs, access values using keys, update dictionaries by adding or removing keys and values, and get lists of keys and values. Examples include creating dictionaries to store bio data, credentials, and student data with names and ages mapped to integer keys. Functions demonstrated include print(), dict(), list(), get(), update(), del, len(), and keys().

Uploaded by

Biplab Parida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1/4/2019 Dictionaries (1)

In [1]: empty_dictionary = {}
print(type(empty_dictionary))

<class 'dict'>

In [2]: bio_data = {'Name': 'Bob Marley', 'Age':35, 'Height':"5.6 ft", 'Hobby': 'Music'}
print(bio_data)

{'Name': 'Bob Marley', 'Age': 35, 'Height': '5.6 ft', 'Hobby': 'Music'}

In [3]: credentials = { 'UserA' : 'wkliopnc' , 'UserB': 98760 , 'UserC' :98760 }

In [4]: hobby = bio_data['Hobby']


print(hobby)

Music

In [5]: age = bio_data['Age']


print(age)

35

In [6]: age = bio_data.get('Age')


print(age)

35

In [7]: profession = bio_data.get('Profession','NA')


print(profession)

NA

In [8]: bio_data['Age'] = 36
print(bio_data)

{'Name': 'Bob Marley', 'Age': 36, 'Height': '5.6 ft', 'Hobby': 'Music'}

In [9]: #add a key, val


bio_data['Profession'] = 'Singer'
print(bio_data)

{'Name': 'Bob Marley', 'Age': 36, 'Height': '5.6 ft', 'Hobby': 'Music', 'Profe
ssion': 'Singer'}

In [10]: print('Profession' in bio_data)

True

http://localhost:8888/notebooks/Downloads/Dictionaries%20(1).ipynb 1/3
1/4/2019 Dictionaries (1)

In [11]: #get list of keys


print(list(bio_data.keys()))

#get list of values


print(list(bio_data.values()))

['Name', 'Age', 'Height', 'Hobby', 'Profession']


['Bob Marley', 36, '5.6 ft', 'Music', 'Singer']

In [12]: new_dictionary = dict(Country='Jamaica', Songs=['One Love','Misty Morning'])

In [13]: bio_data.update(new_dictionary)
print(bio_data)

{'Name': 'Bob Marley', 'Age': 36, 'Height': '5.6 ft', 'Hobby': 'Music', 'Profe
ssion': 'Singer', 'Country': 'Jamaica', 'Songs': ['One Love', 'Misty Mornin
g']}

In [14]: del bio_data['Songs']


print(bio_data)

{'Name': 'Bob Marley', 'Age': 36, 'Height': '5.6 ft', 'Hobby': 'Music', 'Profe
ssion': 'Singer', 'Country': 'Jamaica'}

 In [15]: students_data = { 1:['Shivam Bansal', 24] , 2:['Udit Bansal',25], 3:['Sonam Gupta'

print(students_data)

{1: ['Shivam Bansal', 24], 2: ['Udit Bansal', 25], 3: ['Sonam Gupta', 26], 4:
['Saif Ansari', 24], 5: ['Huzefa Calcuttawala', 27]}

In [16]: print(len(students_data))

In [17]: #see all the details of students.


print(list(students_data.values()))

[['Shivam Bansal', 24], ['Udit Bansal', 25], ['Sonam Gupta', 26], ['Saif Ansar
i', 24], ['Huzefa Calcuttawala', 27]]

http://localhost:8888/notebooks/Downloads/Dictionaries%20(1).ipynb 2/3
1/4/2019 Dictionaries (1)

In [18]: students_data[6] = ['Manasi Sharma', 22]


print(students_data)

{1: ['Shivam Bansal', 24], 2: ['Udit Bansal', 25], 3: ['Sonam Gupta', 26], 4:
['Saif Ansari', 24], 5: ['Huzefa Calcuttawala', 27], 6: ['Manasi Sharma', 22]}

In [19]: del students_data[2]


print(students_data)

{1: ['Shivam Bansal', 24], 3: ['Sonam Gupta', 26], 4: ['Saif Ansari', 24], 5:
['Huzefa Calcuttawala', 27], 6: ['Manasi Sharma', 22]}

In [20]: print(list(students_data.keys()))

[1, 3, 4, 5, 6]

http://localhost:8888/notebooks/Downloads/Dictionaries%20(1).ipynb 3/3

You might also like