# Lecture 07 – Iterating through a list of numbers.

## Presentation on theme: "Lecture 07 – Iterating through a list of numbers."— Presentation transcript:

Lecture 07 – Iterating through a list of numbers

At the end of this lecture, students should be able to: create a new list append elements to a list obtain the length of a list iterate through a list using a for loop perform calculations on elements of a list Examples: Calculate the standard deviation of a list of numbers Calculate the variance of a list of numbers Calculate the sum of a list of numbers Print a list of numbers 2COMPSCI 101 - Principles of Programming

An ordered sequence of data Data stored in memory in adjacent slots 3COMPSCI 101 - Principles of Programming data = [782, 13, 859, 6] data: 782138596

Creating a list my_list = [5, 2, 7, 4, 3, 8, 0, 1, 9, 6] my_list = [3.2, 5.667, 8.42, 8.0] my_list = ['Andrew', 'Luxton-Reilly'] Length of a list len(my_list) Appending to a list my_list = my_list + [3] my_list = my_list + [3, 6, 1] 4COMPSCI 101 - Principles of Programming

For loop used to access the elements of a list (a sequence of data) elements are accessed in order each element is assigned to a variable a block of instructions is executed after each assignment 5COMPSCI 101 - Principles of Programming for value in [5, 2, 7, 3]: print(value) my_list = [5, 2, 7, 4, 3, 8, 0, 1, 9, 6] for value in my_list: print(value)

For each element in the list, add the element to the total 6COMPSCI 101 - Principles of Programming def sum_of_list(my_list): """Calculate the sum of a list of numbers Arguments: my_list – a list of numbers Returns: the sum of the list >>> sum_of_list([1, 1, 0, 1, 1] 4 """ sum = 0 for value in my_list: sum = sum + value return sum my_list = [5, 2, 7, 4, 3, 8, 0, 1, 9, 6] s = sum_of_list(my_list) print(s) function definition how the function might be used

7COMPSCI 101 - Principles of Programming def mean_of_list(number_list): """Returns the mean of the elements in the list. Arguments: number_list -- a list containing 1 or more numbers Returns: the mean of the values in the list Note: The list must contain at least 1 element. >>> mean_of_list([4, 6, 3, 7]) 5.0 """ sum = sum_of_list(number_list) n = len(number_list) return sum / n

Write a function that accepts a list of numbers as an argument and returns a list in which each element is double the value of the element in the original list. 8COMPSCI 101 - Principles of Programming

9 def square_list_elements(number_list): """Returns a list containing the square of each element. Arguments: number_list -- a list containing 0 or more numbers Returns: a list containing the square of each element in the list >>> square_list_elements([1, 4, 6, 3, 1]) [1, 16, 36, 9, 1] """ square_list = [] for element in number_list: square_list = square_list + [element ** 2] return square_list

10COMPSCI 101 - Principles of Programming def variance(numbers): """Returns the variance of a list of numbers. Arguments: numbers -- a list containing 1 or more numbers >>> variance([3, 3, 3, 3]) 0.0 """ #variance = mean of squares - square of means squares = square_list_elements(numbers) mean_of_squares = mean_of_list(squares) square_of_means = mean_of_list(numbers) ** 2 return mean_of_squares - square_of_means

11COMPSCI 101 - Principles of Programming def std_dev(numbers): """Returns the standard deviation of a list of numbers. Arguments: numbers -- a list containing 1 or more numbers Returns: the standard deviation of a list of numbers >>> std_dev([3, 3, 3, 3]) 0.0 >>> std_dev([2, 4]) 1.0 """ return math.sqrt(variance(numbers))

12COMPSCI 101 - Principles of Programming def standard_deviation(numbers): """Calculate the standard deviation of a list of numbers Arguments: numbers – a list of numbers Returns: the standard deviation of the numbers """ n = len(numbers) sum = 0 for element in numbers: sum = sum + element mean = sum / n sum = 0 for element in numbers: sum = sum + element ** 2 mean_of_squares = sum / n var = mean_of_squares - mean ** 2 sd = math.sqrt(var) return sd

A list stores data as a sequence Each element of the list can be accessed We use a for loop to iterate through the contents of a list Example of list syntax: x = [1, 2, 3] y = [1, 2, 3] + [4, 5, 6] Example of for loop syntax: for element in list_of_data: print(element) 13COMPSCI 101 - Principles of Programming

Similar presentations