Lecture 07 – Iterating through a list of numbers.

Slides:



Advertisements
Similar presentations
Slide 1 Insert your own content. Slide 2 Insert your own content.
Advertisements

Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
0 - 0.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
C1 Sequences and series. Write down the first 4 terms of the sequence u n+1 =u n +6, u 1 =6 6, 12, 18, 24.
Introduction to R Brody Sandel. Topics Approaching your analysis Basic structure of R Basic programming Plotting Spatial data.
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
1 Pointers and Strings Section 5.4, , Lecture 12.
Squares and Square Root WALK. Solve each problem REVIEW:
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
11 = This is the fact family. You say: 8+3=11 and 3+8=11
1 Finding Sample Variance & Standard Deviation  Given: The times, in seconds, required for a sample of students to perform a required task were:  Find:
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Python Basics: Statements Expressions Loops Strings Functions.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Lecture 2 Introduction to C Programming
The Application of Graph Criteria: Source Code  It is usually defined with the control flow graph (CFG)  Node coverage is used to execute every statement.
Lecture 03 – Sequences of data.  At the end of this lecture, students should be able to:  Define and use functions  Import functions from modules 
Rules for means Rule 1: If X is a random variable and a and b are fixed numbers, then Rule 2: If X and Y are random variables, then.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
X = =2.67.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Lecture 13 – range function, for…in loops COMPSCI 1 1 Principles of Programming.
Introduction to Software Testing Chapter 2.3 Graph Coverage for Source Code Paul Ammann & Jeff Offutt
Paul Ammann & Jeff Offutt
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Introduction to Software Testing Chapter 2.3 Graph Coverage for Source Code Paul Ammann & Jeff Offutt
Software Verification Graph Model. 2 Graph Coverage Four Structures for Modeling Software Graphs Logic Input Space Syntax Use cases Specs Design Source.
Paul Ammann & Jeff Offutt
Introduction to Software Testing Chapter 2.3 Graph Coverage for Source Code Paul Ammann & Jeff Offutt.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
Lists COMPSCI 105 S Principles of Computer Science.
1 Graph Coverage (3). Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Section 2.2 ◦ Section
Python Programing: An Introduction to Computer Science
Control Flow Graphs : The if Statement 1 if (x < y) { y = 0; x = x + 1; } else { x = y; } x >= yx < y x = y y = 0 x = x + 1 if (x < y) { y = 0;
Data Flow Coverage Criteria
Software Testing and Maintenance Lecture 3 Graph Coverage for Source Code Paul Ammann & Jeff Offutt Instructor: Hossein Momeni Mazandaran.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Chapter 9: Value-Returning Functions
COMPSCI 107 Computer Science Fundamentals
COMPSCI 107 Computer Science Fundamentals
EasyCode Foundations Vocabulary Terms.
Module 5 Working with Data
COMPSCI 107 Computer Science Fundamentals
Paul Ammann & Jeff Offutt
Installation and exercises
Paul Ammann & Jeff Offutt
14, 16, 20, 18, 22 Find range, interquartile range, variance and
Standard Deviation Calculate the mean Given a Data Set 12, 8, 7, 14, 4
Chapter 8 Arrays Objectives
Paul Ammann & Jeff Offutt
Graph Coverage for Source Code
Chapter 8 Arrays Objectives
Vocabulary Memory Cards--Sample
Paul Ammann & Jeff Offutt
Lecture 4 Psyc 300A.
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 Principles of Programming

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

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 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 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 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 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 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 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 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 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 Principles of Programming