Skip to article frontmatterSkip to article content

Exercise 2 - Collections and Loops

  • 🏆 20 points available
  • ✏️ Last updated on 9/4/2025

▶️ First, run the code cell below to import unittest, the module used for the 🧭 Check Your Work boxes and the autograder.

# DO NOT MODIFY THE CODE IN THIS CELL
import unittest
tc = unittest.TestCase()

🎯 Challenge 1: Create a List

👇 Tasks

You have three job offers with salaries of 100000, 110000, and 120000.

Create a new list variable named offers that contains these three salary amounts (in the same order).

💡 Hint

my_list = [1, 2, 3] creates a list named my_list with initial values 1, 2, and 3.

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(offers[0])
print(offers[1])
print(offers[2])

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'create-a-list'
_points = 2

tc.assertEqual(offers, [100000, 110000, 120000])

🎯 Challenge 2: Number of NYX Products

👇 Tasks

Assume you own 30 makeup products. The brands variable contains the list of brand names for each product in your collection. Can you count the number of NYX products in your collection and store the result in the num_NYX_products variable?

Use a for loop to perform this task.

brands = ["Bobbi Brown", "Maybelline", "Hourglass", "Clinique", 
          "NYX", "Kosas", "Bobbi Brown", "Hourglass", "NYX", 
          "NYX", "Clinique", "NYX", "Kosas", "Maybelline", 
          "Urban Decay", "Revlon", "Clinique", "NYX", 
          "Bobbi Brown", "Sephora", "Urban Decay", "NYX", 
          "Clinique", "NYX", "Estee Lauder", "Hourglass", 
          "Kosas", "NYX", "NYX", "Sephora"]
num_NYX_products = 0

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"You have {num_NYX_products} NYX products.")

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'count-occurrences-in-a-list'
_points = 3

tc.assertEqual(num_NYX_products, 9)

🎯 Challenge 3: Add All Values in a List

👇 Tasks

Using a for loop, calculate the sum of all values in the list sales and store the result in the variable total_sales.

💡 Hint

The code below iterates through my_list and incrementally sums up the values in my_list.

my_list = [1, 2, 3, 4]
total = 0

for v in my_list:
    total = total + v
sales = [31, 30, 24, 20]
total_sales = 0

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"Total sales: {total_sales}")

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'calculate-total-sales'
_points = 3

list_SOL = [31, 30, 24, 20]

tc.assertEqual(total_sales, sum(list_SOL))

🎯 Challenge 4: Sum of Even Numbers

👇 Tasks

Using a for loop, find the sum of only even numbers in nums list and store the result to even_sum.

🔑 Sample Code

my_list = [1, 2, 3, 4]
my_sum = 0

for x in my_list:
    if x % 2 == 0:
        my_sum = my_sum + x
nums = [1, 2, 3, 4, 5, 6, 7, 8]
even_sum = 0

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"Sum of even numbers: {even_sum}")

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'even-sum'
_points = 3

tc.assertEqual(even_sum, 20)

🎯 Challenge 5: Total Salary of Chiefs Players 🏈

👇 Tasks

The variable players contains a list of 5 dictionaries. Each dictionary includes an NFL player’s name, team, and salary.

Calculate the total salary of Kansas City Chiefs players ("team" == "Chiefs"). Store the result in the variable chiefs_total_salary.

Use a for loop to perform this task.

players = [
    {
        "name": "Tom Brady",
        "team": "Buccaneers",
        "salary": 23000000
    },
    {
        "name": "Patrick Mahomes",
        "team": "Chiefs",
        "salary": 9900000
    },
    {
        "name": "Aaron Donald",
        "team": "Rams",
        "salary": 19900000
    },
    {
        "name": "Tyreek Hill",
        "team": "Chiefs",
        "salary": 18000000
    },
    {
        "name": "Travis Kelce",
        "team": "Chiefs",
        "salary": 15000000
    }
]
chiefs_total_salary = 0

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"Chiefs players' total salary is {chiefs_total_salary}!")

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'chiefs-players-salary'
_points = 4

tc.assertAlmostEqual(chiefs_total_salary, 42900000.0)

🎯 Challenge 6: Average Positive and Negative Transactions

👇 Tasks

The transactions variable contains a list of transaction amounts. Your goal is to calculate the following two variables:

  • pos_avg: the average of all positive transactions
  • neg_avg: the average of all negative transactions

📘 Example

transactions = [10, -5, -15, 30, 20]

  • pos_avg is 20 ((10 + 30 + 20) / 3) .
  • neg_avg is -10 ((-5 + -15) / 2).

⚔️ Rules

  • Create additional variables as needed for your calculations.
  • You may not manually count positives or negatives when dividing — your code must handle the counting.
  • You must use a for loop to solve this task.
transactions = [15, 8, 7, 5, 15, 72, -20, -66, 9, -4, -11, 21, 19]

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f'Average of positive transactions is {pos_avg}')
print(f'Average of negative transactions is {neg_avg}')

🧭 Check Your Work

Run the code cell below to test your solution.

  • ✔️ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = 'average-pos-neg-transactions'
_points = 5

tc.assertAlmostEqual(pos_avg, 19.0)
tc.assertAlmostEqual(neg_avg, -25.25)