|
| 1 | +# Loops |
| 2 | + |
| 3 | +In programming, a **loop** means repeating something multiple times. |
| 4 | +There are different kinds of loops: |
| 5 | + |
| 6 | +- **While loops** repeat something while a condition is true. |
| 7 | +- **Until loops** repeat something until a condition is true. |
| 8 | +- **For loops** repeat something for each element of a sequence. |
| 9 | + |
| 10 | +We'll talk about all of these in this tutorial. |
| 11 | + |
| 12 | +## While loops |
| 13 | + |
| 14 | +Now we know how if statements work. |
| 15 | + |
| 16 | +```py |
| 17 | +its_raining = True |
| 18 | +if its_raining: |
| 19 | + print("Oh crap, it's raining!") |
| 20 | +``` |
| 21 | + |
| 22 | +While loops are really similar to if statements. |
| 23 | + |
| 24 | +```py |
| 25 | +its_raining = True |
| 26 | +while its_raining: |
| 27 | + print("Oh crap, it's raining!") |
| 28 | + # we'll jump back to the line with the word "while" from here |
| 29 | +print("It's not raining anymore.") |
| 30 | +``` |
| 31 | + |
| 32 | +If you're not familiar with while loops, the program's output may be a |
| 33 | +bit surprising: |
| 34 | + |
| 35 | +``` |
| 36 | +Oh crap, it's raining! |
| 37 | +Oh crap, it's raining! |
| 38 | +Oh crap, it's raining! |
| 39 | +Oh crap, it's raining! |
| 40 | +Oh crap, it's raining! |
| 41 | +Oh crap, it's raining! |
| 42 | +Oh crap, it's raining! |
| 43 | +Oh crap, it's raining! |
| 44 | +Oh crap, it's raining! |
| 45 | +Oh crap, it's raining! |
| 46 | +Oh crap, it's raining! |
| 47 | +Oh crap, it's raining! |
| 48 | +Oh crap, it's raining! |
| 49 | +Oh crap, it's raining! |
| 50 | +Oh crap, it's raining! |
| 51 | +(much more raining) |
| 52 | +``` |
| 53 | + |
| 54 | +Again, this program does not break your computer. It just prints the |
| 55 | +same thing multiple times. We can interrupt it by pressing Ctrl+C. |
| 56 | + |
| 57 | +In this example, `its_raining` was the **condition**. If something in |
| 58 | +the while loop would have set `its_raining` to False, the loop would |
| 59 | +have ended and the program would have printed `It's not raining anymore`. |
| 60 | + |
| 61 | +Let's actually create a program that does just that: |
| 62 | + |
| 63 | +```py |
| 64 | +its_raining = True |
| 65 | +while its_raining: |
| 66 | + print("It's raining!") |
| 67 | + answer = input("Or is it? (y=yes, n=no) ") |
| 68 | + if answer == 'y': |
| 69 | + print("Oh well...") |
| 70 | + elif answer == 'n': |
| 71 | + its_raining = False # end the while loop |
| 72 | + else: |
| 73 | + print("Enter y or n next time.") |
| 74 | +print("It's not raining anymore.") |
| 75 | +``` |
| 76 | + |
| 77 | +Running the program may look like this: |
| 78 | + |
| 79 | +``` |
| 80 | +It's raining! |
| 81 | +Or is it? (y=yes, n=no) i dunno |
| 82 | +Enter y or n next time. |
| 83 | +It's raining! |
| 84 | +Or is it? (y=yes, n=no) y |
| 85 | +Oh well... |
| 86 | +It's raining! |
| 87 | +Or is it? (y=yes, n=no) n |
| 88 | +It's not raining anymore. |
| 89 | +``` |
| 90 | + |
| 91 | +We can also interrupt a loop even if the condition is still true using |
| 92 | +the `break` keyword. In this case, we'll set condition to True and rely |
| 93 | +on nothing but `break` to end the loop. |
| 94 | + |
| 95 | +```py |
| 96 | +while True: |
| 97 | + answer = input("Is it raining? (y=yes, n=no) ") |
| 98 | + if answer == 'y': |
| 99 | + print("It's raining!") |
| 100 | + elif answer == 'n': |
| 101 | + print("It's not raining anymore.") |
| 102 | + break # end the loop |
| 103 | + else: |
| 104 | + print("Enter y or n.") |
| 105 | +``` |
| 106 | + |
| 107 | +The program works like this: |
| 108 | + |
| 109 | +```py |
| 110 | +Is it raining? (y=yes, n=no) who knows |
| 111 | +Enter y or n. |
| 112 | +Is it raining? (y=yes, n=no) y |
| 113 | +It's raining! |
| 114 | +Is it raining? (y=yes, n=no) n |
| 115 | +It's not raining anymore. |
| 116 | +``` |
| 117 | + |
| 118 | +## Until loops |
| 119 | + |
| 120 | +Python doesn't have until loops. If you need an until loop, use |
| 121 | +`while not`: |
| 122 | + |
| 123 | +```py |
| 124 | +raining = False |
| 125 | +while not raining: |
| 126 | + print("It's not raining.") |
| 127 | + if input("Is it raining? (y/n) ") == 'y': |
| 128 | + raining = True |
| 129 | +print("It's raining!") |
| 130 | +``` |
| 131 | + |
| 132 | +## For loops |
| 133 | + |
| 134 | +Let's say we have a list of things we want to print. To print each item |
| 135 | +in stuff, we could just do a bunch of prints: |
| 136 | + |
| 137 | +```py |
| 138 | +stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you'] |
| 139 | + |
| 140 | +print(stuff[0]) |
| 141 | +print(stuff[1]) |
| 142 | +print(stuff[2]) |
| 143 | +print(stuff[3]) |
| 144 | +print(stuff[4]) |
| 145 | +``` |
| 146 | + |
| 147 | +The output of the program is like this: |
| 148 | + |
| 149 | +```py |
| 150 | +hello |
| 151 | +hi |
| 152 | +how are you doing |
| 153 | +im fine |
| 154 | +how about you |
| 155 | +``` |
| 156 | + |
| 157 | +But this is only going to print five items, so if we add something to |
| 158 | +stuff, it's not going to be printed. Or if we remove something from |
| 159 | +stuff, we'll get an error saying "list index out of range". |
| 160 | + |
| 161 | +We could also create an index variable, and use a while loop: |
| 162 | + |
| 163 | +```py |
| 164 | +>>> length_of_stuff = len(stuff) # len(x) is the length of x |
| 165 | +>>> index = 0 |
| 166 | +>>> while index < length_of_stuff: |
| 167 | +... print(stuff[index]) |
| 168 | +... index += 1 |
| 169 | +... |
| 170 | +hello |
| 171 | +hi |
| 172 | +how are you doing |
| 173 | +im fine |
| 174 | +how about you |
| 175 | +>>> |
| 176 | +``` |
| 177 | + |
| 178 | +But there's `len()` and an index variable we need to increment. That's |
| 179 | +a lot of stuff to worry about for just printing each item. |
| 180 | + |
| 181 | +This is when for loops come in: |
| 182 | + |
| 183 | +```py |
| 184 | +>>> stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you'] |
| 185 | +>>> for thing in stuff: |
| 186 | +... # this is repeated for each element of stuff, that is, first |
| 187 | +... # for stuff[0], then for stuff[1], etc. |
| 188 | +... print(thing) |
| 189 | +... |
| 190 | +hello |
| 191 | +hi |
| 192 | +how are you doing |
| 193 | +im fine |
| 194 | +how about you |
| 195 | +>>> |
| 196 | +``` |
| 197 | + |
| 198 | +Without the comments, that's only two simple lines, and one variable. |
| 199 | +Much better than anything else we tried before. |
| 200 | + |
| 201 | +```py |
| 202 | +>>> for thing in stuff: |
| 203 | +... print(thing) |
| 204 | +... |
| 205 | +hello |
| 206 | +hi |
| 207 | +how are you doing |
| 208 | +im fine |
| 209 | +how about you |
| 210 | +>>> |
| 211 | +``` |
| 212 | + |
| 213 | +There's only one big limitation with for looping over lists. You |
| 214 | +shouldn't modify the list in the for loop. If you do, the results can |
| 215 | +be surprising: |
| 216 | + |
| 217 | +```py |
| 218 | +>>> stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you'] |
| 219 | +>>> for thing in stuff: |
| 220 | +... stuff.remove(thing) |
| 221 | +... |
| 222 | +>>> stuff |
| 223 | +['hi', 'im fine'] |
| 224 | +>>> |
| 225 | +``` |
| 226 | + |
| 227 | +Instead, you can create a copy of stuff and loop over it. |
| 228 | + |
| 229 | +```py |
| 230 | +>>> stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you'] |
| 231 | +>>> for thing in stuff.copy(): |
| 232 | +... stuff.remove(thing) |
| 233 | +... |
| 234 | +>>> stuff |
| 235 | +[] |
| 236 | +>>> |
| 237 | +``` |
| 238 | + |
| 239 | +Or if you want to clear a list, just use the `.clear()` list method. |
| 240 | + |
| 241 | +```py |
| 242 | +>>> stuff = ['hello', 'hi', 'how are you doing', 'im fine', 'how about you'] |
| 243 | +>>> stuff.clear() |
| 244 | +>>> stuff |
| 245 | +[] |
| 246 | +>>> |
| 247 | +``` |
| 248 | + |
| 249 | +## Excercises |
| 250 | + |
| 251 | +1. Back in "Using if, else and elif" we created a program that asked |
| 252 | + for username and password and checks them, and we made users "foo" |
| 253 | + and "bar" with passwords "biz" and "baz". Adding a new user would |
| 254 | + have required adding more code that checks the username and |
| 255 | + password. Add this to the beginning of your program: |
| 256 | + |
| 257 | + ```py |
| 258 | + users = [ |
| 259 | + ['foo', 'bar'], |
| 260 | + ['biz', 'baz'], |
| 261 | + ] |
| 262 | + ``` |
| 263 | + |
| 264 | + Then rewrite the rest of the program using a for loop. |
| 265 | + |
| 266 | +2. Make the program ask the username and password over and over again |
| 267 | +3. Can you limit the number of attempts to 3? |
0 commit comments