Skip to main content

- Dictionary in python-

 Dictionary

A dictionary is a collection in which each element is a key-value pair.

 ex:  myGameDict = { “chess ”: “An indoor game for two players played on a checkerboard”, “cricket”: ”A game played on a large field with bats, ball, and wickets”, “football”: ”An outdoor game in which the ball is advanced bypassing“ } 

Elements are enclosed with { }. 

Each: pair is separated by commas(,). and are separated by ‘: ’.

Suppose you are reading a book or watching your favorite TV show and you come across a new word. You don’t know its meaning; what do you do in that case? Have you tried using a dictionary? How does it help us? In a dictionary, words are mapped to their meaning. You can search for a word and find its meaning.

 Note: Here we are referring to online dictionaries.  https://kids.britannica.com/kids/browse/dictionary   find how the dictionary describes the following games:- 

1. chess

 2. cricket 

3. football 

4. tennis 

5. basketball 

Now, today we shall develop a dictionary of our own

 In the above example, the words - chess, cricket, football are the keys and their descriptions are the values. 

Few more facts in the dictionary: 

● Duplicate keys are not allowed 

● Values are accessed using key eg: myGameDist[“chess”] refers to “An indoor game for two players played on a checkerboard”

 ● To insert a new element to the dictionary, myGameDist[“Tennis”] =”An outdoor game played using racket” The above statement inserts a new key-value pair to the above dictionary.

 ● An assignment statement like the above inserts a new element only if that key is not present in the dictionary already. If that key is present, it just reassigns the value. So if we give, myGameDist[“Cricket”] =” An outdoor played among 2 teams have 11 players each” The description/value associated with cricket changes.No new element will be inserted. 

● The keys of a dictionary once created cannot be changed. So we usually go for immutable types as keys.  

● The values can be mutable /immutable. So the value can be int, string, list,  or even another dictionary.

Create a game dictionary.



 Get the user input name of a game and print its description.







 Note:  ‘keys()’ and ‘in’ are used to check if a key is present in the dictionary. 

keys() will return you a list of dictionary keys.

 myGameDict.keys() will return [“Cricket” ,” Football” ,” Basketball” ,” Chess”] 







 Create a new entry for Volleyball in the dictionary. 



Change the description of cricket in the above dictionary. 



The new academic year is starting and you as the store manager are told to take care of accounts of textbook purchases. As The new python programmers correct you decide to maintain a dictionary of textbooks and its cost. Create the dictionary.










 Later you found that the,cost of the physics book was entered wrong. The correct price is 200. Make this change. 






Add the cost of 2 more books to the dictionary.


 





Print cost of the book entered by the user. 






Print all the books and their cost. 



Comments

Popular posts from this blog

- Object Oriented Programming -

In OOP, we can represent an entity using a class.  A class bundles data (features/properties of entity - color, height, width) and functionality (movement, expression) together.  An instance of a class is known as an object.  Which is the only cartoon series that had predicted that Trump would be elected as the president of the USA and also predicted the coronavirus spread?   Answer: Simpsons.  Simpsons is a very popular animated show. You can build something similar with python.  But have you ever wondered, how Simpsons look the same in all the series? Only his movement looking and, expression change but his main features like color, height, and face look exactly the same all the time.  This is because we create a ‘blueprint’ of Simpsons and whenever we have to simulate him on screen we use this blueprint. This keeps a record of all the features of Simpsons and recreates it whenever we want.  This is where Object-Oriented Programming(OOP) h...

– Sequences, Algorithms and Programs in Python ---

 Before moving forward, Let's learn how to install Python software in your System which is very important for the further understanding of these topics. Imagine, you are a regular customer of Uncle Sam’s Pizza restaurant, can you help Uncle Sam prepare the menu and take a printout of it? Which application will you use for that? You will type the menu using MS word and then take a printout of it. right?  So, you know that we can use MS Word to create documents. Similarly, if you want to write a python program, you need an IDE (Integrated development environment). This IDE helps you to write a python program and then run it so that you can see the result/output of the program.  Steps to install python/IDE in your local system. (Windows /MAC )   Note : Prerequisite for installing python in Windows- Windows 8 or above. go to this website .   1. Click on download python as per your OS. Mac users may download the Mac version. Windows users download depending on 32-bit...

-List Slicing in Python-

 List Slicing : Slicing helps in creating a new list from an existing list.  Syntax list_name [start :end:step] Suppose you have a list of Avengers, arranged in the order of your preference.   avengers=[“Iron Man”,”Thor”,”Hulk”,”Spider Man”,”Captain America”,”Black Panther”,”Hawk Eye”,”Vision”,”Black Widow”] Later you decide to split them into a separate list like  favourite=[“Iron Man”,”Thor”,”Hulk”]                                                   less_favourite =[”Spider Man”,”Captain America”,”Black Panther”]                             least_favourite=[”Hawk Eye”,”Vision”,”Black Widow”]  Now, we have 3 lists formed from the main list. Would you want to create these lists again from the beginning? This is where list slicing helps us. Here is the code to  G...