|
| 1 | +--- |
| 2 | +title: Conditionals |
| 3 | +teaching: 15 |
| 4 | +exercises: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Correctly write programs that use `if` and `else` statements using Boolean expressions. |
| 10 | +- Trace the execution of conditionals inside of loops. |
| 11 | + |
| 12 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 13 | + |
| 14 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 15 | + |
| 16 | +- How can programs do different things for different data? |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | +## Use `if` statements to control whether or not a block of code is executed. |
| 21 | + |
| 22 | +An `if` statement is a *conditional* statement that controls whether a block of code is executed or not. The syntax of an `if` statement is similar to a `for` statement: |
| 23 | + |
| 24 | +- The first line opens with `if` and ends with a colon. |
| 25 | +- The body is indented (usually by 4 spaces) |
| 26 | + |
| 27 | +```python |
| 28 | +checkouts = 11 |
| 29 | +if checkouts > 10.0: |
| 30 | + print(f'{checkouts} is over the limit.') |
| 31 | + |
| 32 | +checkouts = 8 |
| 33 | +if checkouts > 10.0: |
| 34 | + print(f'{checkouts} is over the limit.') |
| 35 | +``` |
| 36 | + |
| 37 | +```output |
| 38 | +11 is over the limit. |
| 39 | +``` |
| 40 | + |
| 41 | +## Conditionals are often used inside loops. |
| 42 | + |
| 43 | +There is not much of a point using a conditional when we know the value (as above), but they're useful when we have a collection to process. |
| 44 | + |
| 45 | +```python |
| 46 | +checkouts = [0, 3, 10, 12, 22] |
| 47 | +for checkout in checkouts: |
| 48 | + if checkout > 10.0: |
| 49 | + print(f'{checkout} is over the limit.') |
| 50 | +``` |
| 51 | + |
| 52 | +```output |
| 53 | +12 is over the limit. |
| 54 | +22 is over the limit. |
| 55 | +``` |
| 56 | + |
| 57 | +## Use `else` to execute a block of code when an `if` condition is *not* true. |
| 58 | + |
| 59 | +An `else` statement can be used following `if` to allow us to specify an alternative code block to execute when the `if` *branch* is not taken. |
| 60 | + |
| 61 | +```python |
| 62 | +for checkout in checkouts: |
| 63 | + if checkout > 10.0: |
| 64 | + print(f'{checkout} is over the limit.') |
| 65 | + else: |
| 66 | + print(f'{checkout} is under the limit.') |
| 67 | +``` |
| 68 | + |
| 69 | +```output |
| 70 | +0 is under the limit. |
| 71 | +3 is under the limit. |
| 72 | +10 is under the limit. |
| 73 | +12 is over the limit. |
| 74 | +22 is over the limit. |
| 75 | +``` |
| 76 | + |
| 77 | +Notice that our `else` statement led to a false output that says 10 is under the limit. We can address this by adding a different kind of `else` statement. |
| 78 | + |
| 79 | +## Use `elif` to specify additional tests. |
| 80 | + |
| 81 | +You can use `elif` (short for "else if") to provide several alternative choices, each with its own test. An `elif` statement should always be associated with an `if` statement, and must come before the `else` statement (which is the catch all). |
| 82 | + |
| 83 | +```python |
| 84 | +for checkout in checkouts: |
| 85 | + if checkout > 10.0: |
| 86 | + print(f'*Warning*: {checkout} is over the limit.') |
| 87 | + elif checkout == 10: |
| 88 | + print(f'{checkout} is at the exact limit.') |
| 89 | + else: |
| 90 | + print(f'{checkout} is under the limit.') |
| 91 | +``` |
| 92 | + |
| 93 | +```output |
| 94 | +0 is under the limit. |
| 95 | +3 is under the limit. |
| 96 | +10 is at the exact limit. |
| 97 | +*Warning*: 12 is over the limit. |
| 98 | +*Warning*: 22 is over the limit. |
| 99 | +
|
| 100 | +``` |
| 101 | + |
| 102 | +Conditions are tested once, in order and are not re-evaluated if values change. Python steps through the branches of the conditional in order, testing each in turn, so the order of your statements matters. |
| 103 | + |
| 104 | +```python |
| 105 | +grade = 85 |
| 106 | +if grade >= 70: |
| 107 | + print('grade is C') |
| 108 | +elif grade >= 80: |
| 109 | + print('grade is B') |
| 110 | +elif grade >= 90: |
| 111 | + print('grade is A') |
| 112 | +``` |
| 113 | + |
| 114 | +```output |
| 115 | +grade is C |
| 116 | +``` |
| 117 | + |
| 118 | +## Compound conditionals using `and` and `or` |
| 119 | + |
| 120 | +Often, you want some combination of things to be true. You can combine |
| 121 | +relations within a conditional using `and` and `or`. |
| 122 | + |
| 123 | +We can also check if something is less/greater than or equal to a value by using `>=` and `<=` operators. |
| 124 | + |
| 125 | +```python |
| 126 | +checkouts = [3, 50, 120] |
| 127 | +users = ['fac', 'grad'] |
| 128 | + |
| 129 | +for user in users: |
| 130 | + for checkout in checkouts: |
| 131 | + #faculty checkout limit is 100 |
| 132 | + if checkout >= 100 and user == 'fac': |
| 133 | + print(f"*Warning*: {checkout} is over the {user} limit.") |
| 134 | + |
| 135 | + #grad limit is 50 |
| 136 | + elif checkout >= 50 and user == 'grad': |
| 137 | + print(f"{checkout} is over the {user} limit.") |
| 138 | + |
| 139 | + else: |
| 140 | + print(f"{checkout} is under the {user} limit.") |
| 141 | + |
| 142 | + # print an empty line between users |
| 143 | + print() |
| 144 | + |
| 145 | +``` |
| 146 | +```output |
| 147 | +3 is under the fac limit. |
| 148 | +50 is under the fac limit. |
| 149 | +*Warning*: 120 is over the fac limit. |
| 150 | +
|
| 151 | +3 is under the grad limit. |
| 152 | +*Warning*: 50 is over the grad limit. |
| 153 | +*Warning*: 120 is over the grad limit. |
| 154 | +``` |
| 155 | + |
| 156 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 157 | + |
| 158 | +## Age conditionals |
| 159 | + |
| 160 | +Write a Python program that checks the age of a user to determine if they will receive a youth or adult library card. The program should: |
| 161 | + |
| 162 | +1. Store `age` in a variable. |
| 163 | +2. Use an `if` statement to check if the age is 16 or older. If true, print "You are eligible for an adult library card." |
| 164 | +3. Use an `else` statement to print "You are eligible for a youth library card" if the age is less than 16. |
| 165 | + |
| 166 | +If you finish early, try this challenge: |
| 167 | + |
| 168 | +- In a new cell, adapt your program to loop through a list of age values, testing each age with the same output as above. |
| 169 | + |
| 170 | +::::::::::::::: solution |
| 171 | + |
| 172 | +## Solution |
| 173 | + |
| 174 | +For parts 1 to 3: |
| 175 | + |
| 176 | +```python |
| 177 | +age = 25 |
| 178 | + |
| 179 | +if age >= 16: |
| 180 | + print('You are eligible for an adult library card.') |
| 181 | +else: |
| 182 | + print('You are eligible for a youth library card.') |
| 183 | +``` |
| 184 | + |
| 185 | +For the challenge: |
| 186 | +```python |
| 187 | +ages = [10, 16, 30, 65] |
| 188 | + |
| 189 | +for age in ages: |
| 190 | + if age >= 16: |
| 191 | + print('You are eligible for an adult library card.') |
| 192 | + else: |
| 193 | + print('You are eligible for a youth library card.') |
| 194 | +``` |
| 195 | + |
| 196 | + |
| 197 | +::::::::::::::::::::::::: |
| 198 | + |
| 199 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 200 | + |
| 201 | + |
| 202 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 203 | + |
| 204 | +## Conditional logic: Fill in the blanks |
| 205 | + |
| 206 | +Fill in the blanks in the following program to check if both the name variable is present in the names list and the password variable is equal to 'true' before giving a user access to a library system. |
| 207 | + |
| 208 | +If you have extra time after you solve the fill in the blanks, change the value of `password` and re-run the program to view the output. |
| 209 | + |
| 210 | +```python |
| 211 | +names = ['Wang', 'Garcia', 'Martin'] |
| 212 | +name = 'Martin' |
| 213 | +password = 'true' |
| 214 | + |
| 215 | +___ item in names: |
| 216 | + print(item) |
| 217 | + if name == item ___ password == _____: |
| 218 | + print('Login successful!') |
| 219 | + elif password __ 'true': |
| 220 | + print(f'Your password is incorrect. Try again.') |
| 221 | + ____ name __ item: |
| 222 | + print(f'- Name does not match. Testing the next item in the list for {name}...') |
| 223 | + |
| 224 | +``` |
| 225 | +::::::::::::::: solution |
| 226 | + |
| 227 | +## Solution |
| 228 | + |
| 229 | +```python |
| 230 | +names = ['Wang', 'Garcia', 'Martin'] |
| 231 | +name = 'Martin' |
| 232 | +password = 'true' |
| 233 | + |
| 234 | +for item in names: |
| 235 | + print(item) |
| 236 | + if name == item and password == 'true': |
| 237 | + print('Login successful!') |
| 238 | + elif password != 'true': |
| 239 | + print(f'Your password is incorrect. Try again.') |
| 240 | + elif name != item: |
| 241 | + print(f'- Name does not match. Testing the next item in the list for {name}...') |
| 242 | +``` |
| 243 | + |
| 244 | +```output |
| 245 | +Wang |
| 246 | +- Name does not match. Testing the next item in the list for Martin... |
| 247 | +Garcia |
| 248 | +- Name does not match. Testing the next item in the list for Martin... |
| 249 | +Martin |
| 250 | +Login successful! |
| 251 | +``` |
| 252 | + |
| 253 | +::::::::::::::::::::::::: |
| 254 | + |
| 255 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 256 | + |
| 257 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 258 | + |
| 259 | +## Processing Files Based on Record Length |
| 260 | + |
| 261 | +Modify this program so that it only processes files with fewer than 85 records. |
| 262 | + |
| 263 | +```python |
| 264 | +import glob |
| 265 | +import pandas |
| 266 | +for filename in glob.glob('data/*.csv'): |
| 267 | + contents = pandas.read_csv(filename) |
| 268 | + ____ ___(______) < ____: |
| 269 | + print(f'{filename} : {len(contents)}') |
| 270 | +``` |
| 271 | + |
| 272 | +::::::::::::::: solution |
| 273 | + |
| 274 | +## Solution |
| 275 | + |
| 276 | +```python |
| 277 | +import glob |
| 278 | +import pandas |
| 279 | +for filename in glob.glob('data/*.csv'): |
| 280 | + contents = pandas.read_csv(filename) |
| 281 | + if len(contents) < 85: |
| 282 | + print(f'{filename} : {len(contents)}') |
| 283 | +``` |
| 284 | + |
| 285 | +::::::::::::::::::::::::: |
| 286 | + |
| 287 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 288 | + |
| 289 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 290 | + |
| 291 | +- Use `if` statements to control whether or not a block of code is executed. |
| 292 | +- Conditionals are often used inside loops. |
| 293 | +- Use `else` to execute a block of code when an `if` condition is *not* true. |
| 294 | +- Use `elif` to specify additional tests. |
| 295 | +- Conditions are tested once, in order. |
| 296 | +- Use `and` and `or` to check against multiple value statements. |
| 297 | + |
| 298 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 299 | + |
| 300 | + |
0 commit comments