In [1]:
#see what's in a module
import pandas
everything = dir(pandas)
print everything
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
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
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)
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)