Sequences Nested Loops Memory CS 112 GMU Nested

  • Slides: 17
Download presentation
Sequences Nested Loops & Memory CS 112 @ GMU

Sequences Nested Loops & Memory CS 112 @ GMU

Nested Loops • What happens when one loop is inside another? for i in

Nested Loops • What happens when one loop is inside another? for i in range(3): for j in range(3): print("i: ", i, "j: ", j) #return to top of inner loop #return to top of outer loop output when run: i: i: i: 0 0 0 1 1 1 2 2 2 j: j: j: 0 1 2

Nested Index Loops • Create an index for each dimension. • Nest loops. •

Nested Index Loops • Create an index for each dimension. • Nest loops. • Access each element individually. output when run: xss = [[5, 6, 7], [8, 9, 10]] for i in range(len(xss)): for j in range(len(xss[i])): print(xss[i][j]) 5 6 7 8 9 10

Nested Value Loops • • When we have multiple dimensions to our lists, we

Nested Value Loops • • When we have multiple dimensions to our lists, we can use that many nested loops to access each item individually. Note the access pattern, as well as the total calculation. xss = [[5, 6, 7], [8, 9, 10]] total = 0 for xs in xss: #what type is xs? for x in xs: #what type is x? print("t+ "+str(x)) total += x print("total: ", total) output when run: +5 +6 +7 +8 +9 + 10 total: 45

More Complicated Nested Loops • Our data doesn't have to have multiple dimensions for

More Complicated Nested Loops • Our data doesn't have to have multiple dimensions for our algorithm to find use for nested loops. # are there any duplicates in the list? xs = [2, 3, 5, 4, 5, 1, 7, 8] has_dupes = False for i in range(len(xs)): for j in range(len(xs)): if (i!=j) and xs[i]==xs[j]: has_dupes = True break print("any dupes? ", has_dupes) # are there any duplicates in the list? xs = [2, 3, 5, 4, 5, 1, 7, 8] has_dupes = False for i in range(len(xs)): for j in range(i+1, len(xs)): if xs[i]==xs[j]: has_dupes = True break print("any dupes? ", has_dupes) • What is different/better about the second version?

Challenge Exercise A prime number is a number that is divisible by only itself

Challenge Exercise A prime number is a number that is divisible by only itself and 1. First, write a program that tells the user if a number they entered is prime, or not. Then, update your program to print all the prime numbers that exist between 2 and the number that the user entered.

Poll – Loops loops, nested sequences, nested loops

Poll – Loops loops, nested sequences, nested loops

Memory

Memory

Lists in Memory • So far we've drawn simple boxes next to names for

Lists in Memory • So far we've drawn simple boxes next to names for our variables: x=5 x 5 • Now, we will draw an arrow from a variable to the block of values it contains. 6 7 8 xs = [6, 7, 8] xs

Memory Usage • These arrows help us understand complex data, such as lists of

Memory Usage • These arrows help us understand complex data, such as lists of lists. • Every variable always stores one value in a box. xs = [4, 5, 6] ys = [7, 8, 9] both = [xs, ys] xs ys both 4 5 6 7 8 9

Aliases Example xs = [1, 2, 3] ys = [4, 5, 6] both =

Aliases Example xs = [1, 2, 3] ys = [4, 5, 6] both = [xs, ys] xs[1] = 7 print("xs is", xs) print("both is", both) ys = [8, 9] print("ys is", ys) print("both is", both) program output: xs is [1, 7, 3] both is [[1, 7, 3], [4, 5, 6]] ys is [8, 9] both is [[1, 7, 3], [4, 5, 6]]

What's happening? ! • variables are not the same as values • alias: when

What's happening? ! • variables are not the same as values • alias: when multiple names for the same value exist – such as xs and both[0] • When drawing memory – two arrows to the same box? Alias!

Aliases Example xs = [1, 2, 3] ys = [4, 5, 6] both =

Aliases Example xs = [1, 2, 3] ys = [4, 5, 6] both = [xs, ys] xs[1] = 7 print("xs is", xs) print("both is", both) ys = [8, 9] print("ys is", ys) print("both is", both) xs ys 1 7 2 3 4 5 6 8 9 both program output: xs is [1, 7, 3] both is [[1, 7, 3], [4, 5, 6]] ys is [8, 9] both is [[1, 7, 3], [4, 5, 6]]

id Built-In Function >>> xs = [1, 2, 3] >>> ys = [4, 5,

id Built-In Function >>> xs = [1, 2, 3] >>> ys = [4, 5, 6] >>> both = [xs, ys] >>> id(ys) 4301525288 >>> id(both[1]) 4301525288 >>> ys = [9, 10] >>> id(ys) 4301525864 >>> id(both[1]) 4301525288 id(object): return the "identity" of an object as an int. Think of this as the memory address of the data. • Particular id values aren't important. • The numbers may be different each time you run the program! drawing memory: id() returns the same number? Draw one memory location! 14

Modifying Lists – can't use value loops This code shows how a value loop

Modifying Lists – can't use value loops This code shows how a value loop won't succeed. You should trace through this code to see why (with the visualizer). xs = [1, -2, 3, -4, -5, 100, 150, -30, 123] ys = xs[: ] # here's a copy of xs' original value. for x in xs: if x<0: x = -x # we try, but fail, to modify part of xs if xs==ys: print("failed to modify!") # this prints!

Visualizing This

Visualizing This

Poll – Loops memory

Poll – Loops memory