For Loop Exercises
Contents
For Loop Exercises#
Exercise 1#
Write a function that takes in a list of values and returns its sum
Exercise 2#
Write a function that takes in a list of values and returns its average
Exercise 3#
Write a function that takes in a list of values and returns its average. Instead of using a for loop as in ex2, use sum() and len() to calculate the average
Exercise 4#
Write a function that multiplies the elements of the list by two and returns them
Exercise 5#
Write a pow() function that computes the power of each element in a list
Exercise 6#
Write a function to remove duplicate from a list
Exercise 7#
Write a function that reads grades from an input file and calculates their average
input: filename
use: for_ex7_data1.txt
use: for_ex7_data2.txt
use: for_ex7_data3.txt
Use a list to read the values in to and then use sum and len to calculate the average.
Be careful about empty rows!
Be careful about non-numbers!
Exercise 8#
Write a function simulates a coin toss
Input: number of simulation
Output: a string that concatenates the results, ex. ‘HHHTTTHTHTHT’
Exercise 9#
Write a function that uses the output from the coin_toss function and calculates the probability of H and T Input: number of simulation Output: probability of H and T
Exercise 10#
Write a function that simulates coin_toss_probability for a given number of times and calculates the average of H and T Input: number of simulations Input: number of coin tosses Output: average probability
Exercise 11#
Write a function that reads a file in which each line has multiple student grades and calculates the student average grade Print average of each student on screen Use list comprehension to convert grades to int use: for_ex11_data.txt
Exercise 12#
Write a function that generates a given number of students with a given number of grades and saves them to a file inputs: output_filename, number_of_students, number_of_tests, test_score_range(low, high) example output: student1,93,78,82,83,65 student2,86,76,85,86,65 student3,70,98,88,80,93 student4,89,68,81,80,76 student5,99,67,100,83,68 student6,75,77,69,72,76 student7,67,93,90,92,66 student8,89,83,90,97,91 student9,92,84,75,92,92 student10,65,89,80,68,89