4

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

 2 years ago
source link: https://dev.to/aatmaj/learning-python-basic-course-day-21-summary-of-the-week-and-dictionary-exercises-391e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 modeExit fullscreen mode

output-

{1: 10, 2: 20, 3: 30, 4: 40}
Enter fullscreen modeExit 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 modeExit fullscreen mode
{1: 10, 2: 60, 3: 30, 4: 40}
{1: 10, 2: 20, 3: 30, 4: 40}
Enter fullscreen modeExit 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 modeExit fullscreen mode
{'b': 2, 'a': 1, 'c': 3, 'd': 4}
Enter fullscreen modeExit 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...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK