For loop our last loop type We use

![More for loops: def f(): for x in [1, 3, 5, 7, 9]: print(x) More for loops: def f(): for x in [1, 3, 5, 7, 9]: print(x)](https://slidetodoc.com/presentation_image_h2/1b5b34acc72ed0ab2d28ca3212603d2c/image-2.jpg)


![More for loops: def f(): for x in [0, 1, 2, 3, 4]: print(x) More for loops: def f(): for x in [0, 1, 2, 3, 4]: print(x)](https://slidetodoc.com/presentation_image_h2/1b5b34acc72ed0ab2d28ca3212603d2c/image-5.jpg)








![What does this do? def k(m, n): v = [] for x in range(m): What does this do? def k(m, n): v = [] for x in range(m):](https://slidetodoc.com/presentation_image_h2/1b5b34acc72ed0ab2d28ca3212603d2c/image-14.jpg)

- Slides: 15

For loop: our last loop type We use For loops when we know exactly how many times the loop will occur A subset of the other two loops Form: for variable in [value 1, value 2, value 3…lastvalue]: calculations Example: def f(): for x in [1, 2, 3, 4, 5]: print(x) return(x) print(f())
![More for loops def f for x in 1 3 5 7 9 printx More for loops: def f(): for x in [1, 3, 5, 7, 9]: print(x)](https://slidetodoc.com/presentation_image_h2/1b5b34acc72ed0ab2d28ca3212603d2c/image-2.jpg)
More for loops: def f(): for x in [1, 3, 5, 7, 9]: print(x) return(x) print(f()) def f(): for x in [2, 7, 1, 9, 4]: print(x) return(x) print(f())

More for loops: def f(): y = 0 ct = 0 for x in [3. 2, 7. 1, 8. 0, 3. 4, 5. 1]: print("including " + str(x)) ct +=1 y = y + x return(y/ct) print(f())

Loops with strings: def f(y): ct = 0 for x in ["puppy", "bunny", "puppy", "bird", "echidna", "puppy"]: if x == y: ct += 1 return(ct) print(f("puppy"))
![More for loops def f for x in 0 1 2 3 4 printx More for loops: def f(): for x in [0, 1, 2, 3, 4]: print(x)](https://slidetodoc.com/presentation_image_h2/1b5b34acc72ed0ab2d28ca3212603d2c/image-5.jpg)
More for loops: def f(): for x in [0, 1, 2, 3, 4]: print(x) return(x) print(f()) Shortcut: using range def f(): for x in range(5): print(x) return(x) print(f()) # range(5) = [0, 1, 2, 3, 4]

Same? def whilefunc(y): count = 0 total = 0 while (count < y): total += count += 1 return (total) print(whilefunc(5)) def forfunc(y): total = 0 for x in range(y): total += x return(total) print(forfunc(5))

More on Range: def f(): for x in range(-3, 3): # from -3 up to but not including 3, e. g. , [-3, -2, -1, 0, 1, 2] print(x) return(x) print(f()) def f(): for x in range(-3, 3, 2): # from -3 up to but not including 3, by increments of 2, e. g. , [-3, -1, 1] print(x) return(x) print(f()) (Can we make a loop go backwards? )

What does this do? def f(ls): y = 1000 total = 0 for x in ls: total = total + int(x) * int(y) y /=10 return(total) print(f(["2", "7", "1", "9"]))

What does this do? def f(v): var 1 = "" for x in range(len(v) - 1, -2): var 1 += v[x]; return(var 1) print(f("kebijrtese"))

This one? def f(): strvar = input("enter a number: ") y = 1 z = 0 var 1 = "" for x in range(len(strvar) - 1, -1): z += int(strvar[x]) * y; y*=10 return(z) print(f())

What does this give you? def f(lv): x = len(lv) print(x) for y in range(0, x): if "t" in lv[y]: print(lv[y]) return listvar = ["ham", "boat", "goat", "there", "anywhere"] f(listvar)

How about this one? def f(word): newstr = "" for i in range(1, len(word)+1): newstr += word[-i] return(newstr) wvar = "live" print(f(wvar))

What does this do? def f(word): high = len(word) low = 0 newstr = "" for i in range(10): position = randrange(low, high) newstr += word[position] return(newstr) wvar = “turkey" print(f(wvar))
![What does this do def km n v for x in rangem What does this do? def k(m, n): v = [] for x in range(m):](https://slidetodoc.com/presentation_image_h2/1b5b34acc72ed0ab2d28ca3212603d2c/image-14.jpg)
What does this do? def k(m, n): v = [] for x in range(m): ls = [] for y in range(n): ls. append(0) v. append(ls) return(v) m = k(4, 5) #what is this? for x in range(4): Add x 1 s to the matrix in random places? def p(x, m): i=0 while (i < x): #why didn’t I use a for loop here? b = randrange(len(m)) c = randrange(len(m[b])) if m[b][c] != 1: m[b][c] = 1 i+= 1 return(m) m = p(7, m) print(m[x]) for x in range(4): print(m[x])

Print out the matrix: Print out matrix with tabs between each num in each row, e. g. , : 0 0 1 1 0 0 0 1 1 1 0 0 def q(m): for x in range(len(m)): for y in range(len(m[x])): print(m[x][y], end = "t") print() return q(m)