Homework

2.4.3. Homework#

  • Use these questions after class to check your understanding of the chapter concepts and coding skills.

  • Sign in from the account menu before you start working on the homework.

  • Complete all true/false and coding questions before you submit the homework.

  • Launch Live Code (the rocket) to write and run code in the coding cells.

  • Multiple submissions are allowed. The highest score is kept.

1. Variables

A data team stores the current file name in source_file and the number of rows in row_count. Those variable names make the script easier to read than names such as x and y.

Hide code cell source

### Solution
# True
# Descriptive variable names help readers understand what each value represents.

2. Type Conversion

A web form sends the string "42" for a response count. Python can always use that string directly in numeric addition without conversion.

Hide code cell source

### Solution
# False
# Text input normally needs int() or float() before numeric addition.

3. Numeric Operators

When a report needs whole hours and leftover minutes from a count of seconds, integer division and the remainder operator can help separate the parts.

Hide code cell source

### Solution
# True
# // gives a whole-number quotient and % gives the remainder.

4. Boolean Expressions

A dashboard rule such as score >= 80 and account_active produces a Boolean value that can represent whether a record is eligible.

Hide code cell source

### Solution
# True
# Comparisons and logical operators produce Boolean results.

5. Collection Types

A tuple is the best basic collection type when a program must repeatedly append new daily sales values to the same object.

Hide code cell source

### Solution
# False
# Lists are mutable and fit repeated appends better than tuples.
### 6. Format a report line
# Use the variables below to print exactly:
# Report: sales has 128 rows and 5 columns
report_name = "sales"
row_count = 128
column_count = 5

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
report_name = "sales"
row_count = 128
column_count = 5

print(f"Report: {report_name} has {row_count} rows and {column_count} columns")
Report: sales has 128 rows and 5 columns
### 7. Convert seconds to time parts
# Use numeric operators to split total_seconds into hours, minutes, and seconds.
# Print exactly these three lines:
# Hours: 3
# Minutes: 28
# Seconds: 20
total_seconds = 12500

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
total_seconds = 12500

hours = total_seconds // 3600
remaining_seconds = total_seconds % 3600
minutes = remaining_seconds // 60
seconds = remaining_seconds % 60

print("Hours:", hours)
print("Minutes:", minutes)
print("Seconds:", seconds)
Hours: 3
Minutes: 28
Seconds: 20
### 8. Convert text prices
# Convert the two text prices to floating-point numbers.
# Calculate total and round it to two decimal places.
# Print exactly these three lines:
# Total: 25.49
# First price type: <class 'float'>
# Total type: <class 'float'>
base_price_text = "19.99"
fee_text = "5.50"

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
base_price_text = "19.99"
fee_text = "5.50"

base_price = float(base_price_text)
fee = float(fee_text)
total = round(base_price + fee, 2)

print("Total:", total)
print("First price type:", type(base_price))
print("Total type:", type(total))
Total: 25.49
First price type: <class 'float'>
Total type: <class 'float'>
### 9. Slice a code string
# Use indexing, slicing, and len() with the code below.
# Print exactly these four lines:
# Prefix: PY
# Year: 2026
# Label: LAB
# Code length: 11
course_code = "PY-2026-LAB"

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
course_code = "PY-2026-LAB"

prefix = course_code[0:2]
year = course_code[3:7]
label = course_code[-3:]

print("Prefix:", prefix)
print("Year:", year)
print("Label:", label)
print("Code length:", len(course_code))
Prefix: PY
Year: 2026
Label: LAB
Code length: 11
### 10. Summarize a small collection
# Use the list and dictionary below.
# Create unique_names as a sorted list of unique names.
# Create bob_pair as a tuple with "Bob" and Bob's score.
# Print exactly these four lines:
# Submission count: 4
# Unique names: ['Ana', 'Bob', 'Chen']
# Bob score: 88
# Bob pair: ('Bob', 88)
names = ["Ana", "Bob", "Ana", "Chen"]
scores = {"Ana": 91, "Bob": 88, "Chen": 95}

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
names = ["Ana", "Bob", "Ana", "Chen"]
scores = {"Ana": 91, "Bob": 88, "Chen": 95}

unique_names = sorted(set(names))
bob_pair = ("Bob", scores["Bob"])

print("Submission count:", len(names))
print("Unique names:", unique_names)
print("Bob score:", scores["Bob"])
print("Bob pair:", bob_pair)
Submission count: 4
Unique names: ['Ana', 'Bob', 'Chen']
Bob score: 88
Bob pair: ('Bob', 88)