|
| 1 | +--- |
| 2 | +format: live-html |
| 3 | +--- |
| 4 | + |
| 5 | +<script src='../../src/quiz.js'></script> |
| 6 | + |
| 7 | +# 1.1. Exercises |
| 8 | + |
| 9 | +## Name That Data Type |
| 10 | + |
| 11 | +For the next few questions, name the data type of each value. |
| 12 | + |
| 13 | +<div id='mcq1'></div> |
| 14 | +<script> |
| 15 | + generateQuiz( |
| 16 | + 'mcq1', |
| 17 | + 'Question 1', |
| 18 | + '<code>question_1 = ’NaN’</code>', |
| 19 | + { |
| 20 | + '<code>int</code>': 'Do you notice the quotations?', |
| 21 | + '<code>float</code>': 'Do you notice the quotations?', |
| 22 | + '<code>NoneType</code>': 'Do you notice the quotations?', |
| 23 | + '<code>bool</code>': 'Do you notice the quotations?', |
| 24 | + '<code>str</code>': 'Although, this spells out "NaN", The value is within quotations indicating it is a string.', |
| 25 | + }, |
| 26 | + '<code>str</code>', |
| 27 | + ); |
| 28 | +</script> |
| 29 | + |
| 30 | +<div id='mcq2'></div> |
| 31 | +<script> |
| 32 | + generateQuiz( |
| 33 | + 'mcq2', |
| 34 | + 'Question 2', |
| 35 | + '<code>question_1 = 3.6</code>', |
| 36 | + { |
| 37 | + '<code>int</code>': 'We are not trying to trick you here.', |
| 38 | + '<code>float</code>': 'Nothing tricky about this one.', |
| 39 | + '<code>NoneType</code>': 'We are not trying to trick you here.', |
| 40 | + '<code>bool</code>': 'We are not trying to trick you here.', |
| 41 | + '<code>str</code>': 'We are not trying to trick you here.', |
| 42 | + }, |
| 43 | + '<code>float</code>', |
| 44 | + ); |
| 45 | +</script> |
| 46 | + |
| 47 | +<div id='mcq3'></div> |
| 48 | +<script> |
| 49 | + generateQuiz( |
| 50 | + 'mcq3', |
| 51 | + 'Question 3', |
| 52 | + 'Which data type can only take on 1 value?', |
| 53 | + { |
| 54 | + '<code>int</code>': 'You may want to look over this before moving forward.', |
| 55 | + '<code>float</code>': 'You may want to look over this before moving forward.', |
| 56 | + '<code>NoneType</code>': 'Yes, this can only have the value <code>None</code>.', |
| 57 | + '<code>bool</code>': 'This can actually take 2 values!', |
| 58 | + '<code>str</code>': 'You may want to look over this before moving forward.', |
| 59 | + }, |
| 60 | + '<code>NoneType</code>', |
| 61 | + ); |
| 62 | +</script> |
| 63 | + |
| 64 | + |
| 65 | +## Coding questions |
| 66 | + |
| 67 | +**Instructions:** |
| 68 | + |
| 69 | +Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes. |
| 70 | + |
| 71 | +**When you see `____` in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.** |
| 72 | + |
| 73 | + |
| 74 | +### String Verbs |
| 75 | + |
| 76 | +How many times do the lyrics "Let it be" occur in the Beatles Hit song of the same title? |
| 77 | + |
| 78 | +**Tasks:** |
| 79 | + |
| 80 | +- Use some of the string verbs you learned in the lecture to count all the times "let it be" (all upper and lower case versions) appears in the string `lyrics`. |
| 81 | +- Save it in an object named `letitbe_count`. |
| 82 | + |
| 83 | +```{pyodide} |
| 84 | +#| setup: true |
| 85 | +#| exercise: string_verbs |
| 86 | +import pandas as pd |
| 87 | +``` |
| 88 | + |
| 89 | +```{pyodide} |
| 90 | +#| exercise: string_verbs |
| 91 | +lyrics = '''When I find myself in times of trouble Mother Mary comes to me |
| 92 | + Speaking words of wisdom Let it be And in my hour of darkness |
| 93 | + She is standing right in front of me Speaking words of wisdom Let it be |
| 94 | + Let it be, let it be, let it be, let it be |
| 95 | + Whisper words of wisdom Let it be |
| 96 | + And when the broken - hearted people Living in the world agree |
| 97 | + There will be an answer Let it be |
| 98 | + For though they may be parted there is Still a chance that they will see |
| 99 | + There will be an answer Let it be |
| 100 | + Let it be, let it be, let it be, let it be Yeah, there will be an answer Let it be |
| 101 | + Let it be, let it be, let it be, let it be Whisper words of wisdom Let it be |
| 102 | + Let it be, let it be, let it be, let it be Whisper words of wisdom Let it be |
| 103 | + And when the night is cloudy There is still a light that shines on me |
| 104 | + Shine until tomorrow Let it be |
| 105 | + I wake up to the sound of music Mother Mary comes to me |
| 106 | + Speaking words of wisdom Let it be |
| 107 | + Let it be, let it be, let it be, yeah, let it be There will be an answer Let it be |
| 108 | + Let it be, let it be, let it be, yeah, let it be There will be an answer Let it be |
| 109 | + Let it be, let it be, let it be, yeah, let it be Whisper words of wisdom Let it be''' |
| 110 | +
|
| 111 | +# Use some of the string verbs you learn in lecture to count all the times |
| 112 | +# "let it be" (all upper and lower case versions) is said in the Beatles hit song. |
| 113 | +# Save it in an object named letitbe_count |
| 114 | +____ = ____.____().____(____) |
| 115 | +
|
| 116 | +____ |
| 117 | +``` |
| 118 | + |
| 119 | +```{pyodide} |
| 120 | +#| exercise: string_verbs |
| 121 | +#| check: true |
| 122 | +from src.utils import print_correct_msg |
| 123 | +assert result == 41, "The occurences of 'let it be' is incorrect. Please try again." |
| 124 | +print_correct_msg() |
| 125 | +``` |
| 126 | + |
| 127 | +:::: { .hint exercise="string_verbs"} |
| 128 | +::: { .callout-note collapse="false"} |
| 129 | + |
| 130 | +## Hint 1 |
| 131 | + |
| 132 | +- Are you converting `lyrics` to lowercase using `.lower()`? |
| 133 | +- Are you using `.count()` with the argument `let it be`? |
| 134 | + |
| 135 | +::: |
| 136 | +:::: |
| 137 | + |
| 138 | +:::: { .solution exercise="string_verbs" } |
| 139 | +::: { .callout-tip collapse="false"} |
| 140 | + |
| 141 | +## Fully worked solution: |
| 142 | + |
| 143 | +```{pyodide} |
| 144 | +lyrics = '''When I find myself in times of trouble Mother Mary comes to me |
| 145 | + Speaking words of wisdom Let it be And in my hour of darkness |
| 146 | + She is standing right in front of me Speaking words of wisdom Let it be |
| 147 | + Let it be, let it be, let it be, let it be |
| 148 | + Whisper words of wisdom Let it be |
| 149 | + And when the broken - hearted people Living in the world agree |
| 150 | + There will be an answer Let it be |
| 151 | + For though they may be parted there is Still a chance that they will see |
| 152 | + There will be an answer Let it be |
| 153 | + Let it be, let it be, let it be, let it be Yeah, there will be an answer Let it be |
| 154 | + Let it be, let it be, let it be, let it be Whisper words of wisdom Let it be |
| 155 | + Let it be, let it be, let it be, let it be Whisper words of wisdom Let it be |
| 156 | + And when the night is cloudy There is still a light that shines on me |
| 157 | + Shine until tomorrow Let it be |
| 158 | + I wake up to the sound of music Mother Mary comes to me |
| 159 | + Speaking words of wisdom Let it be |
| 160 | + Let it be, let it be, let it be, yeah, let it be There will be an answer Let it be |
| 161 | + Let it be, let it be, let it be, yeah, let it be There will be an answer Let it be |
| 162 | + Let it be, let it be, let it be, yeah, let it be Whisper words of wisdom Let it be''' |
| 163 | +
|
| 164 | +# Use some of the string verbs you learn in lecture to count all the times |
| 165 | +# "let it be" (all upper and lower case versions) is said in the Beatles hit song. |
| 166 | +# Save it in an object named letitbe_count |
| 167 | +letitbe_count = lyrics.lower().count('let it be') |
| 168 | +
|
| 169 | +letitbe_count |
| 170 | +``` |
| 171 | + |
| 172 | +::: |
| 173 | +:::: |
| 174 | + |
| 175 | +<br> |
| 176 | + |
| 177 | +### Casting Data Types |
| 178 | + |
| 179 | +How can we cast from a string to an integer value? |
| 180 | + |
| 181 | +**Tasks:** |
| 182 | + |
| 183 | +- Convert `string1` to a `float` in an object named `pi`. |
| 184 | +- Convert the object `pi` now into an `int` named `pi_slice`. |
| 185 | + |
| 186 | +```{pyodide} |
| 187 | +#| exercise: casting_data_types_a |
| 188 | +# Convert the following object into the types specified |
| 189 | +
|
| 190 | +# Convert string1 to a float in an object named pi |
| 191 | +string1 = '3.14' |
| 192 | +____ = ____ |
| 193 | +____ |
| 194 | +``` |
| 195 | + |
| 196 | +```{pyodide} |
| 197 | +#| exercise: casting_data_types_a |
| 198 | +#| check: true |
| 199 | +from src.utils import print_correct_msg |
| 200 | +
|
| 201 | +assert isinstance(result, float), "Make sure you are converting 'pi' to a float using the 'float()' function." |
| 202 | +print_correct_msg() |
| 203 | +``` |
| 204 | + |
| 205 | +:::: { .hint exercise="casting_data_types_a"} |
| 206 | +::: { .callout-note collapse="false"} |
| 207 | + |
| 208 | +## Hint 1 |
| 209 | + |
| 210 | +- Are you using `float()`? |
| 211 | + |
| 212 | +::: |
| 213 | +:::: |
| 214 | + |
| 215 | +:::: { .solution exercise="casting_data_types_a" } |
| 216 | +::: { .callout-tip collapse="false"} |
| 217 | + |
| 218 | +## Fully worked solution: |
| 219 | + |
| 220 | +```{pyodide} |
| 221 | +# Convert the following object into the types specified |
| 222 | +
|
| 223 | +# Convert string1 to a float in an object named pi |
| 224 | +string1 = '3.14' |
| 225 | +pi = float(string1) |
| 226 | +pi |
| 227 | +``` |
| 228 | + |
| 229 | +::: |
| 230 | +:::: |
| 231 | + |
| 232 | +<br> |
| 233 | + |
| 234 | +```{pyodide} |
| 235 | +#| setup: true |
| 236 | +#| exercise: casting_data_types_b |
| 237 | +pi = float('3.14') |
| 238 | +``` |
| 239 | + |
| 240 | +```{pyodide} |
| 241 | +#| exercise: casting_data_types_b |
| 242 | +# Convert the following object into the types specified |
| 243 | +
|
| 244 | +# Convert pi into an int named pi_slice |
| 245 | +____ = ____ |
| 246 | +____ |
| 247 | +``` |
| 248 | + |
| 249 | +```{pyodide} |
| 250 | +#| exercise: casting_data_types_b |
| 251 | +#| check: true |
| 252 | +from src.utils import print_correct_msg |
| 253 | +
|
| 254 | +assert isinstance(result, int), "Make sure you are converting 'pi_slice' to a int using the 'int()' function." |
| 255 | +print_correct_msg() |
| 256 | +``` |
| 257 | + |
| 258 | +:::: { .hint exercise="casting_data_types_b"} |
| 259 | +::: { .callout-note collapse="false"} |
| 260 | + |
| 261 | +## Hint 1 |
| 262 | + |
| 263 | +- Are you using `int()`? |
| 264 | + |
| 265 | +::: |
| 266 | +:::: |
| 267 | + |
| 268 | +:::: { .solution exercise="casting_data_types_b" } |
| 269 | +::: { .callout-tip collapse="false"} |
| 270 | + |
| 271 | +## Fully worked solution: |
| 272 | + |
| 273 | +```{pyodide} |
| 274 | +# Convert the following object into the types specified |
| 275 | +
|
| 276 | +# Convert pi into an int named pi_slice |
| 277 | +pi_slice = int(pi) |
| 278 | +pi_slice |
| 279 | +``` |
| 280 | + |
| 281 | +::: |
| 282 | +:::: |
0 commit comments