Lab 6.2.1 Write the code (script or on shell) for a mapping that generates a lis
ID: 3595947 • Letter: L
Question
Lab 6.2.1 Write the code (script or on shell) for a mapping that generates a list of the square roots of the numbers in a list containing 1 to 20. Sample run sss myList [1,2,3,4,5,6,7,8, 9,10, 11,12,13,14,15,16,17,18,19,20] Output [1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0, 3.1622776601683795, 3.3166247903554, 3.4641016151377544, 3.605551275463989, 3.7416573867739413, 3.872983346207417, 4.0, 4.123105625617661, 4.242640687119285, 4.358898943540674, 4.47213595499958] 55D Lab 6.2.2 Create a list of 30 random grades between 0 and 100. Write the code for a filtering that generates a list of grades that are A or B (80gradesExplanation / Answer
Lab 6.2.1
# Importing math module for sqrt
import math
# Create a List Comprehension to store 1 to 21
mylist = [i for i in range(1,21)]
# Create a Empty List
reslist= []
#Iterate the list using for loop
for element in mylist:
# Access Each element and get sqrt
x = math.sqrt(element)
# Append to List
reslist.append(x)
print(reslist)
OUTPUT
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0, 3.1622776601683795, 3.3166247903554, 3.4641016151377544, 3.605551275463989, 3.7416573867739413, 3.872983346207417, 4.0, 4.123105625617661, 4.242640687119285, 4.358898943540674, 4.47213595499958]
Lab 6.2.2:
# Importing random module
from random import *
#Creating empty list
lst=[]
#Iterating from 1 to 30 and storing random values
for x in range (1, 30):
# Using randint to get all random values between 1 and 100
lst.append(randint(1, 100))
print ("The Random Grades are: ")
print (lst)
i=0
lst1=[]
#Iterating random grades and printing
#between 80 and 100
while(i<len(lst)):
if(lst[i]>=80 and lst[i]<100):
lst1.append(lst[i])
i=i+1
print( "The Grades after grading: ")
print (lst1)
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.