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

7
SMART_READER_LITE
LIVE PREVIEW

-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


slide-1
SLIDE 1

http://bit.ly/ds2oo1 practicum 4

Read final project spec for class next week

slide-2
SLIDE 2

DS 2001:Programming Practicum Fall 2020 — Felix Muzny

Practicum 4: Lists!

1

Trace the value of x as the given code

  • executes. What is printed by this code

when it runs?

x = 4 ls = ['cat', 'bat', 'horse'] for i in range(len(ls)): x = x + (ls[i] % 2) print(x)

xDX

$

  • 61
  • 7

lendsCia

'cat

'

42 ERROR!

slide-3
SLIDE 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 different points.

2

def square1(num): squared = num ** 2 print(squared) print(num)

# ERROR

slide-4
SLIDE 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!

3

1 def add(num1, num2): 2 a = num1 + num2 3 return a 4 def sub(num1, num2): 5 a = num1 - num2 6 return a 7 def main(): 8 plussed = add(10, 5) 9 minussed = sub(10, 5) 10 print("done!") 11 main()

Order ofexecution

  • 11

4

7

58

6

1

10

  • ats

2

Is

3

  • q

slide-5
SLIDE 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!

4

1 def add(num1, num2): 2 a = num1 + num2 3 return a 4 def sub(num1, num2): 5 a = num1 - num2 6 return a 7 def main(): 8 plussed = add(sub(10, 5), sub(11, 3)) 9 extra_plus = add(plussed, plussed) 10 print("done!") 11 main()

11

7-

8

4

56

← 5

7

45

6

1

23

slide-6
SLIDE 6

data = [[ ], [ ], [ ], [ ]]

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

5

1 "Anuj" 15 "Mon and Sat" 2 "Kaushik" 14 "Tue and Wed" 3 "Niyati" 14 "Tue and Fri" 4 "Satya" 15 "Tue and Fri"

print (data[

OT ) # 1st row

print (dataCo] [IT) #

Anuj

print ( dataE3] [IT) # Satya

slide-7
SLIDE 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

6

ls = ['cat', 'bat', 'dog'] # iterate by element for element in ls: print(element) # iterate by index for i in range(len(ls)): ls[i] = ls[i] + 's'

print Celement t

"s

'}

}

print lls

) # C

"cats

"

, " bats

"

.

.

  • I
slide-8
SLIDE 8

lists: common patterns

  • Accumulating an answer by visiting each element in a list

7

def number_uppercase(ls): for element in ls:

count = 0

if element . is upperl) :

count

= count

+I

return count

slide-9
SLIDE 9

lists: common patterns

  • Iterating over two-dimensional lists

8

data = [[1, 3, 5], [2, 4, 6]] for row in data: print("row:", row) for item in row: print(item)

P P t

loop # Z

  • # I
  • # 2
slide-10
SLIDE 10

lists: common patterns

  • Iterating over two-dimensional lists, using range so that we have access to the

indices

9

data = [[1, 3, 5], [2, 4, 6]] for row_index in range(len(data)): print("row:", row_index) for col_index in _______________: print(____________)

PPP

Ed, [1,03]

T

8

←¥CdataCrow

  • index

]))

E't]

dataflow -indexed -indeed

slide-11
SLIDE 11

lists: common patterns

  • lists are mutable--this means that they are going to act differently than the types that

we are used to when we pass them as parameters and update them.

10

def main():

  • dds = [1, 3, 5]
  • ther_odds = odds
  • dds[0] = odds[0] + 2

print(odds) print(other_odds) main() def main(): x = 1

  • ther_x = x

x = x + 2 print(x) print(other_x) main()

  • ddsk¥7

x TE

  • ther-odds
  • ther
  • xD
slide-12
SLIDE 12

lists: common patterns

  • lists are mutable--this means that they are going to act differently than the types that

we are used to when we pass them as parameters and update them.

11

def list_add_mutate(ls, num): ls[0] = ls[0] + num def main():

  • dds = [1, 3, 5]

list_add_mutate(odds, 2) print(odds) main()

  • # [373,5]
slide-13
SLIDE 13

lists: common patterns

  • lists are mutable--this means that they are going to act differently than the types that

we are used to when we pass them as parameters and update them.

12

def list_add_pure(ls, num): new_ls = [:] # get a copy of ls new_ls[0] = new_ls[0] + num return new_ls def main():

  • dds = [1, 3, 5]

updated_odds = list_add_pure(odds, 2) print(odds) print(updated_odds) main()

  • #update copy

a-

# Ct, 3,5]

# E3, 3,5]

slide-14
SLIDE 14

lists: common patterns

  • slicing (isolating a part of a list and returning it to us as a new list)

13

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[:])

slide-15
SLIDE 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