Python programming help, please. (a) Write a program that generates a list L of
ID: 3722649 • Letter: P
Question
Python programming help, please.
(a) Write a program that generates a list L of 50 random numbers between 1 and 100 (use this declaration at the top of your code: from random import randint).
(b) Given the above list L that contains random numbers between 1 and 100, create a new list whose first element is how many ones are in L, whose second element is how many twos are in L, etc.
(c) Write a program that prints out the two largest and two smallest elements of the list L.
(d) Write a program that replaces each element in a list L with its square.
Explanation / Answer
Part a)
import random
L = []
for x in range(51):
L.append(random.randint(1,101))
Part b)
occurences = [0] * 100
for i in range(101):
occurences[i-1]= L.count(i)
Part c)
L.sort()
print("Largest element is: ", L[49])
print("Second largest element is: ", L[48])
print("Smallest element is:", L[0])
print("Second Smallest element is:", L[1])
Part d)
for x in range(51):
L[x-1]=L[x-1]*L[x-1]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.