-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIP_PYTHON_PROGRAMMING_CLASS_NOTES_8
More file actions
138 lines (110 loc) · 4.49 KB
/
IP_PYTHON_PROGRAMMING_CLASS_NOTES_8
File metadata and controls
138 lines (110 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
INTERNATIONAL PASSPORT: PYTHON PROGRAMMING
CLASSNOTES & HOMEWORK ASSIGNMENT:
CLASS - June 18, 2023 - SUNDAY - CLASS 8 @4:45PM EST
DICTIONARIES
⁃ Dictionaries are special structure in python which allows you to store information in what are called key value pairs.
⁃ The user creates key value pairs and when he wants to access of information inside the dictionary, he can refer to it by its key.
⁃ When playing with dictionaries you alway want to use curly brackets.
DICTIONARIES CONTINUED
⁃ Dictionaries are like lists. They both share the following properties:
⁃ Both can die used to store values
⁃ Both can be changed in place and can grow and shrink on demand
⁃ The main difference between lists and dictionaries is how they are accessed
# LAB 1 - WRITING OUR FIRST DICTIONARY AND CALLING FROM IT
NBA_MOST_VALUABLE_PLAYER_AWARD = {
2013: "Lebron James",
2014: "Kawhi Leonard",
2015: "Andre Iguodala",
2016: "Lebron James",
2017: "Kevin Durant",
2018: "Kevin Durant",
2019: "Kawhi Leonard",
2020: "Lebron James",
2021: "Giannis Antetokoumpo",
2022: "Stephen Curry",
2023: "Nikola Jokic"
}
print(NBA_MOST_VALUABLE_PLAYER_AWARD[2022])
# LAB 2 - ANOTHER EXAMPLE OF A DICTIONARY
Movie = {
"Title": "Good Fellas",
"Director": "Martin Scoresese",
"Year": 1990,
"Rating": 9.5
}
print(Movie["Title"])
# LAB 3 - UPDATING THE MOVIE VALUE SCORE
Movie = {
"Title": "Good Fellas",
"Director": "Martin Scoresese",
"Year": 1990,
"Rating": 9.5
}
Movie["Rating"] = (Movie["Rating"] + 2)
print(Movie["Rating"])
# LAB 4 - STORE A LIST INSIDE A DICTIONARY AND STORE A DICTIONARY WITHIN THAT DICTIONARY
Movie = {}
Movie["Title"] = "City of God"
Movie["Director"] = "Fernandy Meirelles"
Movie["Year"] = 2002
Movie["Rating"] = 8.2
Movie["Actors"] = ["Alexandre Rodrigues", "Alice Braga", "Douglas Silva"]
Movie["More_Facts"] = {
"MovieLength": 180,
"Language": "English"
}
print(Movie)
DICTIONARY METHODS / FUNCTIONS
⁃ All Pythons types, including dictionaries, have their own methods. Since dictionaries include keys and values, it’s common to access them using dictionary methods.
⁃ A method is a function that belongs to an object. For example, lists objects have methods called append, insert, remove, sort, and so on.
⁃ Methods differ from functions because with functions they re not linked to anything and are independent blocks of code that can be called from anywhere. But with methods these will belong to a specific object. For example, you can’t use list methods in dictionaries.
DICTIONARY METHODS / FUNCTIONS
⁃ get() - This method will return the value of the specific key
⁃ values() - This method will return a list of all the values in the dictionary
⁃ keys() - Used to retrieve all of the keys from the dictionary.
⁃ The keys must be of an immutable type (string, number, or tuple with immutable elements) and must be unique.
⁃ copy() - Is a method that is used on objects to create copies of them.
⁃ It returns a shallow copy of the list.
# LAB 5 - IN BUILT DICTIONARY FUNCTION USING GET()
NBA_MOST_VALUABLE_PLAYER_AWARD = {
2019: "Kawhi Leonard",
2020: "Lebron James",
2021: "Giannis Antetokoumpo",
2022: "Stephen Curry",
2023: "Nikola Jokic"
}
print(NBA_MOST_VALUABLE_PLAYER_AWARD.get(2023))
# LAB 6 - IN BUILT DICTIONARY FUNCTION USING VALUES()
NBA_MOST_VALUABLE_PLAYER_AWARD = {
2019: "Kawhi Leonard",
2020: "Lebron James",
2021: "Giannis Antetokoumpo",
2022: "Stephen Curry",
2023: "Nikola Jokic"
}
print(NBA_MOST_VALUABLE_PLAYER_AWARD.values())
# LAB 7 - IN BUILT DICTIONARY FUNCTION USING KEYS()
NBA_MOST_VALUABLE_PLAYER_AWARD = {
2019: "Kawhi Leonard",
2020: "Lebron James",
2021: "Giannis Antetokoumpo",
2022: "Stephen Curry",
2023: "Nikola Jokic"
}
print(NBA_MOST_VALUABLE_PLAYER_AWARD.keys())
# LAB 8 - IN BUILT DICTIONARY FUNCTION USING COPY()
NBA_MOST_VALUABLE_PLAYER_AWARD = {
2019: "Kawhi Leonard",
2020: "Lebron James",
2021: "Giannis Antetokoumpo",
2022: "Stephen Curry",
2023: "Nikola Jokic"
}
print(NBA_MOST_VALUABLE_PLAYER_AWARD.copy())
________________________________________________________________________
HOMEWORK:
⁃ Add to your python journal documenting you work.
⁃ Write 2 different examples of each lab dictionaries
⁃ You must send International Passport (IP) all your homework by Saturday 11:59 PM EST
⁃ Send to my email: matthewstravels85@gmail.com
________________________________________________________________________