Lab

1.3.2. Lab#

  • Use this lab to practice the chapter’s coding skills in runnable Python code.

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

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

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

### Question 1: First runnable program
# Create a variable named course_name and store "ThinkPy" in it.
# Create a variable named chapter_number and store 1 in it.
# Print exactly two lines:
# ThinkPy Chapter 1
# Python is running.
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
course_name = "ThinkPy"
chapter_number = 1

print(f"{course_name} Chapter {chapter_number}")
print("Python is running.")
ThinkPy Chapter 1
Python is running.
### Question 2: Expressions and assignment statements
# The variables below describe a short reading plan.
# Write assignment statements that calculate total_pages and average_pages_per_day.
# Print both values. Use round(average_pages_per_day, 2) for the average.
hours = 6
pages_per_hour = 14
review_pages = 9
days = 5
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
hours = 6
pages_per_hour = 14
review_pages = 9
days = 5

total_pages = hours * pages_per_hour + review_pages
average_pages_per_day = total_pages / days

print("Total pages:", total_pages)
print("Average per day:", round(average_pages_per_day, 2))
Total pages: 93
Average per day: 18.6
### Question 3: Formatted output with built-in functions
# A weather station recorded the temperatures below for one city.
# Use len(), max(), min(), sum(), and round() to summarize the readings.
# Print exactly these two lines:
# Austin: 4 readings, high 23.0, low 19.5
# Austin,4,21.5
# Use an f-string for the first line, with max() and min() called inside the braces.
# Use print() with sep="," for the second line. The last value is the average
# temperature rounded to 1 decimal place.
city = "Austin"
temps = [21.5, 23.0, 19.5, 22.0]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
city = "Austin"
temps = [21.5, 23.0, 19.5, 22.0]

reading_count = len(temps)
average_temp = round(sum(temps) / reading_count, 1)

print(f"{city}: {reading_count} readings, high {max(temps)}, low {min(temps)}")
print(city, reading_count, average_temp, sep=",")
Austin: 4 readings, high 23.0, low 19.5
Austin,4,21.5
### Question 4: Convert input text and pack an order
# The variables below simulate text a user typed into input() for an order.
# Convert count_text to an integer named item_count.
# Convert price_text to a float named unit_price.
# Each box holds box_size items. Use // to calculate full_boxes and %
# to calculate loose_items (the items left over after filling full boxes).
# Calculate total_cost as item_count * unit_price, rounded to 2 decimal places.
# Use f-strings to print exactly these three lines:
# Full boxes: 3
# Loose items: 5
# Total cost: 109.25
count_text = "23"
price_text = "4.75"
box_size = 6
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
count_text = "23"
price_text = "4.75"
box_size = 6

item_count = int(count_text)
unit_price = float(price_text)

full_boxes = item_count // box_size
loose_items = item_count % box_size
total_cost = round(item_count * unit_price, 2)

print(f"Full boxes: {full_boxes}")
print(f"Loose items: {loose_items}")
print(f"Total cost: {total_cost}")
Full boxes: 3
Loose items: 5
Total cost: 109.25
### Question 5: Number systems and character encoding
# A simple inventory system uses product code "C" and item number 64.
# Save the decimal code for "C" in c_decimal using ord().
# Save the binary representation of c_decimal in c_binary using bin().
# Save the hexadecimal representation of item_number in item_hex using hex().
# Print c_decimal, c_binary, and item_hex.
item_number = 64
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
item_number = 64

c_decimal = ord("C")
c_binary = bin(c_decimal)
item_hex = hex(item_number)

print("C decimal:", c_decimal)
print("C binary:", c_binary)
print("Item hex:", item_hex)
C decimal: 67
C binary: 0b1000011
Item hex: 0x40