forked from justmarkham/DAT8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
00_python_intermediate_workshop.py
239 lines (180 loc) · 7.45 KB
/
00_python_intermediate_workshop.py
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
231
232
233
234
235
236
237
238
239
'''
Python Intermediate Workshop
'''
'''
LISTS
'''
# creating
a = [1, 2, 3, 4, 5] # create lists using brackets
# slicing
a[0] # returns 1 (Python is zero indexed)
a[1:3] # returns [2, 3] (inclusive of first index but exclusive of second)
a[-1] # returns 5 (last element)
# appending
a[5] = 6 # error because you can't assign outside the existing range
a.append(6) # list method that appends 6 to the end
a = a + [0] # use plus sign to combine lists
# checking length
len(a) # returns 7
# checking type
type(a) # returns list
type(a[0]) # returns int
# sorting
sorted(a) # sorts the list
sorted(a, reverse=True) # reverse=True is an 'optional argument'
sorted(a, True) # error because optional arguments must be named
'''
STRINGS
'''
# creating
a = 'hello' # can use single or double quotes
# slicing
a[0] # returns 'h' (works like list slicing)
a[1:3] # returns 'el'
a[-1] # returns 'o'
# concatenating
a + ' there' # use plus sign to combine strings
5 + ' there' # error because they are different types
str(5) + ' there' # cast 5 to a string in order for this to work
# uppercasing
a[0] = 'H' # error because strings are immutable (can't overwrite characters)
a.upper() # string method (this method doesn't exist for lists)
# checking length
len(a) # returns 5 (number of characters)
'''
EXERCISE:
1. Create a list of the first names of your family members.
2. Print the name of the last person in the list.
3. Print the length of the name of the first person in the list.
4. Change one of the names from their real name to their nickname.
5. Append a new person to the list.
6. Change the name of the new person to lowercase using the string method 'lower'.
7. Sort the list in reverse alphabetical order.
Bonus: Sort the list by the length of the names (shortest to longest).
'''
names = ['Wesley', 'Larry', 'Wan'] # list of names
names[-1] # last element
len(names[0]) # length of first string
names[0] = 'Wes' # overwrite existing element
names.append('Gabriel') # append new element
names[-1] = names[-1].lower() # change last string to be lowercase
sorted(names, reverse=True) # sort the list in reverse order
sorted(names, key=len) # sort the list by length
'''
FOR LOOPS AND LIST COMPREHENSIONS
'''
# for loop to print 1 through 5
nums = range(1, 6) # create a list of 1 through 5
for num in nums: # num 'becomes' each list element for one loop
print num
# for loop to print 1, 3, 5
other = [1, 3, 5] # create a different list
for x in other: # name 'x' does not matter, not defined in advance
print x # this loop only executes 3 times (not 5)
# for loop to create a list of 2, 4, 6, 8, 10
doubled = [] # create empty list to store results
for num in nums: # loop through nums (will execute 5 times)
doubled.append(num*2) # append the double of the current value of num
# equivalent list comprehension
doubled = [num*2 for num in nums] # expression (num*2) goes first, brackets
# indicate we are storing results in a list
'''
EXERCISE 1:
Given that: letters = ['a', 'b', 'c']
Write a list comprehension that returns: ['A', 'B', 'C']
EXERCISE 2 (BONUS):
Given that: word = 'abc'
Write a list comprehension that returns: ['A', 'B', 'C']
EXERCISE 3 (BONUS):
Given that: fruits = ['Apple', 'Banana', 'Cherry']
Write a list comprehension that returns: ['A', 'B', 'C']
'''
letters = ['a', 'b', 'c']
[letter.upper() for letter in letters] # iterate through a list of strings,
# and each string has an 'upper' method
word = 'abc'
[letter.upper() for letter in word] # iterate through each character
fruits = ['Apple', 'Banana', 'Cherry']
[fruit[0] for fruit in fruits] # slice the first character from each string
'''
DICTIONARIES
'''
# dictionaries are made of key-value pairs (like a real dictionary)
family = {'dad':'Homer', 'mom':'Marge', 'size':2}
# check the length
len(family) # returns 3 (number of key-value pairs)
# use the key to look up a value (fast operation regardless of dictionary size)
family['dad'] # returns 'Homer'
# can't use a value to look up a key
family['Homer'] # error
# dictionaries are unordered
family[0] # error
# add a new entry
family['cat'] = 'snowball'
# keys must be unique, so this edits an existing entry
family['cat'] = 'snowball ii'
# delete an entry
del family['cat']
# keys can be strings or numbers or tuples, values can be any type
family['kids'] = ['bart', 'lisa'] # value can be a list
# accessing a list element within a dictionary
family['kids'][0] # returns 'bart'
# useful methods
family.keys() # returns list: ['dad', 'kids', 'mom', 'size']
family.values() # returns list: ['Homer', ['bart', 'lisa'], 'Marge', 2]
family.items() # returns list of tuples:
# [('dad', 'Homer'), ('kids', ['bart', 'lisa']), ('mom', 'Marge'), ('size', 2)]
'''
EXERCISE:
1. Print the name of the mom.
2. Change the size to 5.
3. Add 'Maggie' to the list of kids.
4. Fix 'bart' and 'lisa' so that the first letter is capitalized.
Bonus: Do this last step using a list comprehension.
'''
family['mom'] # returns 'Marge'
family['size'] = 5 # replaces existing value for 'size'
family['kids'].append('Maggie') # access a list, then append 'Maggie' to it
family['kids'][0] = 'Bart' # capitalize names by overwriting them
family['kids'][1] = 'Lisa'
# or, capitalize using a list comprehension and the 'capitalize' string method
family['kids'] = [kid.capitalize() for kid in family['kids']]
# or, slice the string, uppercase the first letter, and concatenate with other letters
family['kids'] = [kid[0].upper() + kid[1:] for kid in family['kids']]
'''
REQUESTS
'''
# import module (make its functions available)
import requests
# use requests to talk to the web
r = requests.get('http://www.google.com')
type(r) # special 'response' object
r.text # HTML of web page stored as string
type(r.text) # string is encoded as unicode
r.text[0] # string can be sliced like any string
'''
APIs
What is an API?
- Application Programming Interface
- Structured way to expose specific functionality and data access to users
- Web APIs usually follow the "REST" standard
How to interact with a REST API:
- Make a "request" to a specific URL (an "endpoint"), and get the data back in a "response"
- Most relevant request method for us is GET (other methods: POST, PUT, DELETE)
- Response is often JSON format
- Web console is sometimes available (allows you to explore an API)
API Providers: https://apigee.com/providers
Echo Nest API Console: https://apigee.com/console/echonest
API key: http://bit.ly/myechonest
'''
# request data from the Echo Nest API
r = requests.get('http://developer.echonest.com/api/v4/artist/top_hottt?api_key=KBGUPZPJZS9PHWNIN&format=json')
r.text # looks like a dictionary
type(r.text) # actually stored as a string
r.json() # decodes JSON
type(r.json()) # JSON can be represented as a dictionary
top = r.json() # store that dictionary
# store the artist data
artists = top['response']['artists'] # list of 15 dictionaries
# create a list of artist names only
names = [artist['name'] for artist in artists] # can iterate through list to access dictionaries