Learning Python-Basic course: Day 18, Dictionaries in Python

Learning Python (72 Part Series)

1 Launching the Learning Python course!
2 Learning Python- Basic course: Day 1, Introduction and Installation
68 more parts…
3 Learning Python- Basic course: Day 2, Statements, Comments and Indentation
4 Learning Python- Basic course: Day 3, Operators and If-elif-else
5 Learning Python- Basic course: Day 4, The for loop
6 Learning Python-Basic course: Day 5, Summary of the week and Interview questions
7 Learning Python-Basic course: Day 6, The While Loop and more questions🤓!
8 Learning Python-Basic course: Day 7, Exercises and coding challenges️
9 Learning Python-Basic course: Day 8, Unicode in Python
10 Learning Python-Basic course: Day 9, Summary of the week and exercises.
11 Learning Python-Basic course: Day 10, Lists in Python
12 Learning Python-Basic course: Day 11, Multidimensional lists and Tuples
13 Learning Python- Basic course: Day 12, Basic algorithms
14 Learning Python- Basic course: Day 13, Summary of the week and Stack implementation
15 Learning Python-Basic course: Day 15, More about try-except
16 Learning Python-Basic course: Day 14, Basic Exception and error handling using try-except
17 Learning Python-Basic course: Day 16, Fractal lists and other questions
18 Learning Python-Basic course: Day 17, Summary of the week and Insertion sort.
19 Learning Python-Basic course: Day 18, Dictionaries in Python
20 Learning Python-Basic course: Day 19, Practicing Dictionary exercises
21 Learning Python-Basic course: Day 20, HashTables via Dictionaries
22 Learning Python-Basic course: Day 21, Summary of the week and dictionary exercises.
23 Learning Python-Basic course: Day 22, String Methods Part-1
24 Learning Python-Basic course: Day 23, String Methods Part-2
25 Learning Python-Basic course: Day 24, String Methods Part-3
26 Learning Python-Basic course: Day 25, Summary of the basic course.
27 Learning Python Course- yearning for suggestions
28 Learning Python- Intermediate course: Day 1, User defined functions
29 Learning Python- Intermediate course: Day 2, returning values from methods.
30 Learning Python- Intermediate course: Day 3, Recursion in Python
31 Learning Python- Intermediate course: Day 4, Summary of the week, Guidelines for Recursion and high-level questions.
32 Learning Python- Intermediate course: Day 5, Exploring the math module
33 Learning Python- Intermediate course: Day 6, Math Exercises
34 Learning Python- Intermediate course: Day 7, Making Python modules
35 Learning Python- Intermediate course: Day 8, Summary of the week and nested Modules
36 Learning Python- Intermediate course: Day 9, Complex numbers part 1
37 Learning Python- Intermediate course: Day 10, Complex numbers part 2
38 Learning Python- Intermediate course: Day 11, Random numbers
39 Learning Python- Intermediate course: Day 12, Summary of the week
40 Learning Python- Intermediate course: Day 13, The Statistics Module
41 Learning Python- Intermediate course: Day 14, Introduction to the Decimal module
42 Learning Python- Intermediate course: Day 15, Complete guide to the fractions module
43 Learning Python- Intermediate course: Day 16, Summary of the week
44 Learning Python- Intermediate course: Day 17, Tkinter — a fast and easy way to create GUI applications.
45 Learning Python- Intermediate course: Day 18, Tkinter — Types of Widgets part 1
46 Learning Python- Intermediate course: Day 19, Tkinter — Types of Widgets part 2
47 Learning Python- Intermediate course: Day 20, Tkinter — Types of Widgets part 3
48 Learning Python- Intermediate course: Day 21, Hello world in Tkinter !
49 Learning Python- Intermediate course: Day 22, Bold or Italics !
50 Learning Python- Intermediate course: Day 23, IntVar() and Radio-buttons.
51 Learning Python- Intermediate course: Day 24, Summary of the week and Adding Colors
52 Learning Python- Intermediate course: Day 25, Buttons, Entry and Textboxes
53 Learning Python- Intermediate course: Day 26, Password Manager-Tkinter
54 Learning Python- Intermediate course: Day 27, Entry keypress event
55 Learning Python- Intermediate course: Day 28, Summary of the week
56 Learning Python- Intermediate course: Day 29, Sliders in Tkinter
57 Learning Python- Intermediate course: Day 30, Spinbox and Labelbox
58 Learning Python- Intermediate course: Day 31, Coordinate positions
59 Learning Python- Intermediate course: Day 32, The Menubutton Widget
60 Learning Python- Intermediate course: Day 33, The Menu Widget
61 Learning Python- Intermediate course: Day 34, Toplevel, Panedwindow and Message widgets
62 Learning Python- Intermediate course: Day 35, MessageBox widget
63 Learning Python- Intermediate course: Day 36, Summary of the week
64 Learning Python- Intermediate course: Day 37, File handling in Python
65 Learning Python- Intermediate course: Day 38, OOP
66 Learning Python- Intermediate course: Day 39, OOP-Constructor __init__
67 Learning Python- Intermediate course: Day 40, Summary of the week and more about OOP
68 Learning Python- Intermediate course: Day 41, Inheritance in Python
69 Learning Python- Intermediate course: Day 42, Polymorphism.
70 Learning Python- Intermediate course: Day 43, DDD and more on OOP
71 Learning Python- Intermediate course: Day 44, Summary of the week, examples and exercises
72 Learning Python Intermediate Course- yearning for suggestions

Welcome all🤟 Today we will cover dictionaries!


Dictionary is simply a collection of unordered key value pairs
Or sometimes referred as a ‘hash table’ of key value pairs. Dictionary holds key:value pair. this means that every value in an dictionary is mapped with some other value. Values in a dictionary can be of any datatype. Dictionaries cannot have two items with the same key for obvious reasons.
example

AatmajProfileDictionary={"name":"Aatmaj","Hobby":"teaching","Commits":700}

Enter fullscreen mode Exit fullscreen mode

Here is a quick difference between lists, tuples and dictionaries.

a=[] #list a=() #tuple a={}#dictionary 

Enter fullscreen mode Exit fullscreen mode

Hash table

A Hash table is a data structure. A hash table is a data structure that implements an associative array abstract data type that can map keys to values. A hash table uses a hash function to compute an index also called as the hash code, into a array of buckets or slots, from which the desired value can be found.

As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Sample program-

Here is a sample program just to get you started with dictionaries. It is quite easy to understand and commented whenever necessary

>>> hardware={    "Brand": "Dell",    "Model": 2430,    "Year":  "2020"}
>>> print(hardware) #prints the value of the dictionary {'Brand': 'Dell', 'Model': 2430, 'Year': '2020'}
>>> print(hardware["Model"])
2430
>>> print(hardware.get("Model"))
2430
>>> hardware["Year"]=2021 #Changing the value of the dictionary >>> print(hardware)
{'Brand': 'Dell', 'Model': 2430, 'Year': 2021}
>>> print(hardware.pop("Model"))
2430
>>> print(hardware)
{'Brand': 'Dell', 'Year': 2021}
>>> hardware["Model"]="Lenovo"
>>> hardware["Year"]=2019
>>> print(hardware.popitem()) #popitem returns the last value entered ('Model', 'Lenovo')
>>> print(hardware)
{'Brand': 'Dell', 'Year': 2019}
>>> for y in hardware:
...     print(y)#Corresponds to each key ...
Brand
Year
>>> for x in hardware:
...      print(hardware[x])#refers to the value ...
Dell
2019
>>> for z in hardware.values():
...      print(z)
...
Dell
2019
>>> hardware.clear() #Cleares the dictionary (not delete) >>> print(hardware)
{}
>>> print(hardware["Price"])#trying to remove element which is not present Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Price'

Enter fullscreen mode Exit fullscreen mode

Multidimensional dictionaries

Same story needs no explanation!

hardware={
    "LAPTOP":{"Brand": "Dell","Model": 2430,"Year":  "2020"},
    "DESKTOP":{"Brand":"Lenovo","Model":8877,"Warranty": 2},
    "TABLET":{"Brand":"Apple", "price":"3000$"}
}
print(hardware)
print(hardware["TABLET"])
print(hardware["LAPTOP"]["Model"]) #Note the syntax 

Enter fullscreen mode Exit fullscreen mode

{'TABLET': {'price': '3000$', 'Brand': 'Apple'}, 'LAPTOP': {'Model': 2430, 'Brand': 'Dell', 'Year': '2020'}, 'DESKTOP': {'Model': 8877, 'Brand': 'Lenovo', 'Warranty': 2}}
{'price': '3000$', 'Brand': 'Apple'}
2430

Enter fullscreen mode Exit fullscreen mode

Exercise-
1) Make a dictionary which contains a list and a tuple. Then append the tuple in the list in the dictionary.

2) Dynamic generation of dictionaries– Write a program to take names of five students and their corresponding marks, put them in an dictionary.
output-

Please enter student name peter
Please enter marks 13
Please enter student name john
Please enter marks 32
Please enter student name pappu
Please enter marks 5
Please enter student name bob
Please enter marks 7
Please enter student name mina
Please enter marks 32
{'peter': 13, 'john': 32, 'pappu': 5, 'mina': 32, 'bob': 7}

Enter fullscreen mode Exit fullscreen mode

Answers will be found here

So friends that’s all for this part. For any suggestions please ping me🤠.
Here is my Gmail- aatmaj.mhatre@gmail.com 🤟
Don’t forget to follow me on GitHub for updates on the course.

Learning Python (72 Part Series)

1 Launching the Learning Python course!
2 Learning Python- Basic course: Day 1, Introduction and Installation
68 more parts…
3 Learning Python- Basic course: Day 2, Statements, Comments and Indentation
4 Learning Python- Basic course: Day 3, Operators and If-elif-else
5 Learning Python- Basic course: Day 4, The for loop
6 Learning Python-Basic course: Day 5, Summary of the week and Interview questions
7 Learning Python-Basic course: Day 6, The While Loop and more questions🤓!
8 Learning Python-Basic course: Day 7, Exercises and coding challenges️
9 Learning Python-Basic course: Day 8, Unicode in Python
10 Learning Python-Basic course: Day 9, Summary of the week and exercises.
11 Learning Python-Basic course: Day 10, Lists in Python
12 Learning Python-Basic course: Day 11, Multidimensional lists and Tuples
13 Learning Python- Basic course: Day 12, Basic algorithms
14 Learning Python- Basic course: Day 13, Summary of the week and Stack implementation
15 Learning Python-Basic course: Day 15, More about try-except
16 Learning Python-Basic course: Day 14, Basic Exception and error handling using try-except
17 Learning Python-Basic course: Day 16, Fractal lists and other questions
18 Learning Python-Basic course: Day 17, Summary of the week and Insertion sort.
19 Learning Python-Basic course: Day 18, Dictionaries in Python
20 Learning Python-Basic course: Day 19, Practicing Dictionary exercises
21 Learning Python-Basic course: Day 20, HashTables via Dictionaries
22 Learning Python-Basic course: Day 21, Summary of the week and dictionary exercises.
23 Learning Python-Basic course: Day 22, String Methods Part-1
24 Learning Python-Basic course: Day 23, String Methods Part-2
25 Learning Python-Basic course: Day 24, String Methods Part-3
26 Learning Python-Basic course: Day 25, Summary of the basic course.
27 Learning Python Course- yearning for suggestions
28 Learning Python- Intermediate course: Day 1, User defined functions
29 Learning Python- Intermediate course: Day 2, returning values from methods.
30 Learning Python- Intermediate course: Day 3, Recursion in Python
31 Learning Python- Intermediate course: Day 4, Summary of the week, Guidelines for Recursion and high-level questions.
32 Learning Python- Intermediate course: Day 5, Exploring the math module
33 Learning Python- Intermediate course: Day 6, Math Exercises
34 Learning Python- Intermediate course: Day 7, Making Python modules
35 Learning Python- Intermediate course: Day 8, Summary of the week and nested Modules
36 Learning Python- Intermediate course: Day 9, Complex numbers part 1
37 Learning Python- Intermediate course: Day 10, Complex numbers part 2
38 Learning Python- Intermediate course: Day 11, Random numbers
39 Learning Python- Intermediate course: Day 12, Summary of the week
40 Learning Python- Intermediate course: Day 13, The Statistics Module
41 Learning Python- Intermediate course: Day 14, Introduction to the Decimal module
42 Learning Python- Intermediate course: Day 15, Complete guide to the fractions module
43 Learning Python- Intermediate course: Day 16, Summary of the week
44 Learning Python- Intermediate course: Day 17, Tkinter — a fast and easy way to create GUI applications.
45 Learning Python- Intermediate course: Day 18, Tkinter — Types of Widgets part 1
46 Learning Python- Intermediate course: Day 19, Tkinter — Types of Widgets part 2
47 Learning Python- Intermediate course: Day 20, Tkinter — Types of Widgets part 3
48 Learning Python- Intermediate course: Day 21, Hello world in Tkinter !
49 Learning Python- Intermediate course: Day 22, Bold or Italics !
50 Learning Python- Intermediate course: Day 23, IntVar() and Radio-buttons.
51 Learning Python- Intermediate course: Day 24, Summary of the week and Adding Colors
52 Learning Python- Intermediate course: Day 25, Buttons, Entry and Textboxes
53 Learning Python- Intermediate course: Day 26, Password Manager-Tkinter
54 Learning Python- Intermediate course: Day 27, Entry keypress event
55 Learning Python- Intermediate course: Day 28, Summary of the week
56 Learning Python- Intermediate course: Day 29, Sliders in Tkinter
57 Learning Python- Intermediate course: Day 30, Spinbox and Labelbox
58 Learning Python- Intermediate course: Day 31, Coordinate positions
59 Learning Python- Intermediate course: Day 32, The Menubutton Widget
60 Learning Python- Intermediate course: Day 33, The Menu Widget
61 Learning Python- Intermediate course: Day 34, Toplevel, Panedwindow and Message widgets
62 Learning Python- Intermediate course: Day 35, MessageBox widget
63 Learning Python- Intermediate course: Day 36, Summary of the week
64 Learning Python- Intermediate course: Day 37, File handling in Python
65 Learning Python- Intermediate course: Day 38, OOP
66 Learning Python- Intermediate course: Day 39, OOP-Constructor __init__
67 Learning Python- Intermediate course: Day 40, Summary of the week and more about OOP
68 Learning Python- Intermediate course: Day 41, Inheritance in Python
69 Learning Python- Intermediate course: Day 42, Polymorphism.
70 Learning Python- Intermediate course: Day 43, DDD and more on OOP
71 Learning Python- Intermediate course: Day 44, Summary of the week, examples and exercises
72 Learning Python Intermediate Course- yearning for suggestions

原文链接:Learning Python-Basic course: Day 18, Dictionaries in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容