Skip to article frontmatterSkip to article content

Quiz 1 Prep - Python Basics

  • πŸ† 25 points available
  • ✏️ Last updated on 9/9/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: Average of Positive Transactions¢

πŸ‘‡ TasksΒΆ

The transactions variable contains a list of transaction amounts. Your task is to calculate the average of all positive transactions and store the result in a new variable named pos_avg.

You can assume that there will be at least one positive number in the transactions list.

πŸ“˜ ExampleΒΆ

transactions = [10, -5, -15, 30, 20]
  • pos_avg is 20 ((10 + 30 + 20) / 3).

πŸ“ RulesΒΆ

  • You may create additional variables as needed for your calculations.
  • You must not manually compute the average by counting the positives yourself β€” use code to track the count.
  • Use a for loop to complete this task.
# fmt: off
transactions = [15, 8, 7, 5, 15, 72, -20, -66, 9, -4, 
                -11, 21, 19, 33, -40, -9, -8, -7, 0, 
                -55, 10, 20, -30, -1, -2, -5, -10, 4, 
                7, 8, 10, 9, -50, -10, -1, -3, -8]
# fmt: on

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(f"Average of positive transactions is {pos_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 = "avg-of-positive-transactions"
_points = 6

tc.assertAlmostEqual(pos_avg, 16)

🎯 Challenge 2: Compare Lists by Index¢

πŸ‘‡ TasksΒΆ

You are given two lists: t1 and t2. Your task is to compare their values at each index using a for loop, and count the number of occurrences where the value in t1 is equal to the corresponding value in t2. Store the counted result to a variable named num_equal.

You can assume that both lists have the same length (i.e., the same number of elements).

πŸ“€ Sample Input/OutputΒΆ

t1 = [100, -50, 400, 300, 0]
t2 = [-100, 50, 400, -800, 0]

The expected output of the sample input above is 2 because t1 at indexes 2 and 4 are equal to their corresponding values in t2 (400 == 400 and 0 == 0).

# fmt: off
t1 = [-51, -22, 24, -4, 17, 40, -7, 
      -59, 21, 82, -19, 25, 65, -99, 
      54, 72, -16, -57, -20, 17]
t2 = [-64, -14, 30, -87, -98, 5, -7, 
      -10, 21, -98, 93, 43, 65, 66, 
      51, -98, -16, 57, -56, -17]
# fmt: on

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(num_equal)

🧭 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 = "compare-lists-by-index"
_points = 6

tc.assertEqual(num_equal, 4)

🎯 Challenge 3: Average of Odd Numbers¢

πŸ‘‡ TasksΒΆ

  • Find the average of only the odd numbers in the nums list.
  • Store the result in a variable named result.

πŸ”‘ HintΒΆ

Create extra variables to keep a count of the number of odd numbers and their total.

# fmt: off
nums = [10, 5, 6, 4, 3, 1, 2, 7, 8, 2, 4, 3, 1, 5, 
        3, 7, 1, 4, 6, 3, 3, 9, 6, 5, 1, 2, 1, 12, 
        8, 6, 5, 3, 2, 2, 4, 5, 1, 3, 6, 4, 1, 3, 
        2, 7, 8, 8, 8, 8, 9, 5, 1, 3, 4, 5, 6, 1, 2]
# fmt: off

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(result)

🧭 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-of-odd-numbers"
_points = 6

tc.assertAlmostEqual(result, 3.6666666666666665)

🎯 Challenge 4: Smallest Odd Number in List¢

πŸ‘‡ TasksΒΆ

You are given a list called nums. Your task is to find the smallest odd number in the list and store it in a variable named smallest_odd.

You can assume that the nums list contains at least one odd number.

πŸ“€ Sample Input/OutputΒΆ

nums = [3, 37, 9, 24, 5, -82]
# Expected output: 3
nums = [-12, 3, -25, -4, -8, -5, -9, -33, -90]

# YOUR CODE BEGINS

# YOUR CODE ENDS

print(smallest_odd)

🧭 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 = "smallest-odd-number-in-list"
_points = 7

tc.assertEqual(smallest_odd, -33)