In [1]:
#see what's in a module
import pandas
everything = dir(pandas)
print everything


['Categorical', 'DataFrame', 'DateOffset', 'DatetimeIndex', 'ExcelFile', 'ExcelWriter', 'Expr', 'Float64Index', 'Grouper', 'HDFStore', 'Index', 'IndexSlice', 'Int64Index', 'LooseVersion', 'MultiIndex', 'NaT', 'Panel', 'Panel4D', 'Period', 'PeriodIndex', 'Series', 'SparseArray', 'SparseDataFrame', 'SparseList', 'SparsePanel', 'SparseSeries', 'SparseTimeSeries', 'Term', 'TimeGrouper', 'TimeSeries', 'Timedelta', 'TimedeltaIndex', 'Timestamp', 'WidePanel', '__builtins__', '__doc__', '__docformat__', '__file__', '__name__', '__package__', '__path__', '__version__', '_np_version', '_np_version_under1p8', '_np_version_under1p9', '_period', '_sparse', '_testing', 'algos', 'bdate_range', 'compat', 'computation', 'concat', 'core', 'crosstab', 'cut', 'date_range', 'datetime', 'datetools', 'describe_option', 'eval', 'ewma', 'ewmcorr', 'ewmcov', 'ewmstd', 'ewmvar', 'ewmvol', 'expanding_apply', 'expanding_corr', 'expanding_corr_pairwise', 'expanding_count', 'expanding_cov', 'expanding_kurt', 'expanding_max', 'expanding_mean', 'expanding_median', 'expanding_min', 'expanding_quantile', 'expanding_skew', 'expanding_std', 'expanding_sum', 'expanding_var', 'factorize', 'fama_macbeth', 'get_dummies', 'get_option', 'get_store', 'groupby', 'hashtable', 'index', 'infer_freq', 'info', 'io', 'isnull', 'json', 'lib', 'load', 'lreshape', 'match', 'melt', 'merge', 'msgpack', 'notnull', 'np', 'offsets', 'ols', 'option_context', 'options', 'ordered_merge', 'pandas', 'parser', 'period_range', 'pivot', 'pivot_table', 'plot_params', 'pnow', 'qcut', 'read_clipboard', 'read_csv', 'read_excel', 'read_fwf', 'read_gbq', 'read_hdf', 'read_html', 'read_json', 'read_msgpack', 'read_pickle', 'read_sql', 'read_sql_query', 'read_sql_table', 'read_stata', 'read_table', 'reset_option', 'rolling_apply', 'rolling_corr', 'rolling_corr_pairwise', 'rolling_count', 'rolling_cov', 'rolling_kurt', 'rolling_max', 'rolling_mean', 'rolling_median', 'rolling_min', 'rolling_quantile', 'rolling_skew', 'rolling_std', 'rolling_sum', 'rolling_var', 'rolling_window', 'save', 'scatter_matrix', 'set_eng_float_format', 'set_option', 'show_versions', 'sparse', 'stats', 'timedelta_range', 'to_datetime', 'to_msgpack', 'to_pickle', 'to_timedelta', 'tools', 'tseries', 'tslib', 'unique', 'util', 'value_counts', 'version', 'wide_to_long']

In [ ]:
# Import *everything* from the math module on line 3!
"""
from math import *"""

In [ ]:
#how do functions work and how to get the output

def cube(number): #parameters
    return number ** 3
    
def by_three(number):
    if number % 3 == 0:
        print 'This number is divisble by three! Yay! The result is %d!' % (cube(number))
        return cube(number)
      
    else:
        print 'This number sucks'
        return False

      # need to do print before return

#get the output 
        
by_three(3) #argument. only 1 in this case
by_three(5)

In [ ]:
#min, max, and absolute values

def biggest_number(*args):
    print max(args)
    return max(args)
    
def smallest_number(*args):
    print min(args)
    return min(args)

def distance_from_zero(arg):
    print abs(arg)
    return abs(arg)


biggest_number(-10, -5, 5, 10)
smallest_number(-10, -5, 5, 10)
distance_from_zero(-10)

In [ ]:
#functions... and how they should work. Final exercise of 'Taking a vacation' lol

def hotel_cost(nights):
    return 140 * nights
    
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475
    else:
        print "Not a valid city"
        return False

def rental_car_cost(days):
    cost = days * 40 #NEED TO ADD A VARIABLE FOR THINGS IF YOU WANT TO CHANGE IT WITHIN A FUNCTION
    if days >= 7: 
        cost -= 50
    elif days >= 3: #instead of having two functions that say >= 3 and < 7, just manipulate the way if/elif is written
        cost -= 20
    return cost

def trip_cost(city, days, spending_money):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + (spending_money)

print trip_cost("Los Angeles", 5, 600)

In [17]:
x = ['fizz', 'fizz', 2, 'hi', 'lol']

def fizz_count(x):
    count = 0
    for item in x:
        if item == 'fizz':
            count += 1  #basically count = count + 1
  
    return count

In [26]:
#dictionary introduction
prices = {
    "banana" : 4,
    "apple"  : 2,
    "orange" : 1.5,
    "pear"   : 3,
}
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,
}

for key in prices:
    print "-", key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]

print #make it look nice lol
print

total = 0

for key in prices:
    subtotal = prices[key]*stock[key]
    print "-",key
    print subtotal
    total +=  subtotal
print
print "Total Inventory",total


- orange
price: 1.5
stock: 32
- pear
price: 3
stock: 15
- banana
price: 4
stock: 6
- apple
price: 2
stock: 0


- orange
48.0
- pear
45
- banana
24
- apple
0

Total Inventory 117.0

In [34]:
shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for x in food:
        total += prices[x] 
    return total
    print total
    

    
for value in prices:
    print value #this will print the variable name
    print prices[value] #this will print the value


orange
1.5
pear
3
banana
4
apple
2

In [36]:
shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# make sure stock is greater than 0

def compute_bill(food):
    total = 0
    for x in food:
        if stock[x] > 0: #REMEMBER TO CALL INTEGER VALULE
            total += prices[x] 
            stock[x] -= 1
    return total #make sure this return is in line with 'for' 
#for some reason didn't have to write else. but I think because there's nothing thats negative, and if it did it would just
#count as 0
#prices[x] so you can call the actual integer value. not the names from the list. otherwise if you wanted that, it would be prices[key] ?

In [ ]:
lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

# Add your function below!
def average(numbers):
    total = sum(numbers) #can't do sum(numbers) on left side of eqn
    total = float(total) #same as here.
    result = total / len(numbers)
    return result
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    weighted_avg = .1 * homework + .3 * quizzes + .6 * tests
    return weighted_avg

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    results = [] #empty list
    for student in students:
            results.append(get_average(student))
    return average(results) #return should be in line with for loop or else it will give you a wrong number
    
print get_class_average(students)
print get_letter_grade(get_class_average(students))

In [38]:
board = []
for i in range(0, 5): #makes 5 rows
    board.append(["O"] * 5) #makes 5 columns
#print board - would only make it come out as a list
#to make it come out as a proper 5x5 grid, have to make a function:

def print_board(board):
    for row in board:
        print " ".join(row) #okay this one is a little tricky: prints out a blank space right? the .join(row) means
        #that the items in the list 

print_board(board)


['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']

In [ ]:
board = []
for i in range(1, 6):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print_board(board)