Skip to content

Commit 0369138

Browse files
authored
Merge pull request #61 from DurhamARC-Training/functions_and_formatting_improvement_spring_2026
Functions and formatting improvement spring 2026
2 parents 1b83958 + 82d4adc commit 0369138

2 files changed

Lines changed: 340 additions & 141 deletions

File tree

Basics.ipynb

Lines changed: 165 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@
11891189
},
11901190
{
11911191
"cell_type": "markdown",
1192-
"id": "2f3d5124",
1192+
"id": "c1b5c739",
11931193
"metadata": {
11941194
"editable": false,
11951195
"slideshow": {
@@ -1198,33 +1198,26 @@
11981198
"tags": []
11991199
},
12001200
"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"
12091202
]
12101203
},
12111204
{
12121205
"cell_type": "markdown",
1213-
"id": "c1b5c739",
1206+
"id": "3f6b80d9",
12141207
"metadata": {
12151208
"editable": false,
12161209
"slideshow": {
1217-
"slide_type": "slide"
1210+
"slide_type": ""
12181211
},
12191212
"tags": []
12201213
},
12211214
"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."
12231216
]
12241217
},
12251218
{
12261219
"cell_type": "markdown",
1227-
"id": "3f6b80d9",
1220+
"id": "383ac2a1",
12281221
"metadata": {
12291222
"editable": false,
12301223
"slideshow": {
@@ -1233,23 +1226,67 @@
12331226
"tags": []
12341227
},
12351228
"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."
12371232
]
12381233
},
12391234
{
12401235
"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",
12421280
"metadata": {
1243-
"editable": false,
12441281
"slideshow": {
1245-
"slide_type": ""
1282+
"slide_type": "slide"
12461283
},
1247-
"tags": []
1284+
"editable": false
12481285
},
12491286
"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:"
12531290
]
12541291
},
12551292
{
@@ -1264,16 +1301,66 @@
12641301
"tags": []
12651302
},
12661303
"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": [],
12671329
"source": [
12681330
"# Function to add two numbers\n",
12691331
"def add_numbers(a, b):\n",
1270-
" return a + b\n",
1332+
" total = a + b\n",
1333+
" return total\n",
12711334
"\n",
12721335
"# Call the function\n",
1273-
"result = add_numbers(3, 4)\n",
1336+
"result = add_numbers(17, 25)\n",
12741337
"print(result)"
12751338
]
12761339
},
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+
},
12771364
{
12781365
"cell_type": "markdown",
12791366
"id": "2a8095f4",
@@ -1301,12 +1388,39 @@
13011388
"source": [
13021389
"### _Exercises_\n",
13031390
"\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",
13051392
"$\n",
13061393
"\\text{Fahrenheit} = (\\text{Celsius} \\times \\frac{9}{5}) + 32\n",
13071394
"$"
13081395
]
13091396
},
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+
},
13101424
{
13111425
"cell_type": "code",
13121426
"execution_count": null,
@@ -1420,6 +1534,26 @@
14201534
"print(\"Hello \" + name)"
14211535
]
14221536
},
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+
},
14231557
{
14241558
"cell_type": "markdown",
14251559
"id": "81998d75",
@@ -1470,7 +1604,7 @@
14701604
"tags": []
14711605
},
14721606
"source": [
1473-
"### Writing files and formatting strings (C-style)"
1607+
"### Writing files"
14741608
]
14751609
},
14761610
{
@@ -1488,25 +1622,11 @@
14881622
"with open(\"testfile.txt\", \"w\") as my_file:\n",
14891623
" # Write - note special characters!\n",
14901624
" 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",
14921626
"\n",
14931627
" my_file.write(str(222))"
14941628
]
14951629
},
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-
},
15101630
{
15111631
"cell_type": "markdown",
15121632
"id": "16c995a4",
@@ -1518,7 +1638,7 @@
15181638
"tags": []
15191639
},
15201640
"source": [
1521-
"### Writing files (f-strings)"
1641+
"### Writing files (append mode)"
15221642
]
15231643
},
15241644
{
@@ -1534,26 +1654,12 @@
15341654
},
15351655
"outputs": [],
15361656
"source": [
1537-
"number1 = 44\n",
1657+
"number1 = 42\n",
15381658
"number2 = 111\n",
15391659
"\n",
15401660
"with open(\"testfile.txt\", \"a\") as my_file:\n",
15411661
" # 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}.\")"
15571663
]
15581664
},
15591665
{
@@ -3463,7 +3569,7 @@
34633569
],
34643570
"metadata": {
34653571
"kernelspec": {
3466-
"display_name": "Python 3 (ipykernel)",
3572+
"display_name": "Python 3",
34673573
"language": "python",
34683574
"name": "python3"
34693575
},
@@ -3477,7 +3583,7 @@
34773583
"name": "python",
34783584
"nbconvert_exporter": "python",
34793585
"pygments_lexer": "ipython3",
3480-
"version": "3.13.2"
3586+
"version": "3.14.3"
34813587
}
34823588
},
34833589
"nbformat": 4,

0 commit comments

Comments
 (0)