6.2. For Loop Exercises#
6.2.1. Exercise 1#
Write a function that takes in a list of values and returns its sum
10
6.2.2. Exercise 2#
Write a function that takes in a list of values and returns its average
2.5
6.2.3. 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
2.5
6.2.4. Exercise 4#
Write a function that multiplies the elements of the list by two and returns them
[2, 4, 6, 8]
6.2.5. Exercise 5#
Write a pow() function that computes the power of each element in a list
[1, 4, 9, 16]
[1, 8, 27, 64]
6.2.6. Exercise 6#
Write a function to remove duplicate from a list
[1, 2, 3, 4, 5]
6.2.7. 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!
78.8
6.2.8. Exercise 8#
Write a function that simulates a coin toss
Input: number of simulation
Output: a string that concatenates the results, ex. ‘HHHTTTHTHTHT’
HHTHTTHHHT
6.2.9. 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
(0.54, 0.46)
6.2.10. 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
(0.54, 0.4600000000000001)
6.2.11. 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
student1 68.125
student2 79.6
student3 85.8
student4 78.8
student5 83.4
student6 73.8
student7 81.6
student8 90.0
student9 87.0
student10 78.2
6.2.12. 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