generated from gonzalezulises/Curso-de-Ciencia-de-Datos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1bdd355
Showing
159 changed files
with
312,408 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.ipynb_checkpoints/ | ||
.DS_Store | ||
*.pyc | ||
extras/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
''' | ||
Python Beginner Workshop | ||
''' | ||
|
||
''' | ||
Multi-line comments go between 3 quotation marks. | ||
You can use single or double quotes. | ||
''' | ||
|
||
# One-line comments are preceded by the pound symbol | ||
|
||
|
||
# BASIC DATA TYPES | ||
|
||
x = 5 # creates an object | ||
print type(x) # check the type: int (not declared explicitly) | ||
type(x) # automatically prints | ||
type(5) # assigning it to a variable is not required | ||
|
||
type(5.0) # float | ||
type('five') # str | ||
type(True) # bool | ||
|
||
|
||
# LISTS | ||
|
||
nums = [5, 5.0, 'five'] # multiple data types | ||
nums # print the list | ||
type(nums) # check the type: list | ||
len(nums) # check the length: 3 | ||
nums[0] # print first element | ||
nums[0] = 6 # replace a list element | ||
|
||
nums.append(7) # list 'method' that modifies the list | ||
help(nums.append) # help on this method | ||
help(nums) # help on a list object | ||
nums.remove('five') # another list method | ||
|
||
sorted(nums) # 'function' that does not modify the list | ||
nums # it was not affected | ||
nums = sorted(nums) # overwrite the original list | ||
sorted(nums, reverse=True) # optional argument | ||
|
||
|
||
# FUNCTIONS | ||
|
||
def give_me_five(): # function definition ends with colon | ||
return 5 # indentation required for function body | ||
|
||
give_me_five() # prints the return value (5) | ||
num = give_me_five() # assigns return value to a variable, doesn't print it | ||
|
||
def calc(x, y, op): # three parameters (without any defaults) | ||
if op == 'add': # conditional statement | ||
return x + y | ||
elif op == 'subtract': | ||
return x - y | ||
else: | ||
print 'Valid operations: add, subtract' | ||
|
||
calc(5, 3, 'add') | ||
calc(5, 3, 'subtract') | ||
calc(5, 3, 'multiply') | ||
calc(5, 3) | ||
|
||
|
||
# EXERCISE: Write a function that takes two parameters (hours and rate), and | ||
# returns the total pay. | ||
|
||
def compute_pay(hours, rate): | ||
return hours * rate | ||
|
||
compute_pay(40, 10.50) | ||
|
||
|
||
# FOR LOOPS | ||
|
||
# print each list element in uppercase | ||
fruits = ['apple', 'banana', 'cherry'] | ||
for fruit in fruits: | ||
print fruit.upper() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
Oops, something went wrong.