Question: Goals In this lab assignment, students will demonstrate the ability to: Create and use nested lists Create and use tuples Use list comprehension to create
Goals
In this lab assignment, students will demonstrate the ability to:
Create and use nested lists
Create and use tuples
Use list comprehension to create lists
Install and use the matplotlib package to plot list data
Instructions
In this lab, you will demonstrate your mastery of lists and tuples as well as your understanding of the Python package matplotlib.
Follow the instructions in each problem and submit the specified files. All problems will require that Python code be submitted as well as screenshots that prove the programs have been executed in PyCharm.
All problems will consist of programs that you create from scratch that meets the problem specification.
Problems
Problem 1 Nested Lists
This problem tests your understanding of nested lists.
Create a file named Lab06P1.py.
A college instructor has three students, Ashley, Barb, and Carl, who are taking her class. Each student has taken either 3 or 4 tests which have been graded.
Write a Python program that performs the following steps. Label each block of code that performs these steps with an appropriate comment (e.g. # Step a):
Ask the user to enter 3 test scores for Ashley. Store the scores in a list named a_list. Display the list.
NOTE: The test scores could be entered as floating point numbers.
Ask the user to enter 4 test scores for Barb. Store the scores in a list named b_list. Display the list.
Ask the user to enter 3 test scores for Carl. Store the scores in a list named c_list. Display the list.
Use the following code to create a copy of each list and construct a list of lists from them:
all_scores = [a_list[:], b_list[:], c_list[:]]
Note: We want the elements of all_scores to be a copy of the three score lists but not the three score lists themselves. Therefore, the following code does not generate what we want:
all_scores = [a_list, b_list, c_list] # DO NOT DO THIS!!
After creating the all_scores list, print that list.
The instructor wants to give extra credit to all these students by adding 3 points to each test score. Use a nested set of for loops to add 3 extra points to every score in all_scores. Display all_scores.
NOTE: In order to update the inner elements of a nested list, you must have the for loops generate indexes. With indexes you can then write into each inner element.
Create a new list called max_scores that is the maximum of the extra credit test scores of the three students. Use the data from the updated all_scores to find the maximum.
You do NOT need nested for loops for this step.
Use a single for loop to access each inner list, then find the maximum of the inner list. Append that maximum value to the max_scores list.
Display max_scores.
Display the original lists that were created in Steps a, b, and c. This output will demonstrate the original lists were not altered when the all_scores list was updated.
Sample output:
Please enter Ashley's scores one by one.
Enter a score: 100
Enter a score: 99
Enter a score: 95
Ashley's scores: [100.0, 99.0, 95.0]
Please enter Barb's scores one by one.
Enter a score: 85
Enter a score: 89
Enter a score: 92
Enter a score: 90
Barb's scores: [85.0, 89.0, 92.0, 90.0]
Please enter Carl's scores one by one.
Enter a score: 68
Enter a score: 87
Enter a score: 75
Carl's scores: [68.0, 87.0, 75.0]
All scores: [[100.0, 99.0, 95.0], [85.0, 89.0, 92.0, 90.0], [68.0, 87.0, 75.0]]
All scores after extra point: [[103.0, 102.0, 98.0], [88.0, 92.0, 95.0, 93.0], [71.0, 90.0, 78.0]]
Maximum scores: [103.0, 95.0, 90.0]
Original Scores
Ashley's scores: [100.0, 99.0, 95.0]
Barb's scores: [85.0, 89.0, 92.0, 90.0]
Carl's scores: [68.0, 87.0, 75.0]
Run this program and take a screenshot with the results. Name the screenshot Lab06P1-ouput.jpg.
Submit both files, Lab06P1.py and Lab06P1-output.jpg, to Blackboard for credit.
Problem 2 Tuples
This program tests your understanding of tuples.
Create a file named Lab06P2.py. Write a Python program to do the following:
Ask the user to enter the number of values that should be in the tuple.
Ask the user for the start and end values to use when generating a random number.
Use a for loop and a random integer generator to generate the number random integers indicated by the user in the range that was indicated. Store the random integers in a tuple. Display the tuple as shown in the sample output below.
Hint: You may want to store the random integers in a list first and then convert the list to a tuple.
Create a new tuple. Copy the first three elements of the tuple in part (c) to this tuple. Display this tuple as shown below.
Create a new tuple. Copy the last three elements of the tuple in part (c) to this tuple. Display this tuple as shown below.
Concatenate the two tuples in part (d) and part (e). Display the concatenated tuple as shown below.
Sort the concatenated tuple and ensure your result is a tuple. Display the sorted tuple as shown below.
Sample output:
Step a:
How many values to put in the tuple? 7
Step b:
What is the start of the range? 3
What is the end of the range? 20
Step c: Tuple of 7 random numbers: (17, 8, 20, 12, 5, 10, 17)
Step d: Tuple of first 3 numbers: (17, 8, 20)
Step e: Tuple of last 3 numbers: (5, 10, 17)
Step f: Two tuples concatenated: (17, 8, 20, 5, 10, 17)
Step g: Two tuples concatenated and sorted: (5, 8, 10, 17, 17, 20)
As shown in the sample output, your output should include which step is being displayed.
Run this program and take a screenshot with the results. Name the screenshot Lab06P2-ouput.jpg.
Submit both files, Lab06P2.py and Lab06P2-output.jpg, to Blackboard for credit.
Problem 3 List Comprehensions
This problem tests your understanding of list comprehensions.
Create a file named Lab06P3.py. Write a Python program that performs the following steps. In your program output, make sure to indicate which step is being demonstrated with your output (see sample output):
Start your program (after the comment header) with this predefined list:
list1 = [3, 1, 4, 1, 5]
list2 = [2, 5]
Write a one-line list comprehension that is equivalent to the following code and print list3:
list3 = []
for num in range(5):
list3.append(num * 3 - 2)
Write a one-line list comprehension that is equivalent to the following code and print list4:
list4 = []
for i in range(8):
for j in range(4):
if i % 2 == 1:
list4.append(i - j)
Write a one-line list comprehension that is equivalent to the following code and print list5:
list5 = []
for i in list1:
list5.append(i ** i)
Write a list comprehension with list1 only as an input sequence to generate this list:
[10, 4, 13, 4, 16]
Write a list comprehension with both list1 and list2 as input sequences to generate this list:
[1, -2, -1, -4, 2, -1, -1, -4, 3, 0]
Write a list comprehension with both list1 and list2 as input sequences to generate this list:
['3&2', '3&5', '1&2', '1&5', '4&2', '4&5', '1&2', '1&5', '5&2', '5&5']
Sample output:
Step b: [-2, 1, 4, 7, 10]
Step c: [1, 0, -1, -2, 3, 2, 1, 0, 5, 4, 3, 2, 7, 6, 5, 4]
Step d: [27, 1, 256, 1, 3125]
Step e: [10, 4, 13, 4, 16]
Step f: [1, -2, -1, -4, 2, -1, -1, -4, 3, 0]
Step g: ['3&2', '3&5', '1&2', '1&5', '4&2', '4&5', '1&2', '1&5', '5&2', '5&5']
Run this program and take a screenshot with the results. Name the screenshot Lab06P3-ouput.jpg.
Submit both files, Lab06P3.py and Lab06P3-output.jpg, to Blackboard for credit.
Problem 4 Matplotlib
This problem will test your ability to use the Python module matplotlib. For this particular problem, review the part of Section 7.11 labeled Plotting a Line Graph. In particular, review Programs 7-20 through 7-24 for examples of using matplotlib to create a line graph.
Create a file named Lab06P4.py. Write a program that helps a user generate a line graph graphic:
As discussed in the book, you will need to use the Python utility pip to install matplotlib. Follow the instructions in the book for details.
After your program comment header, add the appropriate line for importing the module matplotlib that makes it convenient to work with the pyplot portion of the module:
import matplotlib.pyplot as plt
Your program should first ask the user to enter a title for a line graph.
The program should also ask the user for labels for the x-axis and y-axis.
Then your program should ask the user for 5 labels and 5 values using a for loop. The labels will be used to label the tick marks on the x-axis. The values will be used to plot points on the y-axis.
After collecting the user's input into a list of labels and a list of values, use the appropriate pyplot functions to do the following:
Create the line graph.
Add a title for the chart.
Add labels for the x-axis and y-axis.
Add the tick mark labels for the x-axis.
Add grid lines.
Display the line graph.
The program terminates after the user closes the window displaying the line graph.
Sample output:
Enter the line graph title: Daily Temperatures
Enter the label for the x-axis: Day
Enter the label for the y-axis: Temperature
Enter the name for tick 1: Mon
Enter the value for tick 1: 65
Enter the name for tick 2: Tue
Enter the value for tick 2: 68.3
Enter the name for tick 3: Wed
Enter the value for tick 3: 69.8
Enter the name for tick 4: Thu
Enter the value for tick 4: 72
Enter the name for tick 5: Fri
Enter the value for tick 5: 70.4
Run this program. Enter any test data you would like. When your program displays the line graph, use the save button to save the line graph to a file named Lab06P4-output.png.
DO NOT DO A SCREENSHOT, but instead demonstrate that you understand you can use the save button in the line graph window to save the picture to the file system.
Submit both files, Lab06P4.py and Lab06P4-output.png, to Blackboard for credit.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
