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

- Animating characters using pygame -

If you see closely you can see that the mickey mouse animation is made out of a lot of shapes.  But to create something like this we need to make a very complex code. The other way around it is to use an image, other than the basic shapes, pygame also can show images.  Using images :  Steps to use images in my game: > find the image you want to use  > move it to the directory where you have created your code  > import/load image  To load the image we will first create an object and then use the image. load method to add the image to the object.  here, we have created an object called an image.  We then use the "pygame.image.load" to find the file in the directory ocean.png has to be replaced with the filename of your file.  To place the object on a particular point of the screen we use the blit function.  this function means that we are going to add the image onto the screen at position 0,0 mickey mouse is winking  Usi...

- 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...

-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...