DATA SCIENCE · CYCLE 2 · PRACTICE
Python Practice
Type real Python here and run it. There is nothing to install and no account. This page is practice: nothing on it is graded, and nothing you type leaves your computer.
How this page works
- Click in a cell and type. Press ▶ Run, or hold Shift and press Return.
- What the cell prints shows under it. The first run loads Python, which takes a moment.
- Work top to bottom. A cell can use only the names an earlier cell made.
- A task says Right. when your cell prints what it asks for. Stuck? Each task can show one way.
1. Read It, Then Run It
Say what the cell will print before you run it.
1import pandas as pd
2nums = pd.Series([3, 5, 10])
3print(nums.mean())
In Sheets this is: =AVERAGE on three numbers.
In English:
- Get the pandas toolbox and call it pd.
- Make a column of the three numbers, and call it nums.
- Print the mean of nums.
Predict: what will it print? Remember how pandas writes a whole mean.
Can’t run it here? Show what it prints
6.0
2. Change One Thing
Each cell works already. Change it so it prints what the task asks, then run it.
This cell prints the mean of nums. Change one word so it prints the median.
In Sheets this is: =AVERAGE. In English: Print the mean of nums.
Stuck? Show one way
print(nums.median())
This cell prints the largest number in nums. Change one word so it prints how many numbers nums has.
In Sheets this is: =MAX. In English: Print the largest number in nums.
Stuck? Show one way
print(nums.count())
Three noon temperatures. Add a fourth, 63, to the list. The mean should print 54.0.
In Sheets this is: =AVERAGE on three numbers. In English: 1. Make a column of the three numbers, and call it temps. 2. Print the mean of temps.
Stuck? Show one way
temps = pd.Series([48, 55, 50, 63])
print(temps.mean())
This cell prints an SD rounded to 2 places, 3.39. Change one number so it rounds to 1 place.
In Sheets this is: =STDEV, then rounding. In English: 1. Make a column of the five numbers, and call it scores. 2. Print the standard deviation of scores, dividing by n − 1, rounded to 2 places.
Stuck? Show one way
scores = pd.Series([3, 4, 8, 9, 11])
print(round(scores.std(), 1))
3. Write It Yourself
The cells start empty. Say each line in English first, then type it. Spelling and capitals must match: pd.Series has a capital S.
Make a column of the numbers 8, 3 and 10, and call it waits. Print its mean.
Stuck? Show one way
waits = pd.Series([8, 3, 10])
print(waits.mean())
Print the median of waits. Guess it first: put the three in order.
Stuck? Show one way
print(waits.median())
Make a column called laps holding 12, 15, 15, 18 and 20. Print its standard deviation, rounded to 2 places. You built this number one column at a time in L19.
Stuck? Show one way
laps = pd.Series([12, 15, 15, 18, 20])
print(round(laps.std(), 2))
Two lines: print the largest of laps, then the mean of laps.
Stuck? Show one way
print(laps.max())
print(laps.mean())
Messy data. Five weights were typed as text: one box is empty and one says "9 kg". The first two lines keep the numbers and call the result clean. Add two lines: print how many numbers are left, then print their mean.
In Sheets this is: fixing "9 kg" and the blank by hand. In English: 1. Make a column of the five things typed, as text, and call it raw. 2. Keep the numbers in raw; anything else becomes blank. Call the result clean.
Stuck? Show one way
raw = pd.Series(["7", "", "9 kg", "5", "6"])
clean = pd.to_numeric(raw, errors="coerce")
print(clean.count())
print(clean.mean())
4. A Real File
The class catches file from L19 is on this page: 18 students, a catch with each hand, in centimeters. These cells build on each other, like a notebook: do them in order.
| File | Its columns |
L19-class-catches.csv | Student, Strong hand (cm), Other hand (cm) |
Run this cell as it is. It opens the file as a table called df, and counts the rows.
In Sheets this is: File > Import the CSV, then count the rows. In English: 1. Open the file as a table called df. 2. Print how many rows.
Stuck? Show one way
df = pd.read_csv("L19-class-catches.csv")
print(len(df), "rows")
The first line calls the strong-hand column strong. Add a line that prints its mean, rounded to 2 places.
In Sheets this is: column B. In English: 1. Take the strong-hand column, and call it strong.
Stuck? Show one way
strong = df["Strong hand (cm)"]
print(round(strong.mean(), 2))
Now the other hand. Make a name, other, for the column df["Other hand (cm)"]. Then print its mean, rounded to 2 places.
Stuck? Show one way
other = df["Other hand (cm)"]
print(round(other.mean(), 2))
Print each hand’s standard deviation, rounded to 2 places. Strong hand first.
Stuck? Show one way
print(round(strong.std(), 2))
print(round(other.std(), 2))
Which hand is more consistent? Print its name: print("strong") or print("other").
Stuck? Show one way
print("strong")
5. Your Notebook
A notebook of your own. Add cells, change them, delete them. This browser keeps your cells until you press Start over. Two files are here to open by name.
| File | What it is | Columns to try |
L19-class-catches.csv | The class catches, 18 rows | Strong hand (cm), Other hand (cm) |
L17-YRBSS.csv | The CDC’s survey of US high school students, 13,583 rows | height (meters), weight (kilograms), age, physically_active_7d, strength_training_7d (days in the last 7) |
The first cell. In Sheets this is: File > Import the CSV, then count the rows. In English: 1. Get the pandas toolbox and call it pd. 2. Open the file as a table called survey. 3. Print how many rows.
The second cell. In Sheets this is: =COUNT and =AVERAGE on the height column. In English: 1. Take the height column, drop its blanks, and call the result h. 2. Print how many heights. 3. Print the mean of h, to 2 places.
.dropna() drops the blanks, so h holds every height a student gave. Things to try:
- Print the SD of h, rounded to 3 places.
- Do the same for
survey["weight"]. Is its mean above its median? L17 said why.
- Draw a histogram: a cell with the one line
h.hist(bins=10). In Sheets this is Insert > Chart, Chart type Histogram chart. In English: draw a histogram of h with 10 bars. The picture shows under the cell. Is it a bell? L21 says what that tells you.
6. Your Project File
Until Colab is on for your account, this is where you run Python on your own project. Your Sheet stays the project file, and the Sheet is what gets graded. These cells read your CLEAN tab and print the numbers your ANALYSIS tab should hold, so you can check every one. If they differ, a row is off: find it, fix it on CLEAN and give the fix a row on LOG.
- On your Sheet, click the CLEAN tab.
- File > Download > Comma-separated values (.csv). The file goes to your Downloads.
- Run the first cell below and choose that file. It is read on this computer and sent nowhere, and this browser offers it again on the next page.
- Run the other cells in order. Each one says which ANALYSIS cells it checks.
No CLEAN tab yet? It is made in L22, Fri 4 Dec. See your Sheet, tab by tab.
In Sheets this is: LOG's =COUNTA(CLEAN!C2:C), and B11's =COUNT(CLEAN!B:B).
In English:
- Get the tool that opens files.
- Get the pandas toolbox and call it pd.
- Show a Choose file button, and call the name of the file you pick name.
- Open the file as a table called df.
- Keep only the numbers in the number column, and put them back in that column.
- Print how many rows.
- Print how many numbers.
Predict: LOG D2 holds your rows and ANALYSIS B11 your numbers. If the two differ, that many rows have no number.
In Sheets this is: ANALYSIS B12, B13 and B14: =AVERAGE, =MEDIAN and =STDEV of CLEAN!B:B (L17, L19).
In English:
- Take the number column, and call it n.
- Print the mean of n, rounded to 2 places.
- Print the median of n.
- Print the SD of n, rounded to 2 places.
Predict: read B12, B13 and B14 first. Will all three match?
In Sheets this is: ANALYSIS B15 to B19: =MIN, =QUARTILE(…,1), =QUARTILE(…,3), =MAX and =B17-B16 (L18).
In English:
- Find the value a quarter of the way up n, in order, and call it q1.
- Find the value three quarters of the way up, and call it q3.
- Print the smallest value, q1 and q3 rounded to 2 places, and the largest value.
- Print q3 minus q1, the IQR, rounded to 2 places.
Predict: is your IQR more or less than your SD in B14?
In Sheets this is: the group table, ANALYSIS A6 to C8: =COUNTIF(CLEAN!A:A,A7) and =AVERAGEIF(CLEAN!A:A,A7,CLEAN!B:B) (L22).
In English:
- Split the rows into groups by the group column, take the number column of each, and call the result by_group.
- Print how many numbers each group has.
- Print each group's mean, rounded to 2 places.
Predict: C7 and C8 hold your two means. Python counts numbers, and =COUNTIF counts rows, so a count may come out smaller here: those are rows with no number. Python lists every group in column A, not only your two.
In Sheets this is: the histogram at D2 on ANALYSIS: Insert > Chart, Chart type Histogram chart (L21).
In English:
- Draw a histogram of n with 20 bars.
Predict: your sentence in B4 says whether it is a bell. Does this one agree? Change 20 to 10 and run it again.
The shuffle test comes in January, in L29: it gets its own cell here then. Each cell's run goes into the PDF you hand in, under On my project file.
When You See Red
A red line is Python saying which line it could not follow. Read the red line: its first word names the problem.
| It says | It means | Check |
NameError | A name is used before a line makes it. | Spelling and capitals. Or run the cell above first. |
SyntaxError | Python could not read the line. | Every ( has a ), every [ has a ], every quote has its pair. |
KeyError | No column has that exact name. | The name inside df[" "]: spelling, capitals, spaces. |
AttributeError | The word after the dot is not one pandas knows. | The word after the dot: .mean(), and no .average(). |
Stuck on any of this? Ask Mr. Case in class or send him a message in Canvas. Copy your cell and the red line into the message.