You have two options for completing today’s lab:

Option A: Do Lab 1 (Python) and Lab 2 (JavaScript).
Option B: Do Lab 3 (Python, extended project) instead.

You can complete the Python lab by simply running your code and getting your outputs in the Jupyter notebook. For JavaScript, you’ll have to open the web console from Developer Tools (ctrl + shift + p -> Developer: Toggle developer tools).

Lab 1: Data Abstractions in Python

In this lab, you’ll practice using data abstractions (like lists and strings) and other data types (integers, floats, booleans, tuples).

# 1. Working with numbers
# Define an integer and a float. Print them both.
# TODO: Replace the ___ with your code.

integer_value = _
float_value = _
print(integer_value)
print(float_value)
# 2. Booleans
# Create two boolean variables: one True and one False.
# Print them.

a = _
b = _
print(a, b)
# 3. Lists
# Create a list of at least 5 integers (your choice).
# Print the first and last element using indexing.

numbers = _
print("First:", _)
print("Last:", _)
# 4. Tuples
# Tuples are like lists but immutable (cannot be changed).
# Create a tuple with (your_name, your_age, your_favorite_number).
# Print each element.

info = _
print(info[0])  # name
print(info[1])  # age
print(info[2])  # favorite number
# 5. Strings and lists together
# Strings are sequences of characters (like lists).
# Create a string variable with a word.
# Print the second character and the last character.

word = _
print("Second character:", _)
print("Last character:", _)
# 6. Abstraction in action
# Create a list of test scores (at least 4 numbers).
# Use a loop to compute the average instead of adding manually.

scores = _
total = 0
for score in scores:
    total += score

average = total / len(scores)
print("Average score:", average)

Things to think about

  • How does using a list make it easier to calculate an average than writing separate variables for each score?
  • Why might tuples be useful if you want to store data that shouldn’t change?

Lab 2: Data Abstractions in JavaScript

Now let’s explore data abstractions in JavaScript using arrays, strings, numbers, and booleans.

// 1. Numbers
// Define a whole number (integer) and a decimal (float). Print them.

let integerValue = _;
let floatValue = _;

console.log(integerValue);
console.log(floatValue);
// 2. Booleans
// Create two boolean variables: one true and one false. Print them.

let a = _;
let b = _;

console.log(a, b);
// 3. Arrays
// Create an array with at least 5 integers.
// Print the first and last element using indexing.

let numbers = _;

console.log("First:", _);
console.log("Last:", _);
// 4. Strings
// Strings are like arrays of characters.
// Create a string variable with a word.
// Print the second and last character.

let word = _;

console.log("Second character:", _);
console.log("Last character:", _);
// 6. Abstraction in action
// Create an array of test scores (at least 4 numbers).
// Use a loop to compute the average.

let scores = _;
let total = 0;

for (let score of scores) {
    total += score;
}

let average = total / scores.length;
console.log("Average score:", average);

Lab 3: Python Gradebook

You are building a small “gradebook” program to track students’ names, test scores, and averages. You can run the cells one by one as you progress (Jupyter saves variables as you run those code blocks), so only move on to another cell once you finis the one before it.

# 1. Represent a student as a dictionary with keys:
# "name"(string), "scores"(list of integers), "passed"(boolean).
# TODO: Replace ___ with your code.

student = {
    "name": _,
    "scores": _,
    "passed": _
}

print(student)
# 2. Print the student's name, first score, and last score.
# Dictionary elements are accessed by `dictionary[key]`. 
# Do your research!

print("Name:", _)
print("First score:", _)
print("Last score:", _)
# 3. Write a function compute_average(scores) that returns the average.
# Use it to calculate this student's average.

def compute_average(scores):
    _

average = compute_average(student["scores"])
print("Average:", average)
# 4. Use the average to set the student's "passed" field.
# Passing is average >= 70.

student["passed"] = _
print("Updated student:", student)
# 5. Create a list of 3 students (each as a dictionary).
# Use loops to:
#   - Print each student’s name and average
#   - Count how many passed

students = [
    {"name": "Alice", "scores": [85, 90, 78], "passed": None},
    {"name": "Bob", "scores": [60, 65, 58], "passed": None},
    {"name": "Charlie", "scores": [95, 100, 92], "passed": None}
]

for s in students:
    _
    print(s["name"], "average:", avg, "passed:", s["passed"])