In Python write a program to display the following: (a) Generate 30 random integ
ID: 3688330 • Letter: I
Question
In Python write a program to display the following:
(a) Generate 30 random integers in the range of 1 through 4 and store them in a list. Display the list.
(b) Calculate and display the average of the first 15 elements of the list.
(c) Calculate and display the average of the last 15 elements of the list.
Example:
List:
[3, 3, 3, 4, 1, 4, 3, 4, 1, 4, 3, 2, 3, 3, 2, 3, 2, 1, 2, 4, 2, 2, 2, 4, 1, 3, 4, 3, 3, 3]
Average of the first 15 elements: 2.8666666666666667
Average of the last 15 elements: 2.6
Explanation / Answer
from random import randint
List=[]
sum=0
for i in range (0,30):
List.append(randint(0,4))
print("List is:",List)
for i in range (0,15):
sum+=List[i]
print("AVERAGE OF THE FIRST 15 ELEMENTS : ",sum/15)
sum=0
for i range (15,30):
sum+=List[i]
print("AVERAGE OF THE LAST 15 ELEMENTS : ",SUM/15)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.