DEV Community

loading...
Cover image for Learning Python-Basic course: Day 21, Summary of the week and dictionary exercises.

Learning Python-Basic course: Day 21, Summary of the week and dictionary exercises.

Aatmaj
I'm a Computer Science enthusiast with a keen interest in Maths. I like to tinker with things around me & build projects on my own. I like to play musical instruments & practise Sahaja Yoga meditation
・3 min read

Today we will do more questions related to dictionaries for a thorough revision. In the process, we also will learn a few methods related to dictionaries.


Summary of the week-

  • Day 18 We learnt about dictionaries. We checked out one example which covered many inbuilt functions related to dictionaries.
  • Day 19 We practiced a few questions on dictionaries and had quite a thorough practice of dictionaries in Python. We did programs to fuse two separate lists to a single dictionary, paired out even numbers and made fancy dictionaries using for loops.
  • Day 20 We learnt about hashtables and how to create them using dictionaries. We tried out simple and chained hashtables and worked out information retrieval using them.

Sample question-Concatenate two dictionaries.

We will now fuse two dictionaries to create a new one using the update() method

a={1:10, 2:20}
b={3:30, 4:40}
c={}
for i in (a,b): 
    #for every element in dictionary a AND dictionary b,
    c.update(i)
    #update the dictionary to include these values.
print(c)
Enter fullscreen mode Exit fullscreen mode

output-

{1: 10, 2: 20, 3: 30, 4: 40}
Enter fullscreen mode Exit fullscreen mode

Conflict resolution- In case both the dictionaries have a same key, in that case the last value is held true by the update() method.

a={1:10, 2:20}
b={3:30, 4:40 , 2:60}
c={}
d={}
for i in (a,b): 
    c.update(i)
print(c)
for i in (b,a): 
    d.update(i)
print(d)
Enter fullscreen mode Exit fullscreen mode
{1: 10, 2: 60, 3: 30, 4: 40}
{1: 10, 2: 20, 3: 30, 4: 40}
Enter fullscreen mode Exit fullscreen mode

Editing keys.

I know what you all must be thinking. Keys are non-mutable right?
Yes. they are. but here is a cleaver trick

a = {
  "a":1,
  "b":2,
  "c":3,
  "e":4
}
# Replace 'e' by 'd'

a["d"] = a.pop("e")
print(a)
Enter fullscreen mode Exit fullscreen mode
{'b': 2, 'a': 1, 'c': 3, 'd': 4}
Enter fullscreen mode Exit fullscreen mode

Today there are no exercises from my side..πŸ˜ƒ...But wait!πŸ˜’ I am providing a reference link for taking on a dictionary quiz to clear all your concepts!πŸ˜‹
Dictionary quiz by PYnative
The quiz contains 14 Questions. 14/14 is the target

Comment your progress below by pasting screenshot of your scores!🀠


✌️So friends that's all for now. 😊 Hope you all are having fun.😎 Please let me know in the comment section below πŸ‘‡. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🀠 Just post in the comments below or gmail me. πŸ˜‰

Please do follow me on Github and Star the Learning Python Repository which contains all the material for this course😁

Thank you allπŸ‘

For those who have not yet made account in Dev.to, you can have a free easy sign-up using your mail or GitHub accounts. I would suggest the budding developers to create your GitHub free account right away. You would require to register sooner or later anyways

πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯
Next day will begin from TuesdayπŸ“…, Monday is reserved for.... MATLAB MONDAYSπŸ’₯ Follow me for updates...

Discussion (0)