Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python


Chapter Five: Python Dictionaries


Download 1.25 Mb.
Pdf ko'rish
bet22/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   ...   18   19   20   21   22   23   24   25   ...   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

Chapter Five: Python Dictionaries
This chapter will walk you through the concept of Python dictionaries, which
are the most important part of Python coding. You can store information in a
dictionary in the form of pairs. You can easily access the information, modify
it, and delete it at will. Dictionaries are amazing in the sense that they allow
you to store unlimited information. Just like lists, I will explain how you can
pair up a dictionary with a loop.
When you have a good grasp of Python dictionaries, you will learn how to
model an object with a dictionary's help. Creating a dictionary is simple, but
updating it and using it in a code can be tricky. I will move through this
chapter step by step. In the first code sample, I will create a simple
dictionary.
officeitems = {'printer' : 'HP', 'paper': 'A4', 'drafting pads': 'blank', 'scanner':
'hybrid', 'table': 'wood', 'table lights': 'LED'}
print(officeitems)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
{'printer': 'HP', 'paper': 'A4', 'drafting pads': 'blank', 'scanner': 'hybrid', 'table':
'wood', 'table lights': 'LED'}
>>>
There is another way to access and display selected information from a
dictionary. You can use one of the pairs' values and use them to access the
other value of the pair. See the following code example.
officeitems = {'printer' : 'HP', 'paper': 'A4', 'drafting pads': 'blank', 'scanner':
'hybrid', 'table': 'wood', 'table lights': 'LED'}
print(officeitems['printer'])
print(officeitems['paper'])
print(officeitems['table lights'])
print(officeitems['table'])
print(officeitems['drafting pads'])


= RESTART: C:/Users/saifia computers/Desktop/sample.py
HP
A4
LED
wood
blank
>>>
Dictionaries are more complex than lists, therefore you need more
programming practice to handle them. You can see that a dictionary contains
key-value pairs where each key is automatically connected to its value. Each
key's value can be a string or an integer or even a list in some cases. It also
can be a dictionary in a more complex code form. You have to wrap up a
dictionary in curly braces or the dictionary will display an error. A key has
directed association with its value.
Accessing values from a dictionary is easy. As you have seen in the above
code sample, I tried to access each value with the help of a key or a
dictionary. Dictionaries are also very dynamic, and they allow you to add as
many key-value pairs to the dictionary as you desire. I will now take an
empty dictionary and fill it up with key-value pairs of my choice.
officeitems = {}
officeitems['printer'] = 'HP'
officeitems['paper'] = 'A4'
officeitems['drafting pads'] = 'blank'
officeitems['scanner'] = 'hybrid'
officeitems['table lights'] = 'LED'
print(officeitems)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
{'printer': 'HP', 'paper': 'A4', 'drafting pads': 'blank', 'scanner': 'hybrid', 'table
lights': 'LED'}


>>>
You also can modify the value of a key as you deem fit. In order to do that,
you have to mention the name of the dictionary and write the key in square
brackets. Then you have to write the new value for the same key.
officeitems = {'printer' : 'HP', 'paper': 'A4', 'drafting pads': 'blank', 'scanner':
'hybrid', 'table': 'wood', 'table lights': 'LED'}
print("I have purchased a printer by " + officeitems['printer'] + ".")
officeitems['printer'] = 'dell'
print("However, I have also purchased one more now by " +
officeitems['printer'] + ".")
= RESTART: C:/Users/saifia computers/Desktop/sample.py
I have purchased a printer by HP.
However, I have also purchased one more now by dell.
>>>

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   18   19   20   21   22   23   24   25   ...   48




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling