Skip to content
Snippets Groups Projects
Commit bec55d3e authored by jackmic's avatar jackmic
Browse files

Upload New File

parent 81ebc872
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:44bff40d tags:
# Checkpoint 0
%% Cell type:markdown id:02215935 tags:
These exercises are a mix of Python and Pandas practice. Most should be no more than a few lines of code!
%% Cell type:code id:a0f62714 tags:
``` python
# here is a Python list:
a = [1, 2, 3, 4, 5, 6]
```
%% Cell type:code id:779d96b1 tags:
``` python
# get a list containing the last 3 elements of a
# Yes, you can just type out [4, 5, 6] but we really want to see you demonstrate you know how to do that in Python
b = a[-3::]
print(b)
```
%% Output
[4, 5, 6]
%% Cell type:code id:b6a54def tags:
``` python
# create a list of numbers from 1 to 100
c = list(range(1, 101))
print(c)
```
%% Output
[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]
%% Cell type:code id:487873ac tags:
``` python
# now get a list with only the even numbers between 1 and 100
# you may or may not make use of the list you made in the last cell
d = list(range(2, 101, 2))
print(d)
```
%% Output
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
%% Cell type:code id:3d4bb5dd tags:
``` python
# write a function that takes two numbers as arguments
# and returns the first number divided by the second
def divide(num1, num2):
return num1 / num2
```
%% Cell type:code id:b93669fa tags:
``` python
# write a function that takes a string as input
# and return that string in all caps
def capitalize(string):
return string.upper()
```
%% Cell type:code id:f55df04e tags:
``` python
# optional challenge - fizzbuzz
# you will need to use both iteration and control flow
# go through all numbers from 1 to 100 in order
# if the number is a multiple of 3, print fizz
# if the number is a multiple of 5, print buzz
# if the number is a multiple of 3 and 5, print fizzbuzz and NOTHING ELSE
# if the number is neither a multiple of 3 nor a multiple of 5, print the number
for num in list(range(1, 101)):
if((num % 3 == 0) & (num % 5 == 0)):
print("fizzbuzz")
elif(num % 3 == 0):
print("fizz")
elif(num % 5 == 0):
print("buzz")
else:
print(num)
```
%% Output
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
22
23
fizz
buzz
26
fizz
28
29
fizzbuzz
31
32
fizz
34
buzz
fizz
37
38
fizz
buzz
41
fizz
43
44
fizzbuzz
46
47
fizz
49
buzz
fizz
52
53
fizz
buzz
56
fizz
58
59
fizzbuzz
61
62
fizz
64
buzz
fizz
67
68
fizz
buzz
71
fizz
73
74
fizzbuzz
76
77
fizz
79
buzz
fizz
82
83
fizz
buzz
86
fizz
88
89
fizzbuzz
91
92
fizz
94
buzz
fizz
97
98
fizz
buzz
%% Cell type:code id:78aace0b tags:
``` python
# create a dictionary that reflects the following menu pricing (taken from Ahmo's)
# Gyro: $9
# Burger: $9
# Greek Salad: $8
# Philly Steak: $10
menu = {"Gyro":9, "Burger":9, "Greek Salad":8, "Philly Steak":10}
```
%% Cell type:code id:a2a78a4b tags:
``` python
# load in the "starbucks.csv" dataset
# refer to how we read the cereal.csv dataset in the tutorial
import pandas
df = pandas.read_csv("starbucks.csv")
```
%% Cell type:code id:68210b5f tags:
``` python
# output the calories, sugars, and protein columns only of every 40th row.
print(df.iloc[0::40][["calories", "sugars", "protein"]])
```
%% Output
calories sugars protein
0 3 0 0.3
40 5 0 0.4
80 350 58 15.0
120 140 20 6.0
160 110 24 2.0
200 200 41 3.0
240 180 35 3.0
%% Cell type:code id:ac0f0c12 tags:
``` python
# select all rows with more than and including 400 calories
hi_cal_rows = df[df["calories"] >= 400]
```
%% Cell type:code id:ee8f8241 tags:
``` python
# select all rows whose vitamin c content is higher than the iron content
vitc_greaterthan_iron_rows = df[df["vitamin c"] > df["iron"]]
```
%% Cell type:code id:d4de48bb tags:
``` python
# create a new column containing the caffeine per calories of each drink
df["caffeine per calories"] = df["caffeine"] / df["calories"]
```
%% Cell type:code id:3a72465a tags:
``` python
# what is the average calorie across all items?
df["calories"].mean()
```
%% Output
193.87190082644628
%% Cell type:code id:7714895a tags:
``` python
# how many different categories of beverages are there?
df["beverage_category"].nunique()
```
%% Output
9
%% Cell type:code id:62392999 tags:
``` python
# what is the average # calories for each beverage category?
bev_categories = df.groupby("beverage_category")
bev_categories["calories"].mean()
```
%% Output
beverage_category
classic espresso drinks 140.172414
coffee 4.250000
frappuccino blended coffee 276.944444
frappuccino blended crme 233.076923
frappuccino light blended coffee 162.500000
shaken iced beverages 114.444444
signature espresso drinks 250.000000
smoothies 282.222222
tazo tea drinks 177.307692
Name: calories, dtype: float64
%% Cell type:code id:435e9d80 tags:
``` python
# plot the distribution of the number of calories in drinks with a histogram
df["calories"].plot.hist(edgecolor="black", title = "Distribution of Calories")
```
%% Output
<AxesSubplot: title={'center': 'Distribution of Calories'}, ylabel='Frequency'>
%% Cell type:code id:ba8948eb tags:
``` python
# plot calories against total fat with a scatterplot
df.plot.scatter(x="calories", y="total fat", title="Calories vs Total Fat")
```
%% Output
<AxesSubplot: title={'center': 'Calories vs Total Fat'}, xlabel='calories', ylabel='total fat'>
%% Cell type:code id:4fe7fb2a tags:
``` python
```
%% Cell type:code id:5ebada65 tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment