Lesson 3 - For Loops in Python

Let’s say we want to do the same thing multiple times in a program. For example, printing each rating or app name from a dataset. Instead of writing separate lines for each item, we can use something much better — a for loop.

This lesson is all about learning how to repeat tasks using for loops. By the end, you’ll be able to:

  • Loop through a dataset
  • Extract specific values from each row
  • Sum up data like ratings or counts
  • Calculate averages
  • Work with large datasets quickly and easily

We’ll start simple and work our way up using the dataset below:

App NamePriceCurrencyTotal ReviewsRating
Facebook0.00USD2,974,6763.5
Instagram0.00USD2,161,5584.5
Clash of Clans0.00USD2,130,8054.5
Fruit Ninja Classic1.99USD698,5164.5
Minecraft: Pocket Edition6.99USD522,0124.5

Here’s how we used to calculate the average rating using just five rows:

row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Fruit Ninja Classic', 1.99, 'USD', 698516, 4.5]
row_5 = ['Minecraft: Pocket Edition', 6.99, 'USD', 522012, 4.5]

app_data_set = [row_1, row_2, row_3, row_4, row_5]

avg_rating = (
    app_data_set[0][-1] +
    app_data_set[1][-1] +
    app_data_set[2][-1] +
    app_data_set[3][-1] +
    app_data_set[4][-1]
) / 5

print(avg_rating)

Output:

4.3

This works when we only have 5 rows. But imagine doing this for thousands of apps — it would take forever.

We need a smarter way to repeat the same action for every app in the dataset. This is where for loops shine.

In the next section, we’ll learn how to write a for loop to make this whole process simpler and faster.


Great! Here’s Section 2: Introducing the for Loop, written in the same clear, beginner-friendly tone and markdown format for your Hugo-based site:


Introducing the for Loop

Let’s say you have a list of app ratings:

app_ratings = [3.5, 4.5, 4.5, 4.5, 4.5]

What if you wanted to print every rating from this list? Sure, you could do it one by one:

print(app_ratings[0])
print(app_ratings[1])
print(app_ratings[2])
# ...and so on

But that’s not practical — especially when you’re dealing with hundreds or thousands of items. Instead, we can loop through the list using a for loop:

for element in app_ratings:
    print(element)

Output:

3.5
4.5
4.5
4.5
4.5

This tells Python: “For each item in app_ratings, name it element, and print it.”

This process of looping through a list and doing something with each item is called a for loop.

Looping Over App Names

Let’s try it with names instead of ratings:

app_names = ['Facebook', 'Instagram', 'Clash of Clans', 'Fruit Ninja Classic', 'Minecraft: Pocket Edition']

for name in app_names:
    print(name)

Output:

Facebook
Instagram
Clash of Clans
Fruit Ninja Classic
Minecraft: Pocket Edition

Important Notes

  • The word for starts the loop.
  • name (or element, or any variable name) is the placeholder for each item.
  • The line that follows must be indented — usually 4 spaces — so Python knows it belongs inside the loop.
  • The loop repeats once for every item in the list.

With just two lines of code, you can handle lists of any length.


Looping Over Rows to Access Ratings

Now let’s return to our dataset. Each row in app_data_set is a list that contains details about one app. For example:

row_1 = ['Facebook', 0.0, 'USD', 2974676, 3.5]
row_2 = ['Instagram', 0.0, 'USD', 2161558, 4.5]
row_3 = ['Clash of Clans', 0.0, 'USD', 2130805, 4.5]
row_4 = ['Fruit Ninja Classic', 1.99, 'USD', 698516, 4.5]
row_5 = ['Minecraft: Pocket Edition', 6.99, 'USD', 522012, 4.5]

app_data_set = [row_1, row_2, row_3, row_4, row_5]

We want to access the last element from each row — that’s the app rating. Previously, we did it manually like this:

print(row_1[-1])
print(row_2[-1])
# and so on...

Let’s make it easier using a for loop:

for row in app_data_set:
    rating = row[-1]
    print(rating)

Output:

3.5
4.5
4.5
4.5
4.5

Explanation

  • for row in app_data_set: loops through every row (each row is a list).
  • row[-1] gives us the last value in that row — the rating.
  • print(rating) shows us that rating.

This works no matter how many rows we have — 5 or 5,000 — and we only need two lines of code.

Bonus Tip

If you want to see the full row instead of just the rating, change it to:

for row in app_data_set:
    print(row)

You’ll get each full list printed out one by one.


Summing Values with a Loop

So far, we’ve printed each rating. Now let’s try something even more useful — adding them all together.

To do this, we’ll need to:

  1. Start with a total of 0
  2. Go through each row
  3. Add the app’s rating to the total during each loop

Here’s how you can do it:

app_data_set = [
    ['Facebook', 0.0, 'USD', 2974676, 3.5],
    ['Instagram', 0.0, 'USD', 2161558, 4.5],
    ['Clash of Clans', 0.0, 'USD', 2130805, 4.5],
    ['Fruit Ninja Classic', 1.99, 'USD', 698516, 4.5],
    ['Minecraft: Pocket Edition', 6.99, 'USD', 522012, 4.5]
]

rating_sum = 0  # Step 1: Start with 0

for row in app_data_set:
    rating = row[-1]             # Step 2: Get the rating
    rating_sum += rating         # Step 3: Add to the total
    print(rating_sum)            # Optional: See the running total

Output:

3.5
8.0
12.5
17.0
21.5

At the end of the loop, rating_sum holds the total of all the ratings.

Why This Works

  • The variable rating_sum starts at 0
  • Each time the loop runs, we add the current rating to the total
  • += is a shortcut for: rating_sum = rating_sum + rating

This technique is useful for more than just ratings. You can use it to sum downloads, review counts, or anything numeric in a dataset.

Next, let’s use this total to calculate the average rating.


Calculating the Average Rating

We’ve summed all the ratings — now let’s figure out the average.

To calculate an average, you follow a simple formula:

average = total / number of items

We already know how to calculate the total using a loop. Now we just need to divide by how many rows are in the dataset.

Let’s go step by step:

app_data_set = [
    ['Facebook', 0.0, 'USD', 2974676, 3.5],
    ['Instagram', 0.0, 'USD', 2161558, 4.5],
    ['Clash of Clans', 0.0, 'USD', 2130805, 4.5],
    ['Fruit Ninja Classic', 1.99, 'USD', 698516, 4.5],
    ['Minecraft: Pocket Edition', 6.99, 'USD', 522012, 4.5]
]

rating_sum = 0

for row in app_data_set:
    rating = row[-1]
    rating_sum += rating

avg_rating = rating_sum / len(app_data_set)
print(avg_rating)

Output:

4.3

Let’s break it down

  • rating_sum ends up as 21.5 (the sum of all ratings)
  • len(app_data_set) is 5 (we have 5 rows)
  • So: 21.5 / 5 = 4.3

That’s your average rating.

You now know how to:

  • Loop through rows
  • Extract values
  • Add them up
  • Divide by the count to get an average

Reading a File into Python

So far, we’ve been working with a small dataset that we typed ourselves. But real-world datasets are usually stored in files — like CSVs — not hardcoded lists.

Let’s see how to read data from a .csv file into Python, so we can work with thousands of rows just like we did with five.

Step-by-step: How to open a CSV file

We’re going to read a file named AppleStore.csv (Download Here). This file contains over 7,000 app entries from the App Store.

Here’s how you do it in Python:

from csv import reader

opened_file = open('AppleStore.csv')
read_file = reader(opened_file)
apps_data = list(read_file)

Now, apps_data is a list of lists, just like the one we were using earlier — except this one has thousands of rows.

Let’s explore the data a bit:

print(len(apps_data))         # How many rows in total?
print(apps_data[0])           # The header row
print(apps_data[1:3])         # First two actual data rows

Example output:

7198
['id', 'track_name', 'size_bytes', ..., 'prime_genre', ...]
[['284882215', 'Facebook', ...], ['389801252', 'Instagram', ...]]

Note: There are 7198 rows, but only 7197 are actual apps. The first row is the header, which contains the column names.

Tip: Exclude the header row

We often want to skip the header row and only loop through the data. You can do that with slicing:

data_only = apps_data[1:]

Now data_only holds just the app entries — no column names.


Handling Errors in the Dataset

When you start working with real datasets like AppleStore.csv, you might run into some errors — especially when trying to calculate things like sums or averages.

Let’s take a look at a common error you might see:

The Problem

Suppose we try this:

rating_sum = 0

for row in apps_data:
    rating = row[7]
    rating_sum += rating

Instead of working, this code gives an error like this:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

What went wrong?

  • row[7] gives us the user rating
  • But it’s still in string format
  • Python can’t add a string to an integer

Also, we’re including the first row, which is the header. That row contains text like 'user_rating' — not a number.

The Fix (Two Parts)

1. Skip the header row

We can do this using slicing:

for row in apps_data[1:]:

2. Convert the rating to a float

rating = float(row[7])

So the full fixed version looks like this:

rating_sum = 0

for row in apps_data[1:]:
    rating = float(row[7])
    rating_sum += rating

Now everything works as expected.


Calculating the Average for All Apps

Now that we’ve fixed the data type issue and excluded the header row, we’re ready to calculate the average user rating across all 7,197 apps in the dataset.

Let’s go step by step:

from csv import reader

opened_file = open('AppleStore.csv')
read_file = reader(opened_file)
apps_data = list(read_file)

rating_sum = 0

for row in apps_data[1:]:
    rating = float(row[7])  # Convert string to float
    rating_sum += rating

avg_rating = rating_sum / len(apps_data[1:])
print(avg_rating)

Output:

4.0 (or a value close to it depending on the actual data)

Why this works:

  • apps_data[1:] skips the header
  • row[7] is the user rating (stored as a string in the CSV)
  • float(row[7]) converts the rating to a number
  • rating_sum += rating adds each rating to the total
  • len(apps_data[1:]) gives the number of actual apps (7197)

This method is efficient, readable, and works even if your dataset grows to 100,000 rows.

Quick Reminder

  • Always convert strings to numbers if you’re doing math
  • Be careful not to include the header row in calculations

Building a List with append()

There’s another useful way to calculate the average rating: by building a list of ratings first, and then using Python’s built-in functions to do the math.

Let’s walk through the process.

Step-by-step:

  1. Start with an empty list
  2. Loop through the dataset (skip the header)
  3. Extract and convert the rating from each row
  4. Add that rating to the list using .append()
  5. Use sum() and len() to get the average

Here’s the code:

from csv import reader

opened_file = open('AppleStore.csv')
read_file = reader(opened_file)
apps_data = list(read_file)

ratings = []  # Step 1: Empty list

for row in apps_data[1:]:  # Step 2: Skip header
    rating = float(row[7])  # Step 3: Convert
    ratings.append(rating)  # Step 4: Add to list

avg_rating = sum(ratings) / len(ratings)  # Step 5
print(avg_rating)

Output:

4.0 (or similar, depending on the dataset)

Why use .append()?

  • You keep all values in one place (the ratings list)
  • You can reuse the list later to do more analysis
  • It’s clear and easy to understand

This method is especially useful if you want to:

  • Filter ratings later
  • Compare ratings across genres or price groups
  • Do other math like min(), max(), or sorting

Review and Takeaways

Well done! You’ve just finished an important lesson — learning how to repeat tasks and work with large datasets using for loops in Python.

Let’s recap what you learned:

✅ Concepts You Mastered

  • For loops let you repeat a task for every item in a list
  • You can loop over rows in a dataset to pull out data like ratings or review counts
  • Python’s for loop is powerful for analyzing large datasets quickly
  • You learned to use:
    • += to add up values
    • len() to count items
    • sum() to calculate totals
    • append() to build a list dynamically
  • You also learned to open CSV files and read them using the csv.reader() method
  • You handled real-world errors like data stored as strings and header rows in CSVs

🔧 Syntax You Practiced

# Looping through a list
for item in a_list:
    print(item)

# Building a list
ratings = []
ratings.append(4.5)

# Reading a file
from csv import reader
opened_file = open('AppleStore.csv')
read_file = reader(opened_file)
apps_data = list(read_file)

🚀 What’s Next?

In the next lesson, you’ll learn about list slicing and indexing in more depth — useful tools to pull out exactly the data you need. These techniques will make your analysis more precise and flexible.

Keep practicing, and try modifying the code on your own:

  • What’s the average rating of paid apps only?
  • What’s the highest rating in the dataset?
  • Can you print only the names of apps rated above 4.5?

You’ve got this! 🎯