CS 111 w20 lecture 11 outline

1 Poll

s = "that's good soup!"
acc = "mmm"
for i in range(1, len(s), 2):
    acc = acc + s[i]
print(acc)
def zero(vec):
    for i in range(len(vec)):
        vec[i] = 0
vec = [1,2,3]
print(zero(vec))
def swap_first(xs, ys):
    t = xs[0]
    xs[0] = ys[0]
    ys[0] = t
v1 = [1, 2, 3]
v2 = ("a", "b", "c")
swap_first(v1, v2)
print(v1)

2 while loop

while CONDITION:
    BODY
  • Each time through the loop (including the first time), CONDITION is evaluated
  • if CONDITION is True, the loop goes through BODY again
  • if CONDITION is False, the program skips to the first unindented line after the loop
  • while True: — infinite loop???