Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
justmarkham committed Aug 7, 2015
0 parents commit 37c02f8
Show file tree
Hide file tree
Showing 8 changed files with 971 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.ipynb_checkpoints/
.DS_Store
*.pyc
extras/
502 changes: 502 additions & 0 deletions README.md

Large diffs are not rendered by default.

235 changes: 235 additions & 0 deletions code/00_python_intermediate_workshop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
'''
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:
Create a list of the first names of your family members.
Print the name of the last person in the list.
Print the length of the name of the first person in the list.
Change one of the names from their real name to their nickname.
Append a new person to the list.
Change the name of the new person to lowercase using the string method 'lower'.
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:
Print the name of the mom.
Change the size to 5.
Add 'Maggie' to the list of kids.
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
22 changes: 22 additions & 0 deletions other/python_packages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## List of Python packages used in the course

Note: Some of these packages have dependencies that will also need to be installed.

### Included with Anaconda ([complete list](http://docs.continuum.io/anaconda/pkg-docs.html))
* beautiful-soup
* ipython
* ipython-notebook
* matplotlib
* nltk
* numpy
* pandas
* pip
* requests
* scikit-learn
* scipy

### Available for installation via `conda`
* seaborn

### Available for installation via `pip`
* textblob
28 changes: 28 additions & 0 deletions other/setup_checklist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Setup checklist

This is a checklist to confirm that your laptop is set up properly for DAT8. If at any point you get an error message, please note the error message and we will help you to fix it! If you don't get any error messages, you are properly set up.

### GitHub
* Log into your GitHub account, and "star" the DAT8 repository (the one you are looking at right now) by clicking the Star button in the upper right corner of the screen.

### Git
* Open a command line application:
* For Windows, we recommend [Git Bash](http://git-scm.com/download/win) instead of Git Shell (which uses Powershell).
* For Mac, you will probably be using Terminal, or another command line tool of your choice.
* Type `git config --global user.name "YourFirstName YourLastName"` (including the quotes)
* Type `git config --global user.email "[email protected]"` (use the email address associated with your GitHub account)

### Python
* While still at the command line:
* Type `conda list` (if you choose not to use Anaconda, this will generate an error)
* Type `pip install textblob`
* Type `python` to open the Python interpreter
* While in the Python interpreter:
* Look at the Python version number. It should start with 2.7. If your version number starts with 3, that's fine as long as you are aware of the differences between Python 2 and 3.
* Type `import pandas`
* Type `import textblob`
* Type `exit()` to exit the interpreter. You can now close the command line application.
* Open Spyder (if you can't find Spyder, look for the Anaconda Launcher application)
* In the console (probably on the right side of the screen), type `import pandas`
* Type `import textblob`
* If this worked from the interpreter but not in Spyder, and you're using a Mac, try opening the PYTHONPATH manager and adding a path to where textblob was installed (such as `/Users/yourname/anaconda/lib/python2.7/site-packages/`). Then, restart Spyder.
Loading

0 comments on commit 37c02f8

Please sign in to comment.