Repository navigation
Expand file tree
/
Copy pathModule09_Lecture_Code.py
More file actions
230 lines (162 loc) · 6.07 KB
/
Copy pathModule09_Lecture_Code.py
File metadata and controls
230 lines (162 loc) · 6.07 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 28 11:20:35 2022
Module 9 - Dictionaries
@author: iburr
"""
morse_code = {'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
'Z': '--..', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.', '0': '-----', ', ': '--..--',
'.': '.-.-.-', '?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-'}
#we will use this import to write dictionaries to files
import json
'''
#Here is the gradebook expressed concisely as a dictionary
#We use a list to pair each grade with the key (which is each student's name)
grade_book = {'Gina': [100, 90, 10, [90, 80, 100 ]], 'Tina': [45, 50, 48], 'Rob': [78, 84, 82], 'Jake': {'Sub-grades': [0, 1, 2]}, 10: 'Mustang'}
#FINDING A VALUE IN A DICTIONARY
student_name = input("Enter the students name to see their respective grades: ")
if student_name in grade_book:
#print(grade_book[key])
print(*grade_book[student_name]) # The ord character * will remove the brackets from the list.
#works like ELSE, but actually searches through the dictionary
if student_name not in grade_book:
print("The student does not exist in the grade book.")
#CHANGING A VALUE IN GRADEBOOK
#Since we have mutliple values, we still need to find the index of the list
#associated with each student
if student_name in grade_book:
grade_change = int(input("Which grade do you want to change? "))
grade_change = grade_change - 1
new_value = int(input("Enter the new grade: "))
for key in grade_book:
#remember, it follows key:value pairs, so we reference the key then the value as grade_change
#grade_book['Tina'][1]
grade_book[student_name][grade_change] = new_value
print(grade_book)
mylist = [1,2,3]
mystring = str(mylist)
print(mystring)
#WRITING THE GRADE_BOOK TO A TEXT FILE USING JSON, FOR LOOPS, AND STRING CONVERSION
with open(r'NewGradeBooks.txt', 'w') as file:
#USING A FOR LOOP TO ITERATE OVER THE DICTIONARY
for key, value in grade_book.items():
file.write('%s:%s\n' % (key, value))
file.write(json.dumps(grade_book))
#without json and dumps
#file.write(str(grade_book))
file.close()
#----------------------------------------------------------------------------------------------------
phonebook = {'Karl': '555-9832','Tim': '754-3220', 'Nelly': '954-8774', 'Todd': 12345, 'Karl_1': '555-9830'}
print(phonebook)
#ADDING AN ELEMENT TO THE DICTIONARY
#dictionary[key]
phonebook['Karl'] = '954-8998'
phonebook['Terrence'] = '305-5565'
#notice how it replaced the value associated with Karl but adds Terrence
print(phonebook)
#DELETING A KEY:VALUE PAIR
del phonebook['Terrence']
print(*phonebook)
print(phonebook['Karl'])
#GETTING THE NUMBER OF ITEMS IN A DICTIONARY USING LEN
length = len(phonebook)
print(length)
#CREATING AN EMPTY DICTIONARY
empty_dictionary = {}
empty_dictionary['Car'] = 'Ford Mustang'
print(empty_dictionary)
#USING A FOR LOOP TO ITERATE OVER THE DICTIONARY
for key in phonebook:
print(key, phonebook[key])
#USING THE CLEAR() METHOD
phonebook.clear()
print(phonebook)
#USING THE GET() METHOD. This takes the key and a default statement if it's not found
number1 = phonebook.get('Phil', 'number not found')
print(number1)
phonebook = {'Johnny': '999-9999'}
number2 = phonebook.get('Johnny')
print(number2)
#PRINTING OUT ALL OF THE ITEM IN A DICTIONARY
print(phonebook.items())
new_phonebook = {'John': '754-8765', 'Turqouise': '555-5555'}
for name in new_phonebook.items():
print(name, value)
print(new_phonebook.keys())
#USING THE POP METHOD, which, which removes a single key:value pair
new_phonebook.pop('Turqouise', 'Number not found')
print(new_phonebook)
#POPITEM METHOD. IN 3.7 + of Python popitem() removes last item added to dict
grade_book.popitem()
print(grade_book)
#USING THE VALUES METHOD()
for val in grade_book.values():
print(val)
#DICTIONARY COMPREHENSIONS
#in this line of code we will create a nnumber and its corresponding square using
#one line of code for the squares dictionary
numbers = [1,2,3,4]
#item:item**2 == result expression. for item in numbers == iteration expression
squares = {item:item**2 for item in numbers}
print(squares)
'''
#LOOK AT PAGE 492, Using if Clauses
'''
#-------------------------------------------------------------------------------
#SETS
mySet = set()
mySet = 'abc'
print(mySet)
myListSet = set([3, 2, 4, 4])
print(myListSet)
length = len(myListSet)
print(length)
myListSet.add('b')
myListSet.add('a')
myListSet.add('a')
myListSet.add(3.4)
myListSet.add(5.5)
length2 = len(myListSet)
print(myListSet)
print(length2)
myListSet.update([9, 6, 10, 'b'], [0, 10])
print(myListSet)
my_dictionary = {3: 5, 'Tim': 6, 'Sam': 10}
new_set = set(my_dictionary)
print(new_set)
new_set.update({"Bill": "Ted"})
print(new_set)
myListSet.remove(10) #also .discard()
print(myListSet)
a_new_set = set('a')
print(f"----------This is an empty set: {a_new_set}-------------")
myListSet.clear() #removes everything
a_new_set.clear()
print(f"----------This is an empty set: {a_new_set}-------------")
#for loops, in operator, not in operator
#YOU WILL DO THIS
#Union of sets - the real reason to use them
set1 = set([1,5,19,8])
set2 = set([7,6,19,4,1,11])
set3 = set1.union(set2)
print(set3) # can use the or operation | as well set3 = set1 | set2
#intersection of sets
set4 = set1.intersection(set2)
print(set4) # like above but use the & operator, set3 = set1 & set2
#difference of sets
set5 = set1.difference(set2)
print(set5)
set5a = set2.difference(set1)
print(set5a)
set6 = set2.symmetric_difference(set1)
print(set6)
result = sorted(set6)
print(result)
'''