Ibnovate Course 2 · The Rising Builders
⏱ 75 minLive session

Session 2 — Playing with Data

Duration: 75 min · Format: live online

What you'll learn: by the end, you can store many values in a list, loop through them, and compute a total, an average, and the maximum.

Soft skill focus — Problem-solving

Today you'll also grow Problem-solving. A loop that builds a total is a vague goal ("add these up") turned into precise, repeatable steps the computer can follow.

What you'll need


Hook

Think about this:

Here's the idea: data science means handling thousands of values at once. Today you learn the two tools that make that possible — a list holds all the values in one place, and a loop processes them in just a few lines.


Teach — A list holds many values

A list stores many values in order. Each value has a position called an index, and indexes start at 0, not 1.

Look at this diagram — trace the loop visiting each number to build a total:

A list of four numbers and a loop that visits each one to make a total of 53

Type and run this in Colab:

scores = [12, 7, 25, 9]

print(scores[0])      # 12  (first item)
print(len(scores))    # 4   (how many)

Ask yourself: "Why does scores[0] give 12 and not 7?" (Answer: lists start counting at 0, so index 0 is the first item.)

⚠ Watch for the off-by-one trap: it's tempting to expect scores[1] to be the first item. It's actually the second. Counting from 0 is the single most common list mistake — watch for it early.


Teach — A loop repeats for every item

A for-loop runs the same steps for each item — whether the list has 4 numbers or 4 million. Notice the indentation: the indented line runs once per item.

Type and run this in Colab:

scores = [12, 7, 25, 9]
total = 0

for s in scores:
    total = total + s     # add each score to the total

print("Total:", total)          # 53
print("Average:", total / len(scores))   # 13.25
print("Highest:", max(scores))  # 25

Walk through one pass of the loop in your head: total starts at 0, then becomes 12, then 19, then 44, then 53.

Ask yourself: "What would happen if I put print("Total:", total) inside the loop, indented?" (Answer: it would print the running total four times, not once.)

⚠ Watch for: Python has handy shortcuts — sum(scores), max(scores), min(scores) — and it's tempting to reach for them straight away. That's fine, but make sure you can also explain how the loop builds the total underneath. That's what makes you a real coder.


Activity — Analyse your own data

Open Google Colab and analyse a real list from your own life.

Type and run this in Colab:

sleep = [8, 7, 6, 9, 7, 10, 8]

Then:

  1. Use a loop (or sum) to print the total and the average.
  2. Print the most and least with max() and min().
  3. Bonus: count how many nights you slept 8 or more hours using a loop and an if.

Watch out for the classic "biggest" bug. Here's some broken code — can you work out why it only ever prints 9?

scores = [12, 7, 25, 9]
for s in scores:
    biggest = s
print(biggest)

Why is biggest always the last number? Here's the fix: biggest is overwritten every loop, so it ends on the last item. To find the real biggest, start biggest = scores[0], then inside the loop if s > biggest: biggest = s. (Or just use max(scores).)


Check yourself

Try these — then check your answers:

  1. What is scores[0] in scores = [12, 7, 25, 9]?12 — lists start counting at 0, so index 0 is the first item.
  2. What does a for-loop do? → Runs the same steps for every item in a list, automatically.
  3. How do you get the average of a list?sum(list) / len(list) — the total divided by how many items.

Wrap-up


Tips & extra challenges

A DataFrame table with columns name, age, score and rows 0, 1, 2

import pandas as pd

# load a real, public dataset (thousands of rows!)
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
df = pd.read_csv(url)

print(df.shape)              # (rows, columns)
df.head()                    # peek at the first 5 rows
print(df["body_mass_g"].mean())   # average penguin mass

Real data has gaps — df = df.dropna() removes empty rows, then check df.shape again. Try printing the average of another column, and the count of each species with df["species"].value_counts().

Vocabulary

Term Meaning
List Many values stored in order
Index An item's position (starts at 0)
Loop Repeating steps for each item
Average / Mean Total ÷ how many
DataFrame A table of data in Pandas

Resources

Practice set

Practise on your own — extra exercises on lists, indexing, loops, and totals/averages/max, easy to hard.

1. Predict the output: for nums = [4, 8, 15, 16, 23], what does print(nums[1]) show, and what does print(len(nums)) show? → 8 (index 1 is the second item) and 5.

2. Fix the index: you want the first item but wrote nums[1]. → Fix: lists start at 0, so the first item is nums[0].

3. Write a loop that prints every item in pets = ["cat", "dog", "fish"] on its own line. →

for pet in pets:
    print(pet)

4. Build a total with a loop (no sum): add up prices = [3, 5, 2, 10] and print it. → start total = 0, then loop adding each; answer 20.

prices = [3, 5, 2, 10]
total = 0
for p in prices:
    total = total + p
print(total)   # 20

5. Average: for temps = [20, 22, 19, 25, 24], print the average. → sum(temps) / len(temps) gives 22.0.

6. Count with if (harder): using a loop, count how many scores in [55, 90, 72, 40, 88] are 60 or higher. → answer 3.

scores = [55, 90, 72, 40, 88]
count = 0
for s in scores:
    if s >= 60:
        count = count + 1
print(count)   # 3

7. Fix the "biggest" bug (hardest): why does this always print 9, and how do you fix it? → biggest is overwritten every pass, so it ends on the last item. Fix: start biggest = scores[0] and compare inside the loop with if s > biggest: biggest = s (or just max(scores)).

scores = [12, 7, 25, 9]
for s in scores:
    biggest = s
print(biggest)   # bug: prints 9

Going deeper (optional)

If you're flying through this, here's a bonus — a loop can build a new list, not just a running number. Say we want to convert a week of hours slept into "enough / not enough":

sleep = [8, 7, 6, 9, 7, 10, 8]
labels = []                 # start with an empty list

for hours in sleep:
    if hours >= 8:
        labels.append("enough")
    else:
        labels.append("short")

print(labels)

append adds one item to the end of a list each pass, so the loop turns seven numbers into seven labels. This is the exact idea behind creating a new column from an old one in Pandas — you're learning the pattern before you meet the shortcut. Try counting how many "enough" nights there were by looping over labels.

Common mistakes & fixes

What's next

Session 3 — Your First Prediction: you'll build a real model that learns a pattern from data and predicts a new value with scikit-learn.

Ibnovate · Build · Innovate
Type to search · Esc to close
Welcome back
Sign in to continue building.
Accounts are created by Ibnovate — ask your instructor for your login.
🔒