Lists and Loops. defining lists lists as queues and stacks inserting and removing membership and ordering lists

Size: px
Start display at page:

Download "Lists and Loops. defining lists lists as queues and stacks inserting and removing membership and ordering lists"

Transcription

1 Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule MCS 507 Lecture 4 Mathematical, Statistical and Scientific Software Jan Verschelde, 5 September 2012 Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

2 Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

3 lists via range With range we make a list of consecutive integers: = range(5,11) >>> type(l) <type list > [5, 6, 7, 8, 9, 10] Note: range(a,b) terminates at b - 1. The default start of range is 0: >> range(5) [0, 1, 2, 3, 4] Instead of 1 as default step, use 3: >>> range(4,4+5*3,3) [4, 7, 10, 13, 16] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

4 selecting and assigning = range(5,10) >>> len(l) 5 >>> K = L; K [5, 6, 7, 8, 9] We can assign lists as any variable. Indexing starts at 0: [2] 7 [2] = 17 Assigning to L also changes K: [5, 6, 17, 8, 9] >>> K [5, 6, 17, 8, 9] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

5 taking slices of lists Generating 7 integers starting at 4 with step size 5: = range(4,4+7*5,5) [4, 9, 14, 19, 24, 29, 34] A slice: [2:5] [14, 19, 24] The first and last 3 numbers are selected as [:3] [4, 9, 14] [-3:] [24, 29, 34] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

6 Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

7 implementing a queue In a queue with first-come first-served protocol, we append items to the end and pop the front. = [].append( a ) [ a ].append( b ); L [ a, b ] >>> first = L.pop(0) >>> first a [ b ] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

8 implementing a stack In a stack (e.g.: a pile of books), we push to the front and also pop the front. = [].insert(0, a ); L [ a ].insert(0, b ); L [ b, a ] >>> top = L.pop(0) >>> top b [ a ] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

9 Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

10 the insert method The first argument of insert is the index of the new element in the list. After L.insert(k,i) we have: L[k] == i. = range(0,5); L [0, 1, 2, 3, 4].insert(2, a ) [0, 1, a, 2, 3, 4].insert(len(L), b ) [0, 1, a, 2, 3, 4, b ].insert(1000, c ); L [0, 1, a, 2, 3, 4, b, c ] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

11 popping and removing Observe the difference between pop and remove: = range(0,5); L [0, 1, 2, 3, 4].remove(0) [1, 2, 3, 4].pop(0) 1 Also their arguments have different type:.pop(2) 4 [2, 3].remove(3); L [2] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

12 deleting elements >>> x = a ; y = b ; L = [x,y] [ a, b ] >>> del L[0] [ b ] >>> x a Although the first element a of the list is deleted, we can still refer to that a via the name x Difference with remove? The del applies to any variable. Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

13 memory locations addresses and values >>> x = 3 >>> id(x) The machine view: address of x 3 value of x In Python, the name x refers to the object 3. x name reference 3 object Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

14 visualizing lists from scitools.lumpy import Lumpy lumpy = Lumpy() lumpy.make_reference() L0 = [1, 4, 3] L1 = L0 L2 = L1[:-1] L1[0] = 100 lumpy.object_diagram() Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

15 Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

16 testing membership The in operator returns True or False: = range(5,10); L [5, 6, 7, 8, 9] >>> 6 in L True >>> 3 in L False If e in L, then L[L.index(e)] == e:.index(6) 1 [1] 6 Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

17 ordering lists = range(1,11) >>> import random >>> random.shuffle(l) [3, 6, 9, 10, 7, 8, 1, 4, 2, 5] >>> max(l); min(l) 10 1.reverse() [5, 2, 4, 1, 8, 7, 10, 9, 6, 3].sort() [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

18 Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

19 for loops Computing the sum of all elements in a list L: e L e. s = 0 for e in L: s = s + e print s The indentation is important! To see how s evolves: s = 0 for e in L: s = s + e print s The second code fragment prints as many times as len(l), while the first code prints only once. Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

20 while loops The for loop is appropriate when the number of steps is fixed and known in advance. A while loop is more flexible. s = 0 for e in L: s = s + e is equivalent to s = 0; i = 0 while (i < len(l)): s = s + L[i] i = i + 1 The while loop continues as long as the condition is True. Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

21 Lists and Loops 1 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 2 Loops in Python for and while loops the composite trapezoidal rule Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

22 the composite trapezoidal rule To approximate the integral of a function f(x) over [a, b], the trapezoidal rule is b a f(x)dx 1 (f(a) + f(b))(b a). 2 Geometrically, we approximate the area under f(x) for x [a, b] by the area of a trapezium, with base [a, b] and heights f(a) and f(b). Dividing [a, b] into n intervals of length h = (b a)/n, denote x k = a + kh, x 0 = a, x n = b, the composite trapezoidal rule is b a f(x)dx h n 1 2 (f(a) + f(b)) + h f(a + kh). k=1 Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

23 evaluating functions At the command prompt $: $ python testevalfun.py give a function in x : exp(x)*sin(x) give a value for x : 1 formula exp(x)*sin(x) at 1 is The script testevalfun.py uses StringFunction of the module scitools. Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

24 the script testevalfun.py # L-4 MCS 507 Wed 5 Sep 2012 : testevalfun.py from scitools.stringfunction \ import StringFunction formula = raw_input( give a function in x : ) f = StringFunction(formula) v = input( give a value for x : ) y = f(v) print formula, formula, at, v, is, y Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

25 running comptraprule.py $ python comptraprule.py give a function in x : exp(x)*sin(x) what is left end of [a,b]? 0 what is right end of [a,b]? 2 give number of evaluations : 100 approximate integral : $ python comptraprule.py give a function in x : exp(x)*sin(x) what is left end of [a,b]? 0 what is right end of [a,b]? 2 give number of evaluations : 1000 approximate integral : $ python comptraprule.py give a function in x : exp(x)*sin(x) what is left end of [a,b]? 0 what is right end of [a,b]? 2 give number of evaluations : approximate integral : Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

26 the script comptraprule.py from scitools.stringfunction \ import StringFunction formula = raw_input( give a function in x : ) f = StringFunction(formula) a = input( what is left end of [a,b]? ) b = input( what is right end of [a,b]? ) n = input( give number of evaluations : ) h = (b-a)/float(n) s = (f(a) + f(b))/2 for i in range(1,n): s = s + f(a+i*h) s = s*h print approximate integral :, s Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

27 using scipy Romberg integration extrapolates on the composite trapezoidal rule, available in the module integrate of the package scipy. >>> from scipy.integrate import romberg >>> from math import exp, sin >>> romberg(lambda x: exp(x)*sin(x),0,2) With lambda we quickly define a function. Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

28 Summary + Exercises We started chapter 2 of the text book. Exercises: 1 Type L = [3, 9]; K = L; K[1] = 6 in an interactive Python session. Sketch (or use Lumpy) the relations between K and L. 2 Write a script that prompts the user for a string definition of a function f (in x), the end points of an interval [a, b] and the number n of equidistant samples in [a, b]. The script prints the maximum and minimum value of f at the n equidistant samples in [a, b]. 3 Replace the for loop in comptraprule.py by an equivalent while loop. Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

29 more exercises 4 Write a script that asks the user ( for ) two integers n and k. n n! The script prints the value of = k k!(n k)!. 5 Modify comptraprule.py so that instead of n the script prompts the user for a tolerance ǫ (e.g.: 0.001). Starting with n = 2, the number of evaluations n doubles in every step till the difference between two consecutive approximations for the integral < ǫ. The first homework collection is on Monday 10 September, at 10AM. Bring to class your answers to exercise 3 of Lecture 1; exercises 1, 2 of Lecture 2; and exercises 1, 3 of Lecture 3. Scientific Software (MCS 507) Lists and Loops 5 Sep / 29

Lists and Loops. browse Python docs and interactive help

Lists and Loops. browse Python docs and interactive help Lists and Loops 1 Help in Python browse Python docs and interactive help 2 Lists in Python defining lists lists as queues and stacks inserting and removing membership and ordering lists 3 Loops in Python

More information

Numerical Integration

Numerical Integration Numerical Integration 1 Functions using Functions functions as arguments of other functions the one-line if-else statement functions returning multiple values 2 Constructing Integration Rules with sympy

More information

List Comprehensions and Simulations

List Comprehensions and Simulations List Comprehensions and Simulations 1 List Comprehensions examples in the Python shell zipping, filtering, and reducing 2 Monte Carlo Simulations testing the normal distribution the Mean Time Between Failures

More information

Defining Functions. turning expressions into functions. writing a function definition defining and using modules

Defining Functions. turning expressions into functions. writing a function definition defining and using modules Defining Functions 1 Lambda Functions turning expressions into functions 2 Functions and Modules writing a function definition defining and using modules 3 Computing Series Developments exploring an example

More information

Outline. half adders adder circuits. the while loop the for loop. Euclid s algorithm approximating π

Outline. half adders adder circuits. the while loop the for loop. Euclid s algorithm approximating π Outline 1 Digital Systems half adders adder circuits 2 Looping Constructs the while loop the for loop 3 Designing Loops Euclid s algorithm approximating π 4 Summary + Assignments MCS 260 Lecture 11 Introduction

More information

differentiation techniques

differentiation techniques differentiation techniques 1 Callable Objects delayed execution of stored code 2 Numerical and Symbolic Differentiation numerical approximations for the derivative storing common code in a parent class

More information

Outline. tallying the votes global and local variables call by value or call by reference. of variable length using keywords for optional arguments

Outline. tallying the votes global and local variables call by value or call by reference. of variable length using keywords for optional arguments Outline 1 Histograms tallying the votes global and local variables call by value or call by reference 2 Arguments of Functions of variable length using keywords for optional arguments 3 Functions using

More information

Interactive Computing

Interactive Computing Interactive Computing 1 Input/Output and Complex Arithmetic interactive Python scripts complex arithmetic 2 Python Coding Style and pylint coding style static code checking with pylint 3 Programming with

More information

turning expressions into functions symbolic substitution, series, and lambdify

turning expressions into functions symbolic substitution, series, and lambdify Defining Functions 1 Lambda Functions turning expressions into functions symbolic substitution, series, and lambdify 2 Functions and Modules writing a function definition defining and using modules where

More information

Floating-Point Arithmetic

Floating-Point Arithmetic Floating-Point Arithmetic 1 Numerical Analysis a definition sources of error 2 Floating-Point Numbers floating-point representation of a real number machine precision 3 Floating-Point Arithmetic adding

More information

Tuples and Nested Lists

Tuples and Nested Lists stored in hash 1 2 stored in hash 3 and 4 MCS 507 Lecture 6 Mathematical, Statistical and Scientific Software Jan Verschelde, 2 September 2011 and stored in hash 1 2 stored in hash 3 4 stored in hash tuples

More information

User Interfaces. MCS 507 Lecture 11 Mathematical, Statistical and Scientific Software Jan Verschelde, 16 September Command Line Interfaces

User Interfaces. MCS 507 Lecture 11 Mathematical, Statistical and Scientific Software Jan Verschelde, 16 September Command Line Interfaces User 1 2 MCS 507 Lecture 11 Mathematical, Statistical and Scientific Software Jan Verschelde, 16 September 2011 User 1 2 command line interfaces Many programs run without dialogue with user, as $ executable

More information

User Interfaces. getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy

User Interfaces. getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy User Interfaces 1 Command Line Interfaces getting arguments of the command line a command line interface to store points fitting points with polyfit of numpy 2 Encapsulation by Object Oriented Programming

More information

Computational Programming with Python

Computational Programming with Python Numerical Analysis, Lund University, 2017 1 Computational Programming with Python Lecture 1: First steps - A bit of everything. Numerical Analysis, Lund University Lecturer: Claus Führer, Alexandros Sopasakis

More information

Physics 514 Basic Python Intro

Physics 514 Basic Python Intro Physics 514 Basic Python Intro Emanuel Gull September 8, 2014 1 Python Introduction Download and install python. On Linux this will be done with apt-get, evince, portage, yast, or any other package manager.

More information

callback, iterators, and generators

callback, iterators, and generators callback, iterators, and generators 1 Adding a Callback Function a function for Newton s method a function of the user to process results 2 A Newton Iterator defining a counter class refactoring the Newton

More information

Review for Midterm Exam

Review for Midterm Exam Review for Midterm Exam 1 Policies and Overview midterm exam policies overview of problems, algorithms, data structures overview of discrete mathematics 2 Sample Questions on the cost functions of algorithms

More information

High Level Parallel Processing

High Level Parallel Processing High Level Parallel Processing 1 GPU computing with Maple enabling CUDA in Maple 15 stochastic processes and Markov chains 2 Multiprocessing in Python scripting in computational science the multiprocessing

More information

Outline. general information policies for the final exam

Outline. general information policies for the final exam Outline 1 final exam on Tuesday 5 May 2015, at 8AM, in BSB 337 general information policies for the final exam 2 some example questions strings, lists, dictionaries scope of variables in functions working

More information

Interval Arithmetic. MCS 507 Lecture 29 Mathematical, Statistical and Scientific Software Jan Verschelde, 28 October 2011

Interval Arithmetic. MCS 507 Lecture 29 Mathematical, Statistical and Scientific Software Jan Verschelde, 28 October 2011 Naive Arithmetic 1 2 Naive 3 MCS 507 Lecture 29 Mathematical, Statistical and Scientific Software Jan Verschelde, 28 October 2011 Naive Arithmetic 1 2 Naive 3 an expression Naive Problem: Evaluate f(x,

More information

OOP and Scripting in Python Advanced Features

OOP and Scripting in Python Advanced Features OOP and Scripting in Python Advanced Features Giuliano Armano Emanuele Tamponi Advanced Features Structure of a Python Script More on Defining Functions Default Argument Values Keyword Arguments Arbitrary

More information

Root Finding Methods. sympy and Sage. MCS 507 Lecture 13 Mathematical, Statistical and Scientific Software Jan Verschelde, 21 September 2011

Root Finding Methods. sympy and Sage. MCS 507 Lecture 13 Mathematical, Statistical and Scientific Software Jan Verschelde, 21 September 2011 wrap Root Finding Methods 1 2 wrap MCS 507 Lecture 13 Mathematical, Statistical and Scientific Software Jan Verschelde, 21 September 2011 Root Finding Methods 1 wrap 2 wrap wrap octave-3.4.0:1> p = [1,0,2,-1]

More information

Divide and Conquer. playing divide and conquer. searching in a sorted list membership in a sorted list

Divide and Conquer. playing divide and conquer. searching in a sorted list membership in a sorted list Divide and Conquer 1 Guessing a Secret playing divide and conquer 2 Binary Search searching in a sorted list membership in a sorted list 3 Bisection Search inverting a sampled function bisection search

More information

Python Lists. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 5, 2011

Python Lists. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 5, 2011 Python Lists Stéphane Vialette LIGM, Université Paris-Est Marne-la-Vallée October 5, 2011 Stéphane Vialette (LIGM UPEMLV) Python Lists October 5, 2011 1 / 31 Outline 1 Introduction 2 Methods 3 Lists as

More information

implementing the breadth-first search algorithm implementing the depth-first search algorithm

implementing the breadth-first search algorithm implementing the depth-first search algorithm Graph Traversals 1 Graph Traversals representing graphs adjacency matrices and adjacency lists 2 Implementing the Breadth-First and Depth-First Search Algorithms implementing the breadth-first search algorithm

More information

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists Module 04: Lists Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10 1 Consider the string method split >>> name = "Harry James Potter" >>> name.split() ['Harry',

More information

operator overloading algorithmic differentiation the class DifferentialNumber operator overloading

operator overloading algorithmic differentiation the class DifferentialNumber operator overloading operator overloading 1 Computing with Differential Numbers algorithmic differentiation the class DifferentialNumber operator overloading 2 Computing with Double Doubles the class DoubleDouble defining

More information

a name refers to an object side effect of assigning composite objects

a name refers to an object side effect of assigning composite objects Outline 1 Formal Languages syntax and semantics Backus-Naur Form 2 Strings, Lists, and Tuples composite data types building data structures the % operator 3 Shared References a name refers to an object

More information

Problem 1 (a): List Operations

Problem 1 (a): List Operations Problem 1 (a): List Operations Task 1: Create a list, L1 = [1, 2, 3,.. N] Suppose we want the list to have the elements 1, 2, 10 range(n) creates the list from 0 to N-1 But we want the list to start from

More information

MATLAB Lecture 4. Programming in MATLAB

MATLAB Lecture 4. Programming in MATLAB MATLAB Lecture 4. Programming in MATLAB In this lecture we will see how to write scripts and functions. Scripts are sequences of MATLAB statements stored in a file. Using conditional statements (if-then-else)

More information

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements Welcome to MCS 360 1 About the Course content expectations 2 our first C++ program using g++ input and output streams the namespace std 3 Greatest Common Divisor Euclid s algorithm the while and do-while

More information

Dynamic Arrays and Amortized Analysis

Dynamic Arrays and Amortized Analysis Yufei Tao ITEE University of Queensland As mentioned earlier, one drawback of arrays is that their lengths are fixed. This makes it difficult when you want to use an array to store a set that may continuously

More information

You Need an Interpreter! Comp Spring /28/08 L10 - An Interpreter

You Need an Interpreter! Comp Spring /28/08 L10 - An Interpreter You Need an Interpreter! Closing the GAP Thus far, we ve been struggling to speak to computers in their language, maybe its time we spoke to them in ours How high can we rasie the level of discourse? We

More information

Programming Training. Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving

Programming Training. Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving Functions in Python A Python program is a sequence of a functions which can be

More information

Chapter 2. Python Programming for Physicists. Soon-Hyung Yook. March 31, Soon-Hyung Yook Chapter 2 March 31, / 52

Chapter 2. Python Programming for Physicists. Soon-Hyung Yook. March 31, Soon-Hyung Yook Chapter 2 March 31, / 52 Chapter 2 Python Programming for Physicists Soon-Hyung Yook March 31, 2017 Soon-Hyung Yook Chapter 2 March 31, 2017 1 / 52 Table of Contents I 1 Getting Started 2 Basic Programming Variables and Assignments

More information

CSC148 Week 2. Larry Zhang

CSC148 Week 2. Larry Zhang CSC148 Week 2 Larry Zhang 1 Admin Discussion board is up (link on the course website). 2 Outline for this week Abstract Data Type Stack Queue 3 Abstract Data Type (ADT) 4 What is an abstract data type

More information

LECTURE 19. Numerical and Scientific Packages

LECTURE 19. Numerical and Scientific Packages LECTURE 19 Numerical and Scientific Packages NUMERICAL AND SCIENTIFIC APPLICATIONS As you might expect, there are a number of third-party packages available for numerical and scientific computing that

More information

Math 250A (Fall 2009) - Lab I: Estimate Integrals Numerically with Matlab. Due Date: Monday, September 21, INSTRUCTIONS

Math 250A (Fall 2009) - Lab I: Estimate Integrals Numerically with Matlab. Due Date: Monday, September 21, INSTRUCTIONS Math 250A (Fall 2009) - Lab I: Estimate Integrals Numerically with Matlab Due Date: Monday, September 21, 2009 4:30 PM 1. INSTRUCTIONS The primary purpose of this lab is to understand how go about numerically

More information

Functions 2/1/2017. Exercises. Exercises. Exercises. and the following mathematical appetizer is about. Functions. Functions

Functions 2/1/2017. Exercises. Exercises. Exercises. and the following mathematical appetizer is about. Functions. Functions Exercises Question 1: Given a set A = {x, y, z} and a set B = {1, 2, 3, 4}, what is the value of 2 A 2 B? Answer: 2 A 2 B = 2 A 2 B = 2 A 2 B = 8 16 = 128 Exercises Question 2: Is it true for all sets

More information

Exercises C-Programming

Exercises C-Programming Exercises C-Programming Claude Fuhrer (claude.fuhrer@bfh.ch) 0 November 016 Contents 1 Serie 1 1 Min function.................................. Triangle surface 1............................... 3 Triangle

More information

Branching and Enumeration

Branching and Enumeration Branching and Enumeration 1 Booleans and Branching computing logical expressions computing truth tables with Sage if, else, and elif 2 Timing Python Code try-except costs more than if-else 3 Recursive

More information

Numerical Methods. Centre for Mathematical Sciences Lund University. Spring 2015

Numerical Methods. Centre for Mathematical Sciences Lund University. Spring 2015 Numerical Methods Claus Führer Alexandros Sopasakis Centre for Mathematical Sciences Lund University Spring 2015 Preface These notes serve as a skeleton for the course. They document together with the

More information

lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions Outline 1 Guessing Secrets functions returning functions oracles and trapdoor functions 2 anonymous functions lambda forms map(), reduce(), filter(), eval(), and apply() estimating π with list comprehensions

More information

Homework: Study 6.1 # 1, 5, 7, 13, 25, 19; 3, 17, 27, 53

Homework: Study 6.1 # 1, 5, 7, 13, 25, 19; 3, 17, 27, 53 January, 7 Goals:. Remember that the area under a curve is the sum of the areas of an infinite number of rectangles. Understand the approach to finding the area between curves.. Be able to identify the

More information

Core Mathematics 1 Graphs of Functions

Core Mathematics 1 Graphs of Functions Regent College Maths Department Core Mathematics 1 Graphs of Functions Graphs of Functions September 2011 C1 Note Graphs of functions; sketching curves defined by simple equations. Here are some curves

More information

1.5 Part - 2 Inverse Relations and Inverse Functions

1.5 Part - 2 Inverse Relations and Inverse Functions 1.5 Part - 2 Inverse Relations and Inverse Functions What happens when we reverse the coordinates of all the ordered pairs in a relation? We obviously get another relation, but does it have any similarities

More information

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections) CS 61A Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections) INSTRUCTIONS You have 3 hours to complete the exam. The exam is open book and open notes. You may not use a

More information

LECTURE 22. Numerical and Scientific Packages

LECTURE 22. Numerical and Scientific Packages LECTURE 22 Numerical and Scientific Packages NUMERIC AND SCIENTIFIC APPLICATIONS As you might expect, there are a number of third-party packages available for numerical and scientific computing that extend

More information

Welcome to MCS 275. Course Content Prerequisites & Expectations. Scripting in Python from OOP to LAMP example: Factorization in Primes

Welcome to MCS 275. Course Content Prerequisites & Expectations. Scripting in Python from OOP to LAMP example: Factorization in Primes Welcome to MCS 275 1 About the Course Course Content Prerequisites & Expectations 2 Introduction to Programming Scripting in Python from OOP to LAMP example: Factorization in Primes 3 Summary MCS 275 Lecture

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

CSE 251 PROJECT 1. Andrew Christlieb. Monday Class AND Friday Class Abstract. Web:

CSE 251 PROJECT 1. Andrew Christlieb. Monday Class AND Friday Class Abstract. Web: CSE 51 PROJECT 1 Andrew Christlieb Monday Class 0-03-14 AND Friday Class 01-31-14 Abstract Web: http://www.cse.msu.edu/ cse51 Project 1 due date: (Monday Class) 0-17-14 AND (Friday Class)0-14-14, time:

More information

MS6021 Scientific Computing. TOPICS: Python BASICS, INTRO to PYTHON for Scientific Computing

MS6021 Scientific Computing. TOPICS: Python BASICS, INTRO to PYTHON for Scientific Computing MS6021 Scientific Computing TOPICS: Python BASICS, INTRO to PYTHON for Scientific Computing Preliminary Notes on Python (v MatLab + other languages) When you enter Spyder (available on installing Anaconda),

More information

Mathematics (JUN11MPC201) General Certificate of Education Advanced Subsidiary Examination June Unit Pure Core TOTAL

Mathematics (JUN11MPC201) General Certificate of Education Advanced Subsidiary Examination June Unit Pure Core TOTAL Centre Number Candidate Number For Examiner s Use Surname Other Names Candidate Signature Examiner s Initials Mathematics Unit Pure Core 2 Wednesday 18 May 2011 General Certificate of Education Advanced

More information

Lesson 12: The Graph of the Equation y = f(x)

Lesson 12: The Graph of the Equation y = f(x) Classwork In Module 1, you graphed equations such as 4x + y = 10 by plotting the points on the Cartesian coordinate plane that corresponded to all of the ordered pairs of numbers (x, y) that were in the

More information

Running Cython. overview hello world with Cython. experimental setup adding type declarations cdef functions & calling external functions

Running Cython. overview hello world with Cython. experimental setup adding type declarations cdef functions & calling external functions Running Cython 1 Getting Started with Cython overview hello world with Cython 2 Numerical Integration experimental setup adding type declarations cdef functions & calling external functions 3 Using Cython

More information

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017 Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Spring 2017 Outline Functions State Functions as building blocks Higher order functions Map Reduce

More information

COMPUTING AND DATA ANALYSIS WITH EXCEL. Numerical integration techniques

COMPUTING AND DATA ANALYSIS WITH EXCEL. Numerical integration techniques COMPUTING AND DATA ANALYSIS WITH EXCEL Numerical integration techniques Outline 1 Quadrature in one dimension Mid-point method Trapezium method Simpson s methods Uniform random number generation in Excel,

More information

Functions with Parameters and Return Values

Functions with Parameters and Return Values CS101, Spring 2015 Functions with Parameters and Return Values Lecture #4 Last week we covered Objects and Types Variables Methods Tuples Roadmap Last week we covered Objects and Types Variables Methods

More information

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 18, 2019 CS1 Lecture 3 Jan. 18, 2019 Office hours for Prof. Cremer and for TAs have been posted. Locations will change check class website regularly First homework assignment will be available Monday evening, due

More information

MATH2070: LAB 3: Roots of Equations

MATH2070: LAB 3: Roots of Equations MATH2070: LAB 3: Roots of Equations 1 Introduction Introduction Exercise 1 A Sample Problem Exercise 2 The Bisection Idea Exercise 3 Programming Bisection Exercise 4 Variable Function Names Exercise 5

More information

More on Lists. https://docs.python.org/2/tutorial/datastructures.html

More on Lists. https://docs.python.org/2/tutorial/datastructures.html The Rise of Google Announcement HW 2 due today. HW 3 will be posted later this week, when we figure out CADE lab s Python, SciPy, NumPy versions Please go to TA s office hours If you have trouble locating

More information

Lecture 1: Hello, MATLAB!

Lecture 1: Hello, MATLAB! Lecture 1: Hello, MATLAB! Math 98, Spring 2018 Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 1 / 21 Syllabus Instructor: Eric Hallman Class Website: https://math.berkeley.edu/~ehallman/98-fa18/ Login:!cmfmath98

More information

Sequences and iteration in Python

Sequences and iteration in Python GC3: Grid Computing Competence Center Sequences and iteration in Python GC3: Grid Computing Competence Center, University of Zurich Sep. 11 12, 2013 Sequences Python provides a few built-in sequence classes:

More information

PHCpack, phcpy, and Sphinx

PHCpack, phcpy, and Sphinx PHCpack, phcpy, and Sphinx 1 the software PHCpack a package for Polynomial Homotopy Continuation polyhedral homotopies the Python interface phcpy 2 Documenting Software with Sphinx Sphinx generates documentation

More information

Fall 2017: Numerical Methods I Assignment 1 (due Sep. 21, 2017)

Fall 2017: Numerical Methods I Assignment 1 (due Sep. 21, 2017) MATH-GA 2010.001/CSCI-GA 2420.001, Georg Stadler (NYU Courant) Fall 2017: Numerical Methods I Assignment 1 (due Sep. 21, 2017) Objectives. This class is for you and you should try to get the most out of

More information

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing

More information

Web Interfaces. the web server Apache processing forms with Python scripts Python code to write HTML

Web Interfaces. the web server Apache processing forms with Python scripts Python code to write HTML Web Interfaces 1 Python Scripts in Browsers the web server Apache processing forms with Python scripts Python code to write HTML 2 Web Interfaces for the Determinant dynamic interactive forms passing data

More information

Outline. policies. with some potential answers... MCS 260 Lecture 19 Introduction to Computer Science Jan Verschelde, 24 February 2016

Outline. policies. with some potential answers... MCS 260 Lecture 19 Introduction to Computer Science Jan Verschelde, 24 February 2016 Outline 1 midterm exam on Friday 26 February 2016 policies 2 questions with some potential answers... MCS 260 Lecture 19 Introduction to Computer Science Jan Verschelde, 24 February 2016 Intro to Computer

More information

CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 3 Jan. 22, 2018 CS1 Lecture 3 Jan. 22, 2018 Office hours for me and for TAs have been posted, locations will change check class website regularly First homework available, due Mon., 9:00am. Discussion sections tomorrow

More information

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving The Bisection Method and Newton s Method. If f( x ) a function, then a number r for which f( r) 0 is called a zero or a root of the function f( x ), or a solution to the equation f( x) 0. You are already

More information

LING 408/508: Computational Techniques for Linguists. Lecture 5

LING 408/508: Computational Techniques for Linguists. Lecture 5 LING 408/508: Computational Techniques for Linguists Lecture 5 Last Time Installing Ubuntu 18.04 LTS on top of VirtualBox Your Homework 2: did everyone succeed? Ubuntu VirtualBox Host OS: MacOS or Windows

More information

Math Analysis Chapter 1 Notes: Functions and Graphs

Math Analysis Chapter 1 Notes: Functions and Graphs Math Analysis Chapter 1 Notes: Functions and Graphs Day 6: Section 1-1 Graphs Points and Ordered Pairs The Rectangular Coordinate System (aka: The Cartesian coordinate system) Practice: Label each on the

More information

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming 2017 Table of contents 1 2 Integers and floats Integer int and float float are elementary numeric types in. integer >>> a=1 >>> a 1 >>> type (a) Integers and floats Integer int and float

More information

Final Exam Review Algebra Semester 1

Final Exam Review Algebra Semester 1 Final Exam Review Algebra 015-016 Semester 1 Name: Module 1 Find the inverse of each function. 1. f x 10 4x. g x 15x 10 Use compositions to check if the two functions are inverses. 3. s x 7 x and t(x)

More information

Draw a diagram of an empty circular queue and describe it to the reader.

Draw a diagram of an empty circular queue and describe it to the reader. 1020_1030_testquestions.text Wed Sep 10 10:40:46 2014 1 1983/84 COSC1020/30 Tests >>> The following was given to students. >>> Students can have a good idea of test questions by examining and trying the

More information

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product

More information

processing data with a database

processing data with a database processing data with a database 1 MySQL and MySQLdb MySQL: an open source database running MySQL for database creation MySQLdb: an interface to MySQL for Python 2 CTA Tables in MySQL files in GTFS feed

More information

Intelligente Datenanalyse Intelligent Data Analysis

Intelligente Datenanalyse Intelligent Data Analysis Universität Potsdam Institut für Informatik Lehrstuhl Maschinelles Lernen Intelligent Data Analysis Tobias Scheffer, Gerrit Gruben, Nuno Marquez Plan for this lecture Introduction to Python Main goal is

More information

Random Walks & Cellular Automata

Random Walks & Cellular Automata Random Walks & Cellular Automata 1 Particle Movements basic version of the simulation vectorized implementation 2 Cellular Automata pictures of matrices an animation of matrix plots the game of life of

More information

S206E Lecture 13, 5/22/2016, Grasshopper Math and Logic Rules

S206E Lecture 13, 5/22/2016, Grasshopper Math and Logic Rules S206E057 -- Lecture 13, 5/22/2016, Grasshopper Math and Logic Rules Copyright 2016, Chiu-Shui Chan. All Rights Reserved. Interface of Math and Logic Functions 1. Basic mathematic operations: For example,

More information

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10 Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10 Ziad Matni Dept. of Computer Science, UCSB Administrative Homework #5 is due today Homework #6 is out and DUE on MONDAY (3/5)

More information

Random Walks & Cellular Automata

Random Walks & Cellular Automata Random Walks & Cellular Automata 1 Particle Movements basic version of the simulation vectorized implementation 2 Cellular Automata pictures of matrices an animation of matrix plots the game of life of

More information

PHY224 Practical Physics I. Lecture 2

PHY224 Practical Physics I. Lecture 2 PHY224 Practical Physics I Python Review Lecture 2 Sept. 15 16 16, 2014 Summary Functions and Modules Graphs (plotting with Pylab) Scipy packages References M H. Goldwasser, D. Letscher: Object oriented

More information

Dynamic Arrays and Amortized Analysis

Dynamic Arrays and Amortized Analysis Department of Computer Science and Engineering Chinese University of Hong Kong As mentioned earlier, one drawback of arrays is that their lengths are fixed. This makes it difficult when you want to use

More information

Implementing Algorithms

Implementing Algorithms Implementing Algorithms 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3

More information

CS1110 Lab 6 (Mar 17-18, 2015)

CS1110 Lab 6 (Mar 17-18, 2015) CS1110 Lab 6 (Mar 17-18, 2015) First Name: Last Name: NetID: The lab assignments are very important and you must have a CS 1110 course consultant tell CMS that you did the work. (Correctness does not matter.)

More information

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6 Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Administrative New Homework (#3) is

More information

Areas of Planar Regions WORK SHEETS 1

Areas of Planar Regions WORK SHEETS 1 Activity 1. Areas of Planar Regions WORK SHEETS 1 In Figure 1 select two points in the first quadrant at integer coordinates. Label the point with the larger x-value P 1 and the other P 2. Select P 2 so

More information

61A Lecture 7. Monday, September 16

61A Lecture 7. Monday, September 16 61A Lecture 7 Monday, September 16 Announcements Homework 2 due Tuesday at 11:59pm Project 1 due Thursday at 11:59pm Extra debugging office hours in Soda 405: Tuesday 6-8, Wednesday 6-7, Thursday 5-7 Readers

More information

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Using Monte Carlo to Estimate π using Buffon s Needle Problem An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Here s the problem (in a simplified form). Suppose

More information

Python Programming Exercises 1

Python Programming Exercises 1 Python Programming Exercises 1 Notes: throughout these exercises >>> preceeds code that should be typed directly into the Python interpreter. To get the most out of these exercises, don t just follow them

More information

CS Advanced Unix Tools & Scripting

CS Advanced Unix Tools & Scripting & Scripting Spring 2011 Hussam Abu-Libdeh slides by David Slater March 4, 2011 Hussam Abu-Libdeh slides by David Slater & Scripting Python An open source programming language conceived in the late 1980s.

More information

Introduction to Python Programming

Introduction to Python Programming advances IN SYSTEMS AND SYNTHETIC BIOLOGY 2018 Anna Matuszyńska Oliver Ebenhöh oliver.ebenhoeh@hhu.de Ovidiu Popa ovidiu.popa@hhu.de Our goal Learning outcomes You are familiar with simple mathematical

More information

Least Squares; Sequence Alignment

Least Squares; Sequence Alignment Least Squares; Sequence Alignment 1 Segmented Least Squares multi-way choices applying dynamic programming 2 Sequence Alignment matching similar words applying dynamic programming analysis of the algorithm

More information

September 08, Graph y 2 =x. How? Is it a function? Function?

September 08, Graph y 2 =x. How? Is it a function? Function? Graph y 2 =x How? Is it a function? Function? Section 1.3 Graphs of Functions Objective: Analyze the graphs of functions. Important Vocabulary Graph of a function The collection of ordered pairs ( x, f(x))

More information

PHY224 Practical Physics I. Lecture 2

PHY224 Practical Physics I. Lecture 2 PHY224 Practical Physics I Python Review Lecture 2 Sept. 19 20 20, 2013 Summary Functions and Modules Graphs (plotting with Pylab) Scipy packages References M H. Goldwasser, D. Letscher: Object oriented

More information

Paper Reference(s) 6672 Edexcel GCE Pure Mathematics P2 Advanced/Advanced Subsidiary Monday 20 January 2003 Morning Time: 1 hour 30 minutes

Paper Reference(s) 6672 Edexcel GCE Pure Mathematics P2 Advanced/Advanced Subsidiary Monday 20 January 2003 Morning Time: 1 hour 30 minutes Paper Reference(s) 6672 Edexcel GCE Pure Mathematics P2 Advanced/Advanced Subsidiary Monday 20 January 2003 Morning Time: 1 hour 30 minutes Materials required for examination Answer Book (AB16) Graph Paper

More information

Higher order procedures procedures as types

Higher order procedures procedures as types Higher order procedures procedures as types Today s topics Review of types of objects and procedures Procedural abstractions Capturing patterns across procedures Higher Order Procedures Note: Lecture ends

More information

Script language: Python Data structures

Script language: Python Data structures Script language: Python Data structures Cédric Saule Technische Fakultät Universität Bielefeld 3. Februar 2015 Immutable vs. Mutable Previously known types: int and string. Both are Immutable but what

More information

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4. 1. Write an X To the left of each valid Python name (identifier). (4pts) a) X pyhonindetfriar c) X a_b_c_d b) 9to5 d) x*y all or none 2. Write an X To the left of each Python reserved word (keyword). (4pts)

More information