|
1189 | 1189 | }, |
1190 | 1190 | { |
1191 | 1191 | "cell_type": "markdown", |
1192 | | - "id": "2f3d5124", |
| 1192 | + "id": "c1b5c739", |
1193 | 1193 | "metadata": { |
1194 | 1194 | "editable": false, |
1195 | 1195 | "slideshow": { |
|
1198 | 1198 | "tags": [] |
1199 | 1199 | }, |
1200 | 1200 | "source": [ |
1201 | | - "## Interlude: Indented blocks in python\n", |
1202 | | - "Python uses indention to separate blocs after a `:`\n", |
1203 | | - "```python\n", |
1204 | | - "some_functionality_on_block:\n", |
1205 | | - " first_statement_in_block()\n", |
1206 | | - " second_statement_in_block()\n", |
1207 | | - "statement_not_in_block()\n", |
1208 | | - "```\n" |
| 1201 | + "# <ins>2.</ins> Functions" |
1209 | 1202 | ] |
1210 | 1203 | }, |
1211 | 1204 | { |
1212 | 1205 | "cell_type": "markdown", |
1213 | | - "id": "c1b5c739", |
| 1206 | + "id": "3f6b80d9", |
1214 | 1207 | "metadata": { |
1215 | 1208 | "editable": false, |
1216 | 1209 | "slideshow": { |
1217 | | - "slide_type": "slide" |
| 1210 | + "slide_type": "" |
1218 | 1211 | }, |
1219 | 1212 | "tags": [] |
1220 | 1213 | }, |
1221 | 1214 | "source": [ |
1222 | | - "# <ins>2.</ins> Functions" |
| 1215 | + "A function is a block of reusable code that performs a specific task. Functions help reduce repetition and make code easier to manage." |
1223 | 1216 | ] |
1224 | 1217 | }, |
1225 | 1218 | { |
1226 | 1219 | "cell_type": "markdown", |
1227 | | - "id": "3f6b80d9", |
| 1220 | + "id": "383ac2a1", |
1228 | 1221 | "metadata": { |
1229 | 1222 | "editable": false, |
1230 | 1223 | "slideshow": { |
|
1233 | 1226 | "tags": [] |
1234 | 1227 | }, |
1235 | 1228 | "source": [ |
1236 | | - "A function is a block of reusable code that performs a specific task. Functions help reduce repetition and make code easier to manage. A function is defined using the `def` keyword." |
| 1229 | + "* You can pass data to functions (parameters).\n", |
| 1230 | + "* Functions can return values.\n", |
| 1231 | + "* Functions help break down complex programs into smaller, manageable parts." |
1237 | 1232 | ] |
1238 | 1233 | }, |
1239 | 1234 | { |
1240 | 1235 | "cell_type": "markdown", |
1241 | | - "id": "383ac2a1", |
| 1236 | + "id": "ca147ce4", |
| 1237 | + "metadata": { |
| 1238 | + "editable": false |
| 1239 | + }, |
| 1240 | + "source": [ |
| 1241 | + "We have already encountered a function - `print`. Here are some more examples:" |
| 1242 | + ] |
| 1243 | + }, |
| 1244 | + { |
| 1245 | + "cell_type": "code", |
| 1246 | + "execution_count": null, |
| 1247 | + "id": "d1f13779", |
| 1248 | + "metadata": {}, |
| 1249 | + "outputs": [], |
| 1250 | + "source": [ |
| 1251 | + "positive = abs(-42)\n", |
| 1252 | + "print(positive)\n", |
| 1253 | + "\n", |
| 1254 | + "# We've also encountered type(), but what about if we *change* the type of a variable?\n", |
| 1255 | + "stringified = str(42)\n", |
| 1256 | + "type_of_stringified = type(stringified)\n", |
| 1257 | + "print(type_of_stringified)\n", |
| 1258 | + "print(stringified == \"42\")\n", |
| 1259 | + "\n", |
| 1260 | + "# Hey look, it's floating! Just like a dolphin!\n", |
| 1261 | + "floatified = float(42)\n", |
| 1262 | + "print(floatified)\n", |
| 1263 | + "\n", |
| 1264 | + "# Functions can take multiple arguments, separated, by, commas:\n", |
| 1265 | + "numbers = range(3, 10)\n", |
| 1266 | + "sum_of_numbers = sum(numbers)\n", |
| 1267 | + "print(sum_of_numbers)\n", |
| 1268 | + "\n", |
| 1269 | + "\n", |
| 1270 | + "# What's That Keyword? (We're not going to talk about that keyword (today))\n", |
| 1271 | + "import random\n", |
| 1272 | + "\n", |
| 1273 | + "random_number = random.randint(0, 100)\n", |
| 1274 | + "print(random_number)" |
| 1275 | + ] |
| 1276 | + }, |
| 1277 | + { |
| 1278 | + "cell_type": "markdown", |
| 1279 | + "id": "696b6e90", |
1242 | 1280 | "metadata": { |
1243 | | - "editable": false, |
1244 | 1281 | "slideshow": { |
1245 | | - "slide_type": "" |
| 1282 | + "slide_type": "slide" |
1246 | 1283 | }, |
1247 | | - "tags": [] |
| 1284 | + "editable": false |
1248 | 1285 | }, |
1249 | 1286 | "source": [ |
1250 | | - "* You can pass data to functions (parameters).\n", |
1251 | | - "* Functions can return values.\n", |
1252 | | - "* Functions help break down complex programs into smaller, manageable parts." |
| 1287 | + "We can make functions with our own code as well.\n", |
| 1288 | + "\n", |
| 1289 | + "A function is defined using the `def` keyword:" |
1253 | 1290 | ] |
1254 | 1291 | }, |
1255 | 1292 | { |
|
1264 | 1301 | "tags": [] |
1265 | 1302 | }, |
1266 | 1303 | "outputs": [], |
| 1304 | + "source": [ |
| 1305 | + "# Function to add two numbers and print it out\n", |
| 1306 | + "def add_numbers(a, b):\n", |
| 1307 | + " print(a + b)\n", |
| 1308 | + "\n", |
| 1309 | + "# Call the function\n", |
| 1310 | + "add_numbers(3, 4)" |
| 1311 | + ] |
| 1312 | + }, |
| 1313 | + { |
| 1314 | + "cell_type": "markdown", |
| 1315 | + "id": "ddd2d577", |
| 1316 | + "metadata": { |
| 1317 | + "editable": false |
| 1318 | + }, |
| 1319 | + "source": [ |
| 1320 | + "To get a value out of a function, we use the `return` keyword:" |
| 1321 | + ] |
| 1322 | + }, |
| 1323 | + { |
| 1324 | + "cell_type": "code", |
| 1325 | + "execution_count": null, |
| 1326 | + "id": "0c93d094", |
| 1327 | + "metadata": {}, |
| 1328 | + "outputs": [], |
1267 | 1329 | "source": [ |
1268 | 1330 | "# Function to add two numbers\n", |
1269 | 1331 | "def add_numbers(a, b):\n", |
1270 | | - " return a + b\n", |
| 1332 | + " total = a + b\n", |
| 1333 | + " return total\n", |
1271 | 1334 | "\n", |
1272 | 1335 | "# Call the function\n", |
1273 | | - "result = add_numbers(3, 4)\n", |
| 1336 | + "result = add_numbers(17, 25)\n", |
1274 | 1337 | "print(result)" |
1275 | 1338 | ] |
1276 | 1339 | }, |
| 1340 | + { |
| 1341 | + "cell_type": "markdown", |
| 1342 | + "id": "09668f03", |
| 1343 | + "metadata": { |
| 1344 | + "slideshow": { |
| 1345 | + "slide_type": "slide" |
| 1346 | + }, |
| 1347 | + "editable": false |
| 1348 | + }, |
| 1349 | + "source": [ |
| 1350 | + "## Interlude: Indented blocks in python\n", |
| 1351 | + "Python uses indentation to separate blocs after a `:`\n", |
| 1352 | + "```python\n", |
| 1353 | + "def this_is_a_function(my_number):\n", |
| 1354 | + " if my_number == 42:\n", |
| 1355 | + " print(\"You already got it!\")\n", |
| 1356 | + " return my_number\n", |
| 1357 | + "\n", |
| 1358 | + " the_answer = my_number + 42\n", |
| 1359 | + " return the_answer\n", |
| 1360 | + "```\n", |
| 1361 | + "Indentation is done with 2 or 4 spaces, and many editors will map the `TAB` key to insert spaces rather than a tab character." |
| 1362 | + ] |
| 1363 | + }, |
1277 | 1364 | { |
1278 | 1365 | "cell_type": "markdown", |
1279 | 1366 | "id": "2a8095f4", |
|
1301 | 1388 | "source": [ |
1302 | 1389 | "### _Exercises_\n", |
1303 | 1390 | "\n", |
1304 | | - "**1)** Write a function that converts Celsius to Fahrenheit. Use the formula \n", |
| 1391 | + "**1)** Use the `add_numbers` function to convert Celsius to Fahrenheit. Use the formula \n", |
1305 | 1392 | "$\n", |
1306 | 1393 | "\\text{Fahrenheit} = (\\text{Celsius} \\times \\frac{9}{5}) + 32\n", |
1307 | 1394 | "$" |
1308 | 1395 | ] |
1309 | 1396 | }, |
| 1397 | + { |
| 1398 | + "cell_type": "code", |
| 1399 | + "execution_count": null, |
| 1400 | + "id": "b7dd742f", |
| 1401 | + "metadata": { |
| 1402 | + "editable": true, |
| 1403 | + "remove_code": "after:# Convert to fahrenheit:" |
| 1404 | + }, |
| 1405 | + "outputs": [], |
| 1406 | + "source": [ |
| 1407 | + "# The temperature in Celsius\n", |
| 1408 | + "celsius = 20\n", |
| 1409 | + "\n", |
| 1410 | + "# Convert to fahrenheit:\n" |
| 1411 | + ] |
| 1412 | + }, |
| 1413 | + { |
| 1414 | + "cell_type": "markdown", |
| 1415 | + "id": "5297733c", |
| 1416 | + "metadata": { |
| 1417 | + "editable": false |
| 1418 | + }, |
| 1419 | + "source": [ |
| 1420 | + "\n", |
| 1421 | + "**2)** Write a function of your own that converts Celsius to Fahrenheit." |
| 1422 | + ] |
| 1423 | + }, |
1310 | 1424 | { |
1311 | 1425 | "cell_type": "code", |
1312 | 1426 | "execution_count": null, |
|
1420 | 1534 | "print(\"Hello \" + name)" |
1421 | 1535 | ] |
1422 | 1536 | }, |
| 1537 | + { |
| 1538 | + "cell_type": "markdown", |
| 1539 | + "id": "12d99e16", |
| 1540 | + "metadata": { |
| 1541 | + "editable": false |
| 1542 | + }, |
| 1543 | + "source": [ |
| 1544 | + "## Interlude: String Formatting\n", |
| 1545 | + "\n", |
| 1546 | + "Python has a magic thing called `f-strings` (formatted string literals), which occur when you prefix a string with the letter `f`:\n", |
| 1547 | + "```python\n", |
| 1548 | + "f\"This is an f-string! But it doesn't do anything special yet...\n", |
| 1549 | + "```\n", |
| 1550 | + "This magic allows us to format variables of different types into a string, letting us write them out concisely. We do this by putting an expression inside curly { braces } inside the f-string:\n", |
| 1551 | + "```python\n", |
| 1552 | + "number = 10\n", |
| 1553 | + "print(f\"My number is {number}!\")\n", |
| 1554 | + "```" |
| 1555 | + ] |
| 1556 | + }, |
1423 | 1557 | { |
1424 | 1558 | "cell_type": "markdown", |
1425 | 1559 | "id": "81998d75", |
|
1470 | 1604 | "tags": [] |
1471 | 1605 | }, |
1472 | 1606 | "source": [ |
1473 | | - "### Writing files and formatting strings (C-style)" |
| 1607 | + "### Writing files" |
1474 | 1608 | ] |
1475 | 1609 | }, |
1476 | 1610 | { |
|
1488 | 1622 | "with open(\"testfile.txt\", \"w\") as my_file:\n", |
1489 | 1623 | " # Write - note special characters!\n", |
1490 | 1624 | " my_file.write(\"This is some text. \\n And some more.\")\n", |
1491 | | - " my_file.write(\"\\n\\nI can also add numbers like this: %d %d \\n\" %(22, 333))\n", |
| 1625 | + " my_file.write(f\"\\n\\nI can also write a number: {42}\\n\")\n", |
1492 | 1626 | "\n", |
1493 | 1627 | " my_file.write(str(222))" |
1494 | 1628 | ] |
1495 | 1629 | }, |
1496 | | - { |
1497 | | - "cell_type": "markdown", |
1498 | | - "id": "0d884b3f", |
1499 | | - "metadata": { |
1500 | | - "editable": false, |
1501 | | - "slideshow": { |
1502 | | - "slide_type": "" |
1503 | | - }, |
1504 | | - "tags": [] |
1505 | | - }, |
1506 | | - "source": [ |
1507 | | - "see also [https://www.learnpython.org/en/String\\_Formatting](https://www.learnpython.org/en/String_Formatting)" |
1508 | | - ] |
1509 | | - }, |
1510 | 1630 | { |
1511 | 1631 | "cell_type": "markdown", |
1512 | 1632 | "id": "16c995a4", |
|
1518 | 1638 | "tags": [] |
1519 | 1639 | }, |
1520 | 1640 | "source": [ |
1521 | | - "### Writing files (f-strings)" |
| 1641 | + "### Writing files (append mode)" |
1522 | 1642 | ] |
1523 | 1643 | }, |
1524 | 1644 | { |
|
1534 | 1654 | }, |
1535 | 1655 | "outputs": [], |
1536 | 1656 | "source": [ |
1537 | | - "number1 = 44\n", |
| 1657 | + "number1 = 42\n", |
1538 | 1658 | "number2 = 111\n", |
1539 | 1659 | "\n", |
1540 | 1660 | "with open(\"testfile.txt\", \"a\") as my_file:\n", |
1541 | 1661 | " # Append to the opened file\n", |
1542 | | - " my_file.write(f\"\\n I have opened the same file again.\\n More numbers: {number1} {number2}.\")" |
1543 | | - ] |
1544 | | - }, |
1545 | | - { |
1546 | | - "cell_type": "markdown", |
1547 | | - "id": "4db7b7e2", |
1548 | | - "metadata": { |
1549 | | - "editable": false, |
1550 | | - "slideshow": { |
1551 | | - "slide_type": "" |
1552 | | - }, |
1553 | | - "tags": [] |
1554 | | - }, |
1555 | | - "source": [ |
1556 | | - "see also [f-strings](https://realpython.com/python-f-strings/)" |
| 1662 | + " my_file.write(f\"\\nI have opened the same file again.\\nMore numbers: {number1} {number2}.\")" |
1557 | 1663 | ] |
1558 | 1664 | }, |
1559 | 1665 | { |
|
3463 | 3569 | ], |
3464 | 3570 | "metadata": { |
3465 | 3571 | "kernelspec": { |
3466 | | - "display_name": "Python 3 (ipykernel)", |
| 3572 | + "display_name": "Python 3", |
3467 | 3573 | "language": "python", |
3468 | 3574 | "name": "python3" |
3469 | 3575 | }, |
|
3477 | 3583 | "name": "python", |
3478 | 3584 | "nbconvert_exporter": "python", |
3479 | 3585 | "pygments_lexer": "ipython3", |
3480 | | - "version": "3.13.2" |
| 3586 | + "version": "3.14.3" |
3481 | 3587 | } |
3482 | 3588 | }, |
3483 | 3589 | "nbformat": 4, |
|
0 commit comments