|
| 1 | +--- |
| 2 | +title: Python Fundamentals |
| 3 | +teaching: 20 |
| 4 | +exercises: 10 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Assign values to variables. |
| 10 | + |
| 11 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 12 | + |
| 13 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 14 | + |
| 15 | +- What basic data types can I work with in Python? |
| 16 | +- How can I create a new variable in Python? |
| 17 | +- How do I use a function? |
| 18 | +- Can I change the value associated with a variable after I create it? |
| 19 | + |
| 20 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 21 | + |
| 22 | +## Variables |
| 23 | + |
| 24 | +Any Python interpreter can be used as a calculator: |
| 25 | + |
| 26 | +```python |
| 27 | +3 + 5 * 4 |
| 28 | +``` |
| 29 | + |
| 30 | +```output |
| 31 | +23 |
| 32 | +``` |
| 33 | + |
| 34 | +This is great but not very interesting. |
| 35 | +To do anything useful with data, we need to assign its value to a *variable*. |
| 36 | +In Python, we can [assign](../learners/reference.md#assign) a value to a |
| 37 | +[variable](../learners/reference.md#variable), using the equals sign `=`. |
| 38 | +For example, we can track the weight of a patient who weighs 60 kilograms by |
| 39 | +assigning the value `60` to a variable `weight_kg`: |
| 40 | + |
| 41 | +```python |
| 42 | +weight_kg = 60 |
| 43 | +``` |
| 44 | + |
| 45 | +From now on, whenever we use `weight_kg`, Python will substitute the value we assigned to |
| 46 | +it. In layperson's terms, **a variable is a name for a value**. |
| 47 | + |
| 48 | +In Python, variable names: |
| 49 | + |
| 50 | +- can include letters, digits, and underscores |
| 51 | +- cannot start with a digit |
| 52 | +- are [case sensitive](../learners/reference.md#case-sensitive). |
| 53 | + |
| 54 | +This means that, for example: |
| 55 | + |
| 56 | +- `weight0` is a valid variable name, whereas `0weight` is not |
| 57 | +- `weight` and `Weight` are different variables |
| 58 | + |
| 59 | +## Types of data |
| 60 | + |
| 61 | +Python knows various types of data. Three common ones are: |
| 62 | + |
| 63 | +- integer numbers |
| 64 | +- floating point numbers, and |
| 65 | +- strings. |
| 66 | + |
| 67 | +In the example above, variable `weight_kg` has an integer value of `60`. |
| 68 | +If we want to more precisely track the weight of our patient, |
| 69 | +we can use a floating point value by executing: |
| 70 | + |
| 71 | +```python |
| 72 | +weight_kg = 60.3 |
| 73 | +``` |
| 74 | + |
| 75 | +To create a string, we add single or double quotes around some text. |
| 76 | +To identify and track a patient throughout our study, |
| 77 | +we can assign each person a unique identifier by storing it in a string: |
| 78 | + |
| 79 | +```python |
| 80 | +patient_id = '001' |
| 81 | +``` |
| 82 | + |
| 83 | +## Using Variables in Python |
| 84 | + |
| 85 | +Once we have data stored with variable names, we can make use of it in calculations. |
| 86 | +We may want to store our patient's weight in pounds as well as kilograms: |
| 87 | + |
| 88 | +```python |
| 89 | +weight_lb = 2.2 * weight_kg |
| 90 | +``` |
| 91 | + |
| 92 | +We might decide to add a prefix to our patient identifier: |
| 93 | + |
| 94 | +```python |
| 95 | +patient_id = 'inflam_' + patient_id |
| 96 | +``` |
| 97 | + |
| 98 | +## Built-in Python functions |
| 99 | + |
| 100 | +To carry out common tasks with data and variables in Python, |
| 101 | +the language provides us with several built-in [functions](../learners/reference.md#function). |
| 102 | +To display information to the screen, we use the `print` function: |
| 103 | + |
| 104 | +```python |
| 105 | +print(weight_lb) |
| 106 | +print(patient_id) |
| 107 | +``` |
| 108 | + |
| 109 | +```output |
| 110 | +132.66 |
| 111 | +inflam_001 |
| 112 | +``` |
| 113 | + |
| 114 | +When we want to make use of a function, referred to as calling the function, |
| 115 | +we follow its name by parentheses. The parentheses are important: |
| 116 | +if you leave them off, the function doesn't actually run! |
| 117 | +Sometimes you will include values or variables inside the parentheses for the function to use. |
| 118 | +In the case of `print`, |
| 119 | +we use the parentheses to tell the function what value we want to display. |
| 120 | +We will learn more about how functions work and how to create our own in later episodes. |
| 121 | + |
| 122 | +We can display multiple things at once using only one `print` call: |
| 123 | + |
| 124 | +```python |
| 125 | +print(patient_id, 'weight in kilograms:', weight_kg) |
| 126 | +``` |
| 127 | + |
| 128 | +```output |
| 129 | +inflam_001 weight in kilograms: 60.3 |
| 130 | +``` |
| 131 | + |
| 132 | +We can also call a function inside of another |
| 133 | +[function call](../learners/reference.md#function-call). |
| 134 | +For example, Python has a built-in function called `type` that tells you a value's data type: |
| 135 | + |
| 136 | +```python |
| 137 | +print(type(60.3)) |
| 138 | +print(type(patient_id)) |
| 139 | +``` |
| 140 | + |
| 141 | +```output |
| 142 | +<class 'float'> |
| 143 | +<class 'str'> |
| 144 | +``` |
| 145 | + |
| 146 | +Moreover, we can do arithmetic with variables right inside the `print` function: |
| 147 | + |
| 148 | +```python |
| 149 | +print('weight in pounds:', 2.2 * weight_kg) |
| 150 | +``` |
| 151 | + |
| 152 | +```output |
| 153 | +weight in pounds: 132.66 |
| 154 | +``` |
| 155 | + |
| 156 | +The above command, however, did not change the value of `weight_kg`: |
| 157 | + |
| 158 | +```python |
| 159 | +print(weight_kg) |
| 160 | +``` |
| 161 | + |
| 162 | +```output |
| 163 | +60.3 |
| 164 | +``` |
| 165 | + |
| 166 | +To change the value of the `weight_kg` variable, we have to |
| 167 | +**assign** `weight_kg` a new value using the equals `=` sign: |
| 168 | + |
| 169 | +```python |
| 170 | +weight_kg = 65.0 |
| 171 | +print('weight in kilograms is now:', weight_kg) |
| 172 | +``` |
| 173 | + |
| 174 | +```output |
| 175 | +weight in kilograms is now: 65.0 |
| 176 | +``` |
| 177 | + |
| 178 | +::::::::::::::::::::::::::::::::::::::::: callout |
| 179 | + |
| 180 | +## Variables as Sticky Notes |
| 181 | + |
| 182 | +A variable in Python is analogous to a sticky note with a name written on it: |
| 183 | +assigning a value to a variable is like putting that sticky note on a particular value. |
| 184 | + |
| 185 | +{alt='Value of 65.0 with weight\_kg label stuck on it'} |
| 186 | + |
| 187 | +Using this analogy, we can investigate how assigning a value to one variable |
| 188 | +does **not** change values of other, seemingly related, variables. For |
| 189 | +example, let's store the subject's weight in pounds in its own variable: |
| 190 | + |
| 191 | +```python |
| 192 | +# There are 2.2 pounds per kilogram |
| 193 | +weight_lb = 2.2 * weight_kg |
| 194 | +print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb) |
| 195 | +``` |
| 196 | + |
| 197 | +```output |
| 198 | +weight in kilograms: 65.0 and in pounds: 143.0 |
| 199 | +``` |
| 200 | + |
| 201 | +Everything in a line of code following the '#' symbol is a |
| 202 | +[comment](../learners/reference.md#comment) that is ignored by Python. |
| 203 | +Comments allow programmers to leave explanatory notes for other |
| 204 | +programmers or their future selves. |
| 205 | + |
| 206 | +{alt='Value of 65.0 with weight\_kg label stuck on it, and value of 143.0 with weight\_lb label stuck on it'} |
| 207 | + |
| 208 | +Similar to above, the expression `2.2 * weight_kg` is evaluated to `143.0`, |
| 209 | +and then this value is assigned to the variable `weight_lb` (i.e. the sticky |
| 210 | +note `weight_lb` is placed on `143.0`). At this point, each variable is |
| 211 | +"stuck" to completely distinct and unrelated values. |
| 212 | + |
| 213 | +Let's now change `weight_kg`: |
| 214 | + |
| 215 | +```python |
| 216 | +weight_kg = 100.0 |
| 217 | +print('weight in kilograms is now:', weight_kg, 'and weight in pounds is still:', weight_lb) |
| 218 | +``` |
| 219 | + |
| 220 | +```output |
| 221 | +weight in kilograms is now: 100.0 and weight in pounds is still: 143.0 |
| 222 | +``` |
| 223 | + |
| 224 | +{alt='Value of 100.0 with label weight\_kg stuck on it, and value of 143.0 with label weight\_lbstuck on it'} |
| 225 | + |
| 226 | +Since `weight_lb` doesn't "remember" where its value comes from, |
| 227 | +it is not updated when we change `weight_kg`. |
| 228 | + |
| 229 | + |
| 230 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 231 | + |
| 232 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 233 | + |
| 234 | +## Check Your Understanding |
| 235 | + |
| 236 | +What values do the variables `mass` and `age` have after each of the following statements? |
| 237 | +Test your answer by executing the lines. |
| 238 | + |
| 239 | +```python |
| 240 | +mass = 47.5 |
| 241 | +age = 122 |
| 242 | +mass = mass * 2.0 |
| 243 | +age = age - 20 |
| 244 | +``` |
| 245 | + |
| 246 | +::::::::::::::: solution |
| 247 | + |
| 248 | +## Solution |
| 249 | + |
| 250 | +```output |
| 251 | +`mass` holds a value of 47.5, `age` does not exist |
| 252 | +`mass` still holds a value of 47.5, `age` holds a value of 122 |
| 253 | +`mass` now has a value of 95.0, `age`'s value is still 122 |
| 254 | +`mass` still has a value of 95.0, `age` now holds 102 |
| 255 | +``` |
| 256 | + |
| 257 | +::::::::::::::::::::::::: |
| 258 | + |
| 259 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 260 | + |
| 261 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 262 | + |
| 263 | +## Sorting Out References |
| 264 | + |
| 265 | +Python allows you to assign multiple values to multiple variables in one line by separating |
| 266 | +the variables and values with commas. What does the following program print out? |
| 267 | + |
| 268 | +```python |
| 269 | +first, second = 'Grace', 'Hopper' |
| 270 | +third, fourth = second, first |
| 271 | +print(third, fourth) |
| 272 | +``` |
| 273 | + |
| 274 | +::::::::::::::: solution |
| 275 | + |
| 276 | +## Solution |
| 277 | + |
| 278 | +```output |
| 279 | +Hopper Grace |
| 280 | +``` |
| 281 | + |
| 282 | +::::::::::::::::::::::::: |
| 283 | + |
| 284 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 285 | + |
| 286 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 287 | + |
| 288 | +## Seeing Data Types |
| 289 | + |
| 290 | +What are the data types of the following variables? |
| 291 | + |
| 292 | +```python |
| 293 | +planet = 'Earth' |
| 294 | +apples = 5 |
| 295 | +distance = 10.5 |
| 296 | +``` |
| 297 | + |
| 298 | +::::::::::::::: solution |
| 299 | + |
| 300 | +## Solution |
| 301 | + |
| 302 | +```python |
| 303 | +print(type(planet)) |
| 304 | +print(type(apples)) |
| 305 | +print(type(distance)) |
| 306 | +``` |
| 307 | + |
| 308 | +```output |
| 309 | +<class 'str'> |
| 310 | +<class 'int'> |
| 311 | +<class 'float'> |
| 312 | +``` |
| 313 | + |
| 314 | +::::::::::::::::::::::::: |
| 315 | + |
| 316 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 317 | + |
| 318 | + |
| 319 | + |
| 320 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 321 | + |
| 322 | +- Basic data types in Python include integers, strings, and floating-point numbers. |
| 323 | +- Use `variable = value` to assign a value to a variable in order to record it in memory. |
| 324 | +- Variables are created on demand whenever a value is assigned to them. |
| 325 | +- Use `print(something)` to display the value of `something`. |
| 326 | +- Use `# some kind of explanation` to add comments to programs. |
| 327 | +- Built-in functions are always available to use. |
| 328 | + |
| 329 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 330 | + |
| 331 | + |
0 commit comments