Lab

5.4.2. Lab#

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

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

  • Write and run your code directly in the coding cells.

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

### Question 1: Choose a Chart Type
# Choose chart types for three management questions.
# Use the words line, bar, and scatter.
# Print exactly:
# Trend chart: line
# Comparison chart: bar
# Relationship chart: scatter
questions = ["monthly revenue trend", "regional sales comparison", "price and demand relationship"]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
questions = ["monthly revenue trend", "regional sales comparison", "price and demand relationship"]

trend_chart = "line"
comparison_chart = "bar"
relationship_chart = "scatter"

print("Trend chart:", trend_chart)
print("Comparison chart:", comparison_chart)
print("Relationship chart:", relationship_chart)
Trend chart: line
Comparison chart: bar
Relationship chart: scatter
### Question 2: Prepare Plot Data
# Pair each month with its revenue for plotting.
# Store the pairs in plot_points.
# Print exactly:
# First point: Jan 120
# Last point: Mar 150
months = ["Jan", "Feb", "Mar"]
revenue = [120, 135, 150]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
months = ["Jan", "Feb", "Mar"]
revenue = [120, 135, 150]

plot_points = []
index = 0
for month in months:
    plot_points.append([month, revenue[index]])
    index = index + 1

print("First point:", plot_points[0][0], plot_points[0][1])
print("Last point:", plot_points[-1][0], plot_points[-1][1])
First point: Jan 120
Last point: Mar 150
### Question 3: Histogram Bins
# Count survey scores into low, medium, and high bins.
# Low is below 4, medium is 4 through 7, and high is above 7.
# Print exactly:
# Low: 2
# Medium: 4
# High: 2
scores = [2, 4, 6, 8, 7, 3, 9, 5]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
scores = [2, 4, 6, 8, 7, 3, 9, 5]

low = 0
medium = 0
high = 0
for score in scores:
    if score < 4:
        low = low + 1
    elif score <= 7:
        medium = medium + 1
    else:
        high = high + 1

print("Low:", low)
print("Medium:", medium)
print("High:", high)
Low: 2
Medium: 4
High: 2
### Question 4: Bar Chart Labels
# Build labels for a bar chart from region names and sales values.
# Store them in chart_labels.
# Print exactly:
# Chart title: Sales by Region
# First label: East: 240
regions = ["East", "West", "South"]
sales = [240, 210, 180]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
regions = ["East", "West", "South"]
sales = [240, 210, 180]

chart_title = "Sales by Region"
chart_labels = []
index = 0
for region in regions:
    chart_labels.append(region + ": " + str(sales[index]))
    index = index + 1

print("Chart title:", chart_title)
print("First label:", chart_labels[0])
Chart title: Sales by Region
First label: East: 240
### Question 5: Identify Outliers
# Find values that are far above the rest of the monthly counts.
# Treat values greater than 100 as outliers.
# Print exactly:
# Regular count: 5
# Outliers: [210]
counts = [42, 38, 45, 41, 39, 210]
### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
counts = [42, 38, 45, 41, 39, 210]

regular_count = 0
outliers = []
for count in counts:
    if count > 100:
        outliers.append(count)
    else:
        regular_count = regular_count + 1

print("Regular count:", regular_count)
print("Outliers:", outliers)
Regular count: 5
Outliers: [210]