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

- Functions in Python -

  Function-  ● A function is a named block of code designed to perform a specific task.  ● It takes in some input (optional), processes it, and gives us some output. (optional)  ● A function once defined, can be called many times. eg: function to draw a square, function to calculate average, etc. A function has 2 parts - function definition and function call.  Have you ever used a soft drink vending machine?  You give it some input (the drink you want, money ) and it does some processing inside(checks the amount, checks availability) and gives you the output(the soft drink).  Here, you are not bothered about, how the machine works or from where it gets the soft drink. right? You just give the input and you get the required output. isn’t it?  Functions in programming also behave in a similar way. Function definition: Set of instructions that form the function.  Syntax  def  <function name>(<parameter>):     ...

- Quiz game in Python -

 Game Overview  ● Questions are stored in a file. We will have to read it from the file and store it in a dictionary.  ● 2 points for the correct answer and -1 point for the wrong answer  ● Max no. of questions asked:5  ● Questions will be shuffled once and then asked sequentially.  File Template (for the program)  Each question in the file is stored as <Question> : <Answer> Here ‘:’ acts as a separator between question and answer.  Each line read from the file will be of string data type. ‘:’ helps in separating the answer from the question.  Note: Make sure you add a ‘:’ between question and answer if adding a question manually.  Algorithm  Now that you have seen how the quiz game works, Let's have look at the algorithm for the same.  1. Read the entire file  2. From each line read, get the question and answer a. Add to dictionary  3. Print the rules of the game 4. Shuffle the questions  5. Get eac...