Lecture 8 — Exercises¶
Solutions to the problems below must be sent to Submitty for grading. A separate file must submitted for each problem. These must be submitted by 4 pm on Friday, September 30.
Upload a text file showing the output of the following code to Submitty. As usual you should try to predict the output by hand.
l1 = [ 6, 12, 13, 'hello' ] print(l1, l1[-2]) l1.append( 15 ) print( len(l1) ) print( len(l1) ) l1.pop(3) l1.sort() l1.insert(2, [14, 15] ) l1 += l1 l1 += l1 print(l1) l1.pop() l1.remove(14) print(l1)
Write a short Python program that starts with the list
values = [ 14, 10, 8, 19, 7, 13 ]
(The above statement list should be the first line of your program.) Then add code that does the following steps:
- Reads an integer, prints it (as we have done for input when using
Submitty), and appends it to the end of
- Reads another integer, prints it and inserts it at index
location 2 of
- Prints the integer at index 3 of
valuesand print the integer at index -1 of
values, both on one line.
- Prints the difference between the maximum and minimum of the
- Prints the average of the integers in
values, accurate to one decimal place. This must use the functions
- Prints the median of the numbers in
values. Since the list is even length (a fact that you are allowed to use, just for this exercise), this is the average of the two middle integers after
Here is an example of running our solution (as it would look on Submitty):
Enter a value: 15 Enter another value: 23 8 15 Difference: 16 Average: 13.6 Median: 13.5
- Reads an integer, prints it (as we have done for input when using Submitty), and appends it to the end of