Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Loop Examples Functions and Parameters

Similar presentations


Presentation on theme: "More Loop Examples Functions and Parameters"— Presentation transcript:

1 More Loop Examples Functions and Parameters

2 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.

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

4 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

5 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)

6 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...

7 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?

8 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!

9 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)

10 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

11 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

12 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

13 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

14 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

15 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

16 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]

17 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

18 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 ?


Download ppt "More Loop Examples Functions and Parameters"

Similar presentations


Ads by Google