Upload
wendy-newcombe
View
227
Download
0
Tags:
Embed Size (px)
Citation preview
Lecture 07 – Iterating through a list of numbers
COMPSCI 101Principles of Programming
2COMPSCI 101 - Principles of Programming
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
Learning outcomes
3COMPSCI 101 - Principles of Programming
An ordered sequence of data Data stored in memory in adjacent slots
What is a list?
data = [782, 13, 859, 6]
data: 782 13 859 6
4COMPSCI 101 - Principles of Programming
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]
Syntax for a list in Python
5COMPSCI 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
Doing something with each element
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)
6COMPSCI 101 - Principles of Programming
For each element in the list, add the element to the total
Calculating the sum of elements in a list
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
Calculate the mean of a list of numbers
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
8COMPSCI 101 - Principles of Programming
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.
Exercise
9COMPSCI 101 - Principles of Programming
Square each element in the list
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
Calculate the variance of a list of numbers
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
Calculate the standard deviation
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
Standard Deviation in a single functiondef 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
13COMPSCI 101 - Principles of Programming
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)
Summary