7
play

-7 lends Cia ' cat ' print(x) 42 ERROR ! 1 Functions + Scope - PowerPoint PPT Presentation

http://bit.ly/ds2oo1 practicum 4 Read final project spec for class next week DS 2001:Programming Practicum Fall 2020 Felix Muzny Practicum 4: Lists! Trace the value of x as the given code executes. What is printed by this code x = 4 x DX


  1. http://bit.ly/ds2oo1 practicum 4 Read final project spec for class next week

  2. DS 2001:Programming Practicum Fall 2020 — Felix Muzny Practicum 4: Lists! Trace the value of x as the given code executes. What is printed by this code x = 4 x DX when it runs? ls = ['cat', 'bat', 'horse'] $ for i in range(len(ls)): - 61 x = x + (ls[i] % 2) -7 lends Cia ' cat ' print(x) 42 ERROR ! 1

  3. Functions + Scope • Function parameters "live" only inside the function they belong to (they are "local" to that function) • Variables and parameters defined in one function are not accessible in another function • I strongly recommend that you only define variable inside of functions from now on • This will keep your program structure more organized, and make it easier to track what variables have what values at di ff erent points. def square1(num): squared = num ** 2 print(squared) # ERROR print(num) 2

  4. Functions: tracing program execution • When we run programs, we can trace the execution by looking at what lines run in what order. This gets complex when we have multiple functions! Order of execution 1 def add(num1, num2): 2 a = num1 + num2 - 11 3 return a 4 4 def sub(num1, num2): 58 7 5 a = num1 - num2 6 6 return a 1 10 7 def main(): oats 2 8 plussed = add(10, 5) Is 3 9 minussed = sub(10, 5) - q 10 print("done!") 11 main() → 3

  5. Functions: tracing program execution • When we run programs, we can trace the execution by looking at what lines run in what order. This gets complex when we have multiple functions! 1 def add(num1, num2): 11 2 a = num1 + num2 7- 3 return a 8 4 4 def sub(num1, num2): 56 5 a = num1 - num2 6 return a ← 5 45 7 def main(): 7 8 plussed = add(sub(10, 5), sub(11, 3)) 6 9 extra_plus = add(plussed, plussed) 1 10 print("done!") 11 main() 23 4

  6. lists: a summary • Lists are a container type that holds other values (ints, strings, other lists, etc) • We can think of two dimensional lists as tables of data with each sublist as one row and indexes of the sublists corresponding to columns data = [[ ], 1 "Anuj" 15 "Mon and Sat" [ ], 2 "Kaushik" 14 "Tue and Wed" [ ], 3 "Niyati" 14 "Tue and Fri" [ ]] 4 "Satya" 15 "Tue and Fri" OT ) # 1st row print ( data [ Anuj print ( data Co ] [ IT ) # print ( data E3 ] [ IT ) # Satya 5

  7. lists: common patterns • iterating over a list can be done in two ways: by element or by index • iterate by element if you need a quick interface to the values in the list • iterate by index if you need location information or to modify the list elements ls = ['cat', 'bat', 'dog'] # iterate by element ' } for element in ls: print(element) print C element t " s # iterate by index } for i in range(len(ls)): ls[i] = ls[i] + 's' " bats - I print l l s " ) # C " " cats 6 . . ,

  8. lists: common patterns • Accumulating an answer by visiting each element in a list def number_uppercase(ls): count = 0 for element in ls: if element . is upper l ) : + I = count count return count 7

  9. lists: common patterns • Iterating over two-dimensional lists data = [[1, 3, 5], [2, 4, 6]] P t P loop # Z - # I for row in data: print("row:", row) for item in row: # 2 - print(item) 8

  10. lists: common patterns • Iterating over two-dimensional lists, using range so that we have access to the indices data = [[1, 3, 5], [2, 4, 6]] Ed , [ 1,03 ] PPP for row_index in range(len(data)): T print("row:", row_index) ] ) ) - index ←¥ CdataCrow 8 for col_index in _______________: E 't ] print(____________) dataflow - indexed - indeed 9

  11. lists: common patterns • lists are mutable --this means that they are going to act di ff erently than the types that we are used to when we pass them as parameters and update them. odds k ¥ 7 x TE def main(): def main(): odds = [1, 3, 5] x = 1 other - odds - xD other - other_odds = odds other_x = x odds[0] = odds[0] + 2 x = x + 2 print(odds) print(x) print(other_odds) print(other_x) main() main() 10

  12. lists: common patterns • lists are mutable --this means that they are going to act di ff erently than the types that we are used to when we pass them as parameters and update them. def list_add_mutate(ls, num): - ls[0] = ls[0] + num def main(): odds = [1, 3, 5] list_add_mutate(odds, 2) # [ 373,5 ] print(odds) main() 11

  13. lists: common patterns • lists are mutable --this means that they are going to act di ff erently than the types that we are used to when we pass them as parameters and update them. def list_add_pure(ls, num): - new_ls = [:] # get a copy of ls # update copy new_ls[0] = new_ls[0] + num return new_ls def main(): odds = [1, 3, 5] a- updated_odds = list_add_pure(odds, 2) # Ct , 3,5 ] print(odds) # E3 , 3,5 ] print(updated_odds) main() 12

  14. lists: common patterns • slicing (isolating a part of a list and returning it to us as a new list) list_part = list_variable[begin_index:end_index] ls = ['cat', 'hat', 'horse'] print(ls[1:2]) print(ls[1:3]) print(ls[1:]) print(ls[:2]) print(ls[:]) 13

  15. Practicum today - coding • For practicum today, you'll be writing a series of functions that work with lists . • Each time you write a function, you'll write one or more corresponding function calls in your main function. • Your main function will go at the bottom of your file so that the other function definitions can be read in by python first. • Remember to work together and ask us questions—those of you who are remote, we'll put you in groups and ask you to screenshare with each other, as usual. 14

Recommend


More recommend