More Loop Examples Functions and Parameters

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Lists Introduction to Computing Science and Programming I.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Lists in Python.
CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue.
Lecture 21 - Tuples COMPSCI 101 Principles of Programming.
UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao.
Computer Science 210 Computer Organization Arrays.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
1 Workin’ with Pointas An exercise in destroying your computer.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Lecture 15 – more on lists and for…in loops, the split() function COMPSCI 1 1 Principles of Programming.
UNIT 10 Multidimensional Arrays.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
Find LCM Least Common Multiple of 3 and 5: List the Multiples of each number, The multiples of 3 are 3, 6, 9, 12, 15, 18,... etc The multiples of 5 are.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Operator Overloading Introduction
Advanced Python Idioms
EECS 110: Lec 12: Mutable Data
(Numerical Arrays of Multiple Dimensions)
Computer Science 210 Computer Organization
Computer Programming BCT 1113
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Foundations of Programming: Arrays
Java Review: Reference Types
Computer Science 210 Computer Organization
CSC 253 Lecture 8.
CSC 253 Lecture 8.
CS 220: Discrete Structures and their Applications
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Value returning Functions
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
CMSC201 Computer Science I for Majors Lecture 14 – Functions (Continued) Prof. Katherine Gibson Based on concepts from:
Arrays Outline Introduction Arrays Declaring Arrays
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
2D Array and Matrix.
5. Functions Rocky K. C. Chang 30 October 2018
Building Java Programs
Reference semantics, variables and names
EECE.2160 ECE Application Programming
Advanced Python Idioms
Functions continued.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Advanced Python Idioms
Pointers and dynamic objects
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
CIS 110: Introduction to Computer Programming
EECS 110: Lec 12: Mutable Data
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Introduction to Dictionaries
Arrays, Part 1 of 2 Topics Definition of a Data Structure
More 2D Array and Loop Examples Functions and Parameters
def-ining a function A function as an execution control structure
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
2D Array, Nested Loops.
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Introduction to Computer Science
Presentation transcript:

More Loop Examples Functions and Parameters

Example: min difference Given two list of numbers, compute the minimum difference among any pair of numbers, one from each list. E.g., list1 = [1, 2, 3, 4], list2 = [-2, 10, 5, 0, 6], the function minDiff would return 1, which occurs twice, {1,0}, {4,5}. Remember the findMin function? def findMin( aList ): min = aList[0] for x in aList: if min > x: min = x return min We can use similar ideas.

Example: min difference def minDiff( list1, list2 ): ’’’ The parameters list1, list2 are two int lists. Find minimum difference among any pairs. minDiffSoFar = 999999 # some outrageously big value for x in list1: for y in list2: diff = abs(x – y) if diff < minDiffSoFar: minDiffSoFar = diff return minDiffSoFar

Example: min difference Alternatively, you can use Python’s maximum value as the prime. import sys def minDiff( list1, list2 ): ’’’ The parameters list1, list2 are two int lists. Find minimum difference among any pairs. minDiffSoFar = sys.maxsize # largest Python value for x in list1: for y in list2: diff = abs(x – y) if diff < minDiffSoFar: minDiffSoFar = diff return minDiffSoFar

Functions and Parameters We’ve learned how to develop functions def findMax( aList ): max = aList[0] for i in range( len(aList) ): if aList[i] > max: max = aList[i] return max def sumList( aList ): sum = 0 for i in range( len(aList) ): sum += aList[i] return sum In both cases, ‘aList’ is called a parameter for the function A function can have multiple parameters Two types of parameters, mutable and immutable Let’s try out the examples (25_passing.py)

Pass By Value PASS BY VALUE def main() """ calls conform """ print(" Welcome to Conformity, Inc. ") fav = 7 conform(fav) print(" My favorite number is", fav ) def conform(fav) """ sets input to 42 """ fav = 42 return fav 7 fav type: int PASS BY VALUE 7 42 fav type: int “Pass by value" means that the data's value is copied when sent to a function...

Passing list content by reference … def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print(" My favorite numbers are", fav ) def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 7 11 fav fav[0] fav[1] type: list fav type: list What gets passed by value here?

Passing list content by reference… def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print(" My favorite numbers are", fav ) def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 42 42 7 11 fav fav[0] fav[1] The reference is copied, i.e., passed by value, but the contents arn’t ! fav and it can change data elsewhere!

Watch out! You can change the contents of lists in functions that take those lists as input. (actually, lists or any mutable objects) Those changes will be visible everywhere. (immutable objects are safe, however)

But lists are passing by value!!! def main() """ calls conform3 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform3(fav) print(" My favorite numbers are", fav ) def conform3(fav) """ creates a new fav!!! """ fav = [ 42, 42 ] 7 11 fav fav[0] fav[1] type: list fav type: list

But lists are passing by value!!! def main() """ calls conform3 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform3(fav) print(" My favorite numbers are", fav ) def conform3(fav) """ creates a new fav!!! """ fav = [ 42, 42 ] 7 11 fav[0] fav[1] fav 42 fav[0] fav[1] fav

Mutable vs. Immutable Pass by REFERENCE Pass by VALUE tuple dictionary float string list bool int x = [5,42,'hi'] s = 'hi' Reference Pointer id 'hi' 5 42 'hi' x s x[0] x[1] x[2] Lists and dictionaries are handled by reference (the variables really hold a memory address) Other types of data, incl. strings, are handled by value: they hold the actual data

Lists’ flexibility Lists can hold ANY type of data x = [ 42, 75, 70 ] 42 75 70 list int int int x We can equally well imagine them as vertical structures. 42 list int x 75 int 70 int

Lists’ flexibility Lists can hold ANY type of data 42.0 75.0 70.0 42 7 double double double x 42 7 -11 list int int int x “go” “red” “sox!” list String String String x

2d lists or arrays Lists can hold ANY type of data -- including lists ! x = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list x

2d arrays Lists can hold ANY type of data -- including lists ! x = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list x[0] x list x[1] list x[2]

Jagged arrays Lists can hold ANY type of data -- including lists ! x = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list x[0] x list x[1] list x[2] Rows within 2d arrays need not be the same length

Rectangular arrays What does x[1] refer to? list list x[0] x x[0][0] list x[1] list x[2] x[2][3] What does x[1] refer to? What value is changed with x[1][2]=42 ? How many rows does x have, in general ? How many columns does x have, in general ?