From f9f40d3622da07812dc366b61270471f119781ad Mon Sep 17 00:00:00 2001
From: A Naden <46694302+a-naden@users.noreply.github.com>
Date: Tue, 19 May 2026 17:36:25 +0100
Subject: [PATCH 1/2] Removes most of recap, issue #72.
---
Intermediate.ipynb | 1459 ++++++---------------------------------
Intermediate_full.ipynb | 1433 ++++++--------------------------------
2 files changed, 400 insertions(+), 2492 deletions(-)
diff --git a/Intermediate.ipynb b/Intermediate.ipynb
index f9389fd..f5c284d 100644
--- a/Intermediate.ipynb
+++ b/Intermediate.ipynb
@@ -231,7 +231,7 @@
"editable": false
},
"source": [
- "This section is a brief recap of materials covered in the Beginner Python course:\n",
+ "This section is a (very) brief recap of materials covered in the Beginner Python course:\n",
"\n",
"* Data Types, variables, operators\n",
"* Comments\n",
@@ -239,7 +239,8 @@
"* Reading and Writing Files\n",
"* Lists\n",
"* Repetitions and Conditions\n",
- "* Functions\n"
+ "* Functions\n",
+ "Please review the previous course if this seems unfamiliar. [Link to previous course](https://github.com/DurhamARC-Training/BasicProgrammingPython)"
]
},
{
@@ -294,134 +295,151 @@
},
"outputs": [],
"source": [
- "enrolled_students = 728"
+ "enrolled_students = 728\n",
+ "welcome_message = \"Welcome!\"\n",
+ "---\n",
+ "**NOTE:** Values can be overwritten!\n",
+ "\n",
+ "If we define a new `welcome_message`, it changes.\n",
+ "print(welcome_message)"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "1489fa3d",
+ "cell_type": "markdown",
+ "id": "2a63c2dd",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "work_hours = 7.5"
+ "## Data types"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "fb6110d3",
+ "cell_type": "markdown",
+ "id": "7ab44bf2",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "is_loaded = False"
+ "Among the basic ones are `str` strings, `int` integers, `float` floating-point numbers and `bool` booleans.\n",
+ "str - characters, text\n",
+ "msg = \"Welcome to ARC training\"\n",
+ "#int - integer (whole numbers)\n",
+ "attendee_total=123\n",
+ "#float - decimal numbers\n",
+ "pi = 3.14159\n",
+ "#bool - True or False\n",
+ "event = True\n"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "ac31e498",
+ "cell_type": "markdown",
+ "id": "b68a5bbf",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "editable": false
},
- "outputs": [],
"source": [
- "welcome_message = \"Welcome!\""
+ "**Operators** \n",
+ "\n",
+ "- Numerical data: `+`, `-`, `*`, `/`, `%`, `**`, built-in functions `abs`, ...\n",
+ "- Strings: `+`, `\"apple,banana,orange\".split(\",\")`\n",
+ "\n"
]
},
{
"cell_type": "markdown",
- "id": "e1bd297e",
+ "id": "d20cb9b3",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": [],
"editable": false
},
"source": [
- "---\n",
- "**NOTE:** Values can be overwritten!\n",
- "\n",
- "If we define a new `welcome_message`, it changes."
+ "## Comments\n",
+ "Use # to add single line comments.\n",
+ "or use ''' or \"\"\" to start and end multiline comments\n.",
+ "to describe what your code is doing."
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "94b6e716",
+ "cell_type": "markdown",
+ "id": "8ee98ee9",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "welcome_message = \"Welcome, User\""
+ "## User Input\n",
+ "Use `variable = input()` to recieve user input from the terminal"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "d11dd969",
+ "cell_type": "markdown",
+ "id": "52819c95",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "print(welcome_message)"
+ "## Reading and Writing Files"
]
},
{
"cell_type": "markdown",
- "id": "2a63c2dd",
+ "id": "a0966820",
"metadata": {
"slideshow": {
- "slide_type": "slide"
+ "slide_type": ""
},
"tags": [],
"editable": false
},
"source": [
- "## Data types"
+ "**Opening a File**\n",
+ "\n",
+ "[Python Documentation (mode)](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)"
]
},
{
- "cell_type": "markdown",
- "id": "7ab44bf2",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "38966db9",
"metadata": {
"slideshow": {
"slide_type": ""
},
- "tags": [],
- "editable": false
+ "tags": []
},
+ "outputs": [],
"source": [
- "As with any programming language, Python can deal with many different data types. Among the basic ones are `str` strings, `int` integers, `float` floating-point numbers and `bool` booleans.\n",
- "\n",
- "* an example of a _numeric_ value, _integer_ value;\n",
- "* an integer _expression_, a basic building block of a Python _statement_;\n",
- "* normal arithmetic addition"
+ "# Using a with statement\n",
+ "with open('./data/data_file.txt', 'r') as text_file:# use r(ead), (over)w(rite), a(ppend) or r+, w+ (read+write)\n",
+ " content = text_file.read()\n",
+ " print(content)"
]
},
{
"cell_type": "markdown",
- "id": "c33c20f5",
+ "id": "aadba414",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -430,172 +448,142 @@
"editable": false
},
"source": [
- "#### Numerical"
+ "**Reading from a File**\n",
+ "\n",
+ "Here are some file functions for reading from a file:\n",
+ "\n",
+ "* file.read() - Read the entire file content as a newline (\\n) separated string.\n",
+ "* file.readlines() - Read all lines into a List object. Each element in the list will contain one line including its newline character (\\n)."
]
},
{
"cell_type": "markdown",
- "id": "7a797eab",
+ "id": "1f1185e6",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": [],
"editable": false
},
"source": [
- "##### Floating-point representation:\n",
- "* A built-in `float` is typically a double-precision (64-bit) floating-point number, following the IEEE 754 standard\n",
- "\n",
- "| Title | Storage | Smallest Magnitude | Largest Magnitude | Minimum Precision |\n",
- "|-------|---------|--------------------|-------------------|-------------------|\n",
- "| float | 64 bits | 2.22507 \u00d7 10e\u2212308 | 1.79769 \u00d7 10+308 | ~15-17 digits |"
+ "**Writing to a File**"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "c13d9a15",
+ "cell_type": "markdown",
+ "id": "179f132a",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "fragment"
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "pi = 3.14159\n",
- "print(\"Pi =\", pi)"
+ "One way we can write to files is using the `write()` function inside a with block."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "26b8760c",
+ "id": "911d44b3",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "type(pi) # float"
+ "with open(\"./data/testfile.txt\", \"w\") as text_file:\n",
+ " text_file.write(\"Some words \\n\")\n",
+ " text_file.write(str(25))"
]
},
{
"cell_type": "markdown",
- "id": "b9324c88",
+ "id": "b61a8691",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
+ "tags": [],
"editable": false
},
"source": [
- "_NOTE_: Scientific notation is supported!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "3253e3d0",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "avogadros_number = 6.022e23\n",
- "c = 2.998e8\n",
- "print(\"Avogadro's number =\", avogadros_number)\n",
- "print(\"Speed of light =\", c)"
+ "## Lists"
]
},
{
"cell_type": "markdown",
- "id": "397f9f86",
+ "id": "9c82edee",
"metadata": {
"slideshow": {
- "slide_type": "slide"
+ "slide_type": ""
},
+ "tags": [],
"editable": false
},
"source": [
- "##### Integer representation:\n",
- "* No Fixed Bounds. You can store extremely large positive or negative integers, constrained only by the available memory\n",
- "* No explicit smallest or largest representable `int`"
+ "Python can work not only with basic data types mentioned before, but also with compound ones. Among the most commonly used is _lists_ to contain other data types.\n",
+ "\n",
+ "* [List](https://docs.python.org/3/tutorial/datastructures.html#Lists)\n",
+ "\n",
+ "We'll learn about other data structures later today."
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "3342a922",
+ "cell_type": "markdown",
+ "id": "8bb91a15",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "n = 3\n",
- "print(n)"
+ "An ordered list of items, accessed by a numerical index (starting at `0`). Elements within a list can be removed, modified, or accessed by their index, and a list can have values added to it.\n",
+ "\n",
+ "Defined using square brackets `[]`."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "36e671e1",
+ "id": "fdc8600e",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "type(n) # integer"
+ "letter_list = [\"a\", \"b\", \"c\", \"d\", \"e\"] A list of odd letters\n",
+ "print(letter_list)"
]
},
{
"cell_type": "markdown",
- "id": "b68a5bbf",
+ "id": "efc747a1",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
+ "tags": [],
"editable": false
},
"source": [
- "**Numerical Operators** \n",
- "\n",
- "- Numerical data: `+`, `-`, `*`, `/`, `%`, `**`, built-in functions `abs`, ...\n",
- "- Order of execution:\n",
- " 1. `()`\n",
- " 2. `**`\n",
- " 3. `*`, `/`\n",
- " 4. `+`, `-`\n",
- " 5. Left-to-right (except exponentiation!)\n",
- "\n",
- "So, use parenthesis to make sure!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "af62e441",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Addition:\", 1 + 2)"
+ "**List Access** \n",
+ "You can access a list using its index value, starting from `0`."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "7e7ef933",
+ "id": "2b2c4dee",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -603,55 +591,58 @@
},
"outputs": [],
"source": [
- "print(\"Subtraction:\", 1 - 2)"
+ "letter_list[3] # access the 4th element in the list (starts counting at 0)"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "28810d42",
+ "cell_type": "markdown",
+ "id": "b8369a1a",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "print(\"Multiplication:\", 5 * 10)"
+ "**Slicing** \n",
+ "You can also use slicing for specific partial list access."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "8b66a0a4",
+ "id": "e0fac9bf",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "print(\"Division:\", 10 / 5)"
+ "letter_list[1:4:2] # Slicing from index 1 (inclusive), to 4 (exclusive) with a step of 2"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "6984dbe1",
+ "cell_type": "markdown",
+ "id": "fe6ffaf2",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "print(\"Modulus:\", 10 % 3)"
+ "**Empty List** \n",
+ "You can create empty lists:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "74e37711",
+ "id": "0edf3fa6",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -659,12 +650,13 @@
},
"outputs": [],
"source": [
- "print(\"Exponentiation:\", 2 ** 3)"
+ "new_list = [] # An empty list\n",
+ "print(new_list)"
]
},
{
"cell_type": "markdown",
- "id": "a5e3ab30",
+ "id": "f87cfa0f",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -673,43 +665,42 @@
"editable": false
},
"source": [
- "#### String"
+ "## Repetitions and Conditions\n"
]
},
{
"cell_type": "markdown",
- "id": "ba570bf4",
+ "id": "eae67d96",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
+ "tags": [],
"editable": false
},
"source": [
- "A string is a series of characters enclosed in quotes, either single or double, used to represent text."
+ "**Logical Operators** \n",
+ "We can determine conditions based on Boolean logic: using `and`, `or`, `not` to *evaluate* boolean expressions:"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "ea3df5a1",
+ "cell_type": "markdown",
+ "id": "b38b2739",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "fragment"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "s = 'Hello, World!'\n",
- "s = \"Hello, World!\"\n",
- "print(s)"
+ "**AND:**"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "e3c09d2d",
+ "id": "db33e773",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -718,29 +709,32 @@
},
"outputs": [],
"source": [
- "type(s) # string"
+ "a = True\n",
+ "b = False\n",
+ "\n",
+ "a and b\n",
+ "a or b\n",
+ "not a\n"
]
},
{
"cell_type": "markdown",
- "id": "50a4347d",
+ "id": "9303b3ff",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
- "tags": [],
"editable": false
},
"source": [
- "**String Concatenation** \n",
- "\n",
- "We can combine strings together."
+ "**Conditions** \n",
+ "In order to control program flow, and whether or not code is executed, we can do conditions based on variable values."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "d9fadfdc",
+ "id": "a0af6ea1",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -748,14 +742,16 @@
},
"outputs": [],
"source": [
- "# String concatenation\n",
- "hello_world = \"Hello,\" + \" World!\"\n",
- "print(hello_world)"
+ "x = 5\n",
+ "y = 6\n",
+ "\n",
+ "if ((x > 2) and (y < 12)):\n",
+ " print(\"x =\", x, \"is GREATER THAN 2 AND y =\",y ,\"is LESS THAN 12\")\n"
]
},
{
"cell_type": "markdown",
- "id": "d20cb9b3",
+ "id": "3246bbe1",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -764,862 +760,32 @@
"editable": false
},
"source": [
- "## Comments"
+ "**Else/Elif** \n",
+ "\n",
+ "We may also specify either/or with `if/else`, and even add extra conditions with `elif`."
]
},
{
"cell_type": "markdown",
- "id": "7ac031a7",
+ "id": "8c87d3f8",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
"tags": [],
"editable": false
},
"source": [
- "Used to write notes or comments about code, as well as description of what the code is doing, or the variables used."
+ "**Repetitions** \n",
+ "There are a number of ways for us to repeat lines or blocks of code within our program, to do this we use `loops`:\n",
+ "for loops - execute for each item specified in the definition\n",
+ "while loop - execute while condition is met (checked at start of each cycle)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "41b7cdfe",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "# A single-line comment!\n",
- "welcome_message = \"Welcome, User!\"\n",
- "print(welcome_message) # Print welcome message"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "9c50f102",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "'''\n",
- "A multi-line comment!\n",
- "'''\n",
- "print(\"something\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "3f72dedf",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "\"\"\"\n",
- "Another multi-line comment!\n",
- "\"\"\"\n",
- "print(\"something else\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8ee98ee9",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "## User Input"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6b2e3c6e",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "You can use `input()` to read user input: "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "a1178a62",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "inputted_variable = float(input())\n",
- "print(inputted_variable) # Print what we just input!\n",
- "type(inputted_variable)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "52819c95",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "## Reading and Writing Files"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "a0966820",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Opening a File**\n",
- "\n",
- "Two things to note here:\n",
- " - My object `my_file` is different from my file `\"testfile.txt\"`!\n",
- " - There are different modes:\n",
- " - read: `'r'`\n",
- " - (over-)write: `'w'`\n",
- " - append: `'a'`\n",
- " - read+write: `'w+'` or `'r+'`\n",
- "\n",
- "[Python Documentation (mode)](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "38966db9",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "# Using a with statement\n",
- "with open('./data/data_file.txt', 'r') as text_file:\n",
- " content = text_file.read()\n",
- " print(content)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "aadba414",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Reading from a File**\n",
- "\n",
- "Here are some file functions for reading from a file:\n",
- "\n",
- "* file.read() - Read the entire file content as a newline (\\n) separated string.\n",
- "* file.readlines() - Read all lines into a List object. Each element in the list will contain one line including its newline character (\\n)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "7f0a6056",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "with open(\"./data/data_file.txt\") as text_file:\n",
- " print(text_file.read())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "ec567746",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "with open(\"./data/data_file.txt\") as text_file:\n",
- " print(text_file.readlines())"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "1f1185e6",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Writing to a File**"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "179f132a",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "One way we can write to files is using the `write()` function."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "e998f8fe",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "Using the `with` keyword:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "911d44b3",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "with open(\"./data/testfile.txt\", \"w\") as text_file:\n",
- " text_file.write(\"Some words \\n\")\n",
- " text_file.write(str(25))"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b61a8691",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "## Lists"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "9c82edee",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "Python can work not only with basic data types mentioned before, but also with compound ones. Compound data types in Python are a powerful tool for organizing and storing data. Among the most commonly used is _lists_ which we learned about in the beginner's course.\n",
- "\n",
- "* [List](https://docs.python.org/3/tutorial/datastructures.html#Lists)\n",
- "\n",
- "We'll learn about other data structures later today."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8bb91a15",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "An ordered list of items, accessed by a numerical index (starting at `0`). Elements within a list can be removed, modified, or accessed by their index, and a list can have values added to it.\n",
- "\n",
- "Defined using square brackets `[]`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "fdc8600e",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "odd_list = [1, 3, 5, 7, 9] # A list of odd numbers\n",
- "print(odd_list)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "efc747a1",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**List Access** \n",
- "You can access a list using its index value, starting from `0`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2b2c4dee",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "3f87d56b",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "b686d58d",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "odd_list[-1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1f916c68",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[-2]"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b8369a1a",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Slicing** \n",
- "You can also use slicing for specific partial list access."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "e0fac9bf",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "odd_list[1:4] # Slicing from index 1 (inclusive), to 4 (exclusive)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "586edf14",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[3:] # Slicing from index 3 to end."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "9f0dea38",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[:3] # Slicing from beginning to index 3"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "bab8e8a0",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[::2] # Slicing with a step of 2"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "fe6ffaf2",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Empty List** \n",
- "You can create empty lists:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "0edf3fa6",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "new_list = [] # An empty list\n",
- "print(new_list)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "f87cfa0f",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "## Repetitions and Conditions\n",
- "\n",
- "First, let's recap boolean logic. Boolean logic uses just two values: True, and False."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "9ca15865",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "editable": false
- },
- "source": [
- "#### Boolean"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "d7b7abb8",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "b1 = False\n",
- "b2 = True\n",
- "print(b1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "9296bb88",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "type(True) # boolean"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "eae67d96",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Logical Operators** \n",
- "We can determine conditions based on Boolean logic: using `and`, `or`, `not` to *evaluate* boolean expressions:"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b38b2739",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**AND:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "db33e773",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "a = True\n",
- "b = False\n",
- "\n",
- "a and b"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "5c8055b9-5863-486e-a741-de11eee3faa9",
- "metadata": {
- "editable": false
- },
- "source": [
- "We can display the results of the `AND` operation using a \"truth table\":\n",
- "| *and* | True | False |\n",
- "| --------- | ----- | ----- |\n",
- "| __True__ | True | False |\n",
- "| __False__ | False | False |"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8b00b658",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "---\n",
- "**OR:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "725d0173",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "a = True\n",
- "b = False\n",
- "\n",
- "a or b"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4a4f35dc-5107-4589-984a-75f7a929958c",
- "metadata": {
- "editable": false
- },
- "source": [
- "| *or* | True | False |\n",
- "| --------- | ----- | ----- |\n",
- "| __True__ | True | True |\n",
- "| __False__ | True | False |\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6a10fe2f",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "editable": false
- },
- "source": [
- "---\n",
- "**NOT:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "27378611",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "a = True\n",
- "\n",
- "not a"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "bf19d081-ce51-4b07-bc67-94c4569b9ca9",
- "metadata": {
- "editable": false
- },
- "source": [
- "| *not* | True | False |\n",
- "| ----- | ----- | ----- |\n",
- "| | False | True |"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "9303b3ff",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "editable": false
- },
- "source": [
- "**Conditions** \n",
- "In order to control program flow, and whether or not code is executed, we can do conditions based on variable values."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "7fd769b8-28d3-416a-823f-21f7ffcceef7",
- "metadata": {},
- "outputs": [],
- "source": [
- "if a:\n",
- " print(\"a is True!\")\n",
- "else:\n",
- " print(\"a is False.\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "a0af6ea1",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "x = 5\n",
- "\n",
- "if x > 2:\n",
- " print(\"x =\", x, \"is GREATER THAN 2\")\n",
- "\n",
- "if x == 3:\n",
- " print(\"x =\", x, \"is EQUAL TO 3\")\n",
- "\n",
- "if x >= 3:\n",
- " print(\"x =\", x, \"is GREATER THAN or EQUAL TO 3\")\n",
- "\n",
- "if x != 0:\n",
- " print(\"x =\", x, \"IS NOT 0\")\n",
- "\n",
- "if x < 4:\n",
- " print(\"x =\", x, \"is LESS THAN 4\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "3246bbe1",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Else/Elif** \n",
- "\n",
- "We may also specify either/or with `if/else`, and even add extra conditions with `elif`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "8cab77e6",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "x = 10\n",
- "\n",
- "if x > 0:\n",
- " print(\"x is positive\")\n",
- "elif x < 0:\n",
- " print(\"x is negative\")\n",
- "else:\n",
- " print(\"x is zero\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8c87d3f8",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Repetitions** \n",
- "There are a number of ways for us to repeat lines or blocks of code within our program, to do this we use `loops`:"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "095edef4",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "For Each Loop: \n",
- "A for each loop will execute its code for each item specified within the loop definition"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "c7dc2e03",
+ "id": "c7dc2e03",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1628,114 +794,12 @@
},
"outputs": [],
"source": [
+ "#for\n",
"animal_list = [\"Cat\", \"Dog\", \"Bird\"]\n",
"\n",
"for animal in animal_list:\n",
- " print(\"Current animal is:\", animal)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "76e99a09",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "editable": false
- },
- "source": [
- "The above describes `for each animal in animal_list`."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "e5b05a2c",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "---\n",
- "We are able to use a range-based for loop:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "b14f546d",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "for i in range(0, 5):\n",
- " print(i * 2) # Here we are manipulating the value each time!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "713884f6",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "for i in range(0, 10, 2): # Here we define a step of 2!\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "b3078796",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "for i in range(10, 0, -1): # Counting down from 10, in steps of 1\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "99b52e73",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "While Loop \n",
- "A `while` loop will continue until the defined condition has been met, which can potentially not happen and cause an infinite loop.\n",
- "\n",
- "Here we are printing `n`, then incrementing it by 1, while `n` is LESS THAN 10."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "136268d9",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
+ " print(\"Current animal is:\", animal)\n",
+ "#while\n",
"n = 0\n",
"\n",
"while n < 10:\n",
@@ -1745,30 +809,13 @@
},
{
"cell_type": "markdown",
- "id": "89cd06cc",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "editable": false
- },
+ "id": "ad3766a1",
"source": [
- "_NOTE_: The `break` keyword can be used to stop execution of a loop. \n",
- "_NOTE_: Use `ctrl+C` to break execution in your terminal if needed."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b6c7b631",
+ "## Functions"
+ ],
"metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
"editable": false
- },
- "source": [
- "## Functions"
- ]
+ }
},
{
"cell_type": "markdown",
@@ -1785,50 +832,7 @@
"\n",
"We define a function using the `def` keyword, then parenthesis, which may contain the function `parameters` or `arguments` (different terms, same thing).\n",
"\n",
- "_NOTE_: You should give your functions and arguments explanative names."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b83eb9fd",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Parameters/Arguments** \n",
- "A function can receive 0 or more variables as arguments which are 'passed' through and are used during the execution of the function as required."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "25dddacf",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "**Returns** \n",
- "A function may return a single variable, or variables, it may also return nothing where it is required to just execute some logic."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "aa2983d4",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
+ "_NOTE_: You should give your functions and arguments explanative names.\n",
"Here is a very basic example of a function:"
]
},
@@ -1852,71 +856,6 @@
"print(calculated_value)"
]
},
- {
- "cell_type": "markdown",
- "id": "429c78c2",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "Note that we are able to save the result of `sum_numbers`, to a variable which we have aptly named `calculated_value`, which can be reused later."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "f64d31aa",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
- },
- "source": [
- "---\n",
- "Here we are modifying setting a default value for our `name` argument:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "643f4038",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "def generate_greeting(name='Guest'):\n",
- " \"\"\"\n",
- " Generate the default greeting message.\n",
- " Name will default to 'guest'.\n",
- " \"\"\"\n",
- " print(f\"Hello, {name}!\")\n",
- "\n",
- "generate_greeting()\n",
- "generate_greeting(\"User\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b07b62ef",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "editable": false
- },
- "source": [
- "Without specifying the `name` argument, the `generate_greeting` will use the default value specified within the function definition."
- ]
- },
{
"cell_type": "markdown",
"id": "7e95592e",
diff --git a/Intermediate_full.ipynb b/Intermediate_full.ipynb
index a7ea66c..23d096c 100644
--- a/Intermediate_full.ipynb
+++ b/Intermediate_full.ipynb
@@ -218,7 +218,7 @@
}
},
"source": [
- "This section is a brief recap of materials covered in the Beginner Python course:\n",
+ "This section is a (very) brief recap of materials covered in the Beginner Python course:\n",
"\n",
"* Data Types, variables, operators\n",
"* Comments\n",
@@ -226,7 +226,8 @@
"* Reading and Writing Files\n",
"* Lists\n",
"* Repetitions and Conditions\n",
- "* Functions\n"
+ "* Functions\n",
+ "Please review the previous course if this seems unfamiliar. [Link to previous course](https://github.com/DurhamARC-Training/BasicProgrammingPython)"
]
},
{
@@ -279,131 +280,144 @@
},
"outputs": [],
"source": [
- "enrolled_students = 728"
+ "enrolled_students = 728\n",
+ "welcome_message = \"Welcome!\"\n",
+ "---\n",
+ "**NOTE:** Values can be overwritten!\n",
+ "\n",
+ "If we define a new `welcome_message`, it changes.\n",
+ "print(welcome_message)"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "1489fa3d",
+ "cell_type": "markdown",
+ "id": "2a63c2dd",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
"tags": []
},
- "outputs": [],
"source": [
- "work_hours = 7.5"
+ "## Data types"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "fb6110d3",
+ "cell_type": "markdown",
+ "id": "7ab44bf2",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "is_loaded = False"
- ]
+ "Among the basic ones are `str` strings, `int` integers, `float` floating-point numbers and `bool` booleans.\n",
+ "str - characters, text\n",
+ "msg = \"Welcome to ARC training\"\n",
+ "#int - integer (whole numbers)\n",
+ "attendee_total=123\n",
+ "#float - decimal numbers\n",
+ "pi = 3.14159\n",
+ "#bool - True or False\n",
+ "event = True\n"
+ ]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "ac31e498",
+ "cell_type": "markdown",
+ "id": "b68a5bbf",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
}
},
- "outputs": [],
"source": [
- "welcome_message = \"Welcome!\""
+ "**Operators** \n",
+ "\n",
+ "- Numerical data: `+`, `-`, `*`, `/`, `%`, `**`, built-in functions `abs`, ...\n",
+ "- Strings: `+`, `\"apple,banana,orange\".split(\",\")`\n",
+ "\n"
]
},
{
"cell_type": "markdown",
- "id": "e1bd297e",
+ "id": "d20cb9b3",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": []
},
"source": [
- "---\n",
- "**NOTE:** Values can be overwritten!\n",
- "\n",
- "If we define a new `welcome_message`, it changes."
+ "## Comments\n",
+ "Use # to add single line comments.\n",
+ "or use ''' or \"\"\" to start and end multiline comments\n.",
+ "to describe what your code is doing."
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "94b6e716",
+ "cell_type": "markdown",
+ "id": "8ee98ee9",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
"tags": []
},
- "outputs": [],
"source": [
- "welcome_message = \"Welcome, User\""
+ "## User Input\n",
+ "Use `variable = input()` to recieve user input from the terminal"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "d11dd969",
+ "cell_type": "markdown",
+ "id": "52819c95",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
"tags": []
},
- "outputs": [],
"source": [
- "print(welcome_message)"
+ "## Reading and Writing Files"
]
},
{
"cell_type": "markdown",
- "id": "2a63c2dd",
+ "id": "a0966820",
"metadata": {
"slideshow": {
- "slide_type": "slide"
+ "slide_type": ""
},
"tags": []
},
"source": [
- "## Data types"
+ "**Opening a File**\n",
+ "\n",
+ "[Python Documentation (mode)](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)"
]
},
{
- "cell_type": "markdown",
- "id": "7ab44bf2",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "38966db9",
"metadata": {
"slideshow": {
"slide_type": ""
},
"tags": []
},
+ "outputs": [],
"source": [
- "As with any programming language, Python can deal with many different data types. Among the basic ones are `str` strings, `int` integers, `float` floating-point numbers and `bool` booleans.\n",
- "\n",
- "* an example of a _numeric_ value, _integer_ value;\n",
- "* an integer _expression_, a basic building block of a Python _statement_;\n",
- "* normal arithmetic addition"
+ "# Using a with statement\n",
+ "with open('./data/data_file.txt', 'r') as text_file:# use r(ead), (over)w(rite), a(ppend) or r+, w+ (read+write)\n",
+ " content = text_file.read()\n",
+ " print(content)"
]
},
{
"cell_type": "markdown",
- "id": "c33c20f5",
+ "id": "aadba414",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -411,154 +425,136 @@
"tags": []
},
"source": [
- "#### Numerical"
+ "**Reading from a File**\n",
+ "\n",
+ "Here are some file functions for reading from a file:\n",
+ "\n",
+ "* file.read() - Read the entire file content as a newline (\\n) separated string.\n",
+ "* file.readlines() - Read all lines into a List object. Each element in the list will contain one line including its newline character (\\n)."
]
},
{
"cell_type": "markdown",
- "id": "7a797eab",
+ "id": "1f1185e6",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": []
},
"source": [
- "##### Floating-point representation:\n",
- "* A built-in `float` is typically a double-precision (64-bit) floating-point number, following the IEEE 754 standard\n",
- "\n",
- "| Title | Storage | Smallest Magnitude | Largest Magnitude | Minimum Precision |\n",
- "|-------|---------|--------------------|-------------------|-------------------|\n",
- "| float | 64 bits | 2.22507 × 10e−308 | 1.79769 × 10+308 | ~15-17 digits |"
+ "**Writing to a File**"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "c13d9a15",
+ "cell_type": "markdown",
+ "id": "179f132a",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "fragment"
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "pi = 3.14159\n",
- "print(\"Pi =\", pi)"
+ "One way we can write to files is using the `write()` function inside a with block."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "26b8760c",
+ "id": "911d44b3",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "type(pi) # float"
+ "with open(\"./data/testfile.txt\", \"w\") as text_file:\n",
+ " text_file.write(\"Some words \\n\")\n",
+ " text_file.write(str(25))"
]
},
{
"cell_type": "markdown",
- "id": "b9324c88",
+ "id": "b61a8691",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
- }
+ "slide_type": "slide"
+ },
+ "tags": []
},
"source": [
- "_NOTE_: Scientific notation is supported!"
+ "## Lists"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "3253e3d0",
+ "cell_type": "markdown",
+ "id": "9c82edee",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "avogadros_number = 6.022e23\n",
- "c = 2.998e8\n",
- "print(\"Avogadro's number =\", avogadros_number)\n",
- "print(\"Speed of light =\", c)"
+ "Python can work not only with basic data types mentioned before, but also with compound ones. Among the most commonly used is _lists_ to contain other data types.\n",
+ "\n",
+ "* [List](https://docs.python.org/3/tutorial/datastructures.html#Lists)\n",
+ "\n",
+ "We'll learn about other data structures later today."
]
},
{
"cell_type": "markdown",
- "id": "397f9f86",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "##### Integer representation:\n",
- "* No Fixed Bounds. You can store extremely large positive or negative integers, constrained only by the available memory\n",
- "* No explicit smallest or largest representable `int`"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "3342a922",
+ "id": "8bb91a15",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "n = 3\n",
- "print(n)"
+ "An ordered list of items, accessed by a numerical index (starting at `0`). Elements within a list can be removed, modified, or accessed by their index, and a list can have values added to it.\n",
+ "\n",
+ "Defined using square brackets `[]`."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "36e671e1",
+ "id": "fdc8600e",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "type(n) # integer"
+ "letter_list = [\"a\", \"b\", \"c\", \"d\", \"e\"] A list of odd letters\n",
+ "print(letter_list)"
]
},
{
"cell_type": "markdown",
- "id": "b68a5bbf",
+ "id": "efc747a1",
"metadata": {
"slideshow": {
"slide_type": "slide"
- }
+ },
+ "tags": []
},
"source": [
- "**Numerical Operators** \n",
- "\n",
- "- Numerical data: `+`, `-`, `*`, `/`, `%`, `**`, built-in functions `abs`, ...\n",
- "- Order of execution:\n",
- " 1. `()`\n",
- " 2. `**`\n",
- " 3. `*`, `/`\n",
- " 4. `+`, `-`\n",
- " 5. Left-to-right (except exponentiation!)\n",
- "\n",
- "So, use parenthesis to make sure!"
+ "**List Access** \n",
+ "You can access a list using its index value, starting from `0`."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "af62e441",
+ "id": "2b2c4dee",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -566,55 +562,56 @@
},
"outputs": [],
"source": [
- "print(\"Addition:\", 1 + 2)"
+ "letter_list[3] # access the 4th element in the list (starts counting at 0)"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "7e7ef933",
+ "cell_type": "markdown",
+ "id": "b8369a1a",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "print(\"Subtraction:\", 1 - 2)"
+ "**Slicing** \n",
+ "You can also use slicing for specific partial list access."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "28810d42",
+ "id": "e0fac9bf",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "print(\"Multiplication:\", 5 * 10)"
+ "letter_list[1:4:2] # Slicing from index 1 (inclusive), to 4 (exclusive) with a step of 2"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "8b66a0a4",
+ "cell_type": "markdown",
+ "id": "fe6ffaf2",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "print(\"Division:\", 10 / 5)"
+ "**Empty List** \n",
+ "You can create empty lists:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "6984dbe1",
+ "id": "0edf3fa6",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -622,26 +619,26 @@
},
"outputs": [],
"source": [
- "print(\"Modulus:\", 10 % 3)"
+ "new_list = [] # An empty list\n",
+ "print(new_list)"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "74e37711",
+ "cell_type": "markdown",
+ "id": "f87cfa0f",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "print(\"Exponentiation:\", 2 ** 3)"
+ "## Repetitions and Conditions\n"
]
},
{
"cell_type": "markdown",
- "id": "a5e3ab30",
+ "id": "eae67d96",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -649,42 +646,27 @@
"tags": []
},
"source": [
- "#### String"
+ "**Logical Operators** \n",
+ "We can determine conditions based on Boolean logic: using `and`, `or`, `not` to *evaluate* boolean expressions:"
]
},
{
"cell_type": "markdown",
- "id": "ba570bf4",
+ "id": "b38b2739",
"metadata": {
"slideshow": {
"slide_type": "fragment"
- }
- },
- "source": [
- "A string is a series of characters enclosed in quotes, either single or double, used to represent text."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "ea3df5a1",
- "metadata": {
- "slideshow": {
- "slide_type": ""
},
"tags": []
},
- "outputs": [],
"source": [
- "s = 'Hello, World!'\n",
- "s = \"Hello, World!\"\n",
- "print(s)"
+ "**AND:**"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "e3c09d2d",
+ "id": "db33e773",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -693,28 +675,31 @@
},
"outputs": [],
"source": [
- "type(s) # string"
+ "a = True\n",
+ "b = False\n",
+ "\n",
+ "a and b\n",
+ "a or b\n",
+ "not a\n"
]
},
{
"cell_type": "markdown",
- "id": "50a4347d",
+ "id": "9303b3ff",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
+ "slide_type": "slide"
+ }
},
"source": [
- "**String Concatenation** \n",
- "\n",
- "We can combine strings together."
+ "**Conditions** \n",
+ "In order to control program flow, and whether or not code is executed, we can do conditions based on variable values."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "d9fadfdc",
+ "id": "a0af6ea1",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -722,14 +707,17 @@
},
"outputs": [],
"source": [
- "# String concatenation\n",
- "hello_world = \"Hello,\" + \" World!\"\n",
- "print(hello_world)"
+ "x = 5\n",
+ "y = 6\n",
+
+ "\n",
+ "if ((x > 2) and (y < 12)):\n",
+ " print(\"x =\", x, \"is GREATER THAN 2 AND y =\",y ,\"is LESS THAN 12\")\n"
]
},
{
"cell_type": "markdown",
- "id": "d20cb9b3",
+ "id": "3246bbe1",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -737,26 +725,31 @@
"tags": []
},
"source": [
- "## Comments"
+ "**Else/Elif** \n",
+ "\n",
+ "We may also specify either/or with `if/else`, and even add extra conditions with `elif`."
]
},
{
"cell_type": "markdown",
- "id": "7ac031a7",
+ "id": "8c87d3f8",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
"tags": []
},
"source": [
- "Used to write notes or comments about code, as well as description of what the code is doing, or the variables used."
+ "**Repetitions** \n",
+ "There are a number of ways for us to repeat lines or blocks of code within our program, to do this we use `loops`:\n",
+ "for loops - execute for each item specified in the definition\n",
+ "while loop - execute while condition is met (checked at start of each cycle)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "41b7cdfe",
+ "id": "c7dc2e03",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -765,951 +758,29 @@
},
"outputs": [],
"source": [
- "# A single-line comment!\n",
- "welcome_message = \"Welcome, User!\"\n",
- "print(welcome_message) # Print welcome message"
+ "#for\n",
+ "animal_list = [\"Cat\", \"Dog\", \"Bird\"]\n",
+ "\n",
+ "for animal in animal_list:\n",
+ " print(\"Current animal is:\", animal)\n",
+ "#while\n",
+ "n = 0\n",
+ "\n",
+ "while n < 10:\n",
+ " print(n)\n",
+ " n += 1"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ad3766a1",
+ "source": [
+ "## Functions"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "9c50f102",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "'''\n",
- "A multi-line comment!\n",
- "'''\n",
- "print(\"something\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "3f72dedf",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "\"\"\"\n",
- "Another multi-line comment!\n",
- "\"\"\"\n",
- "print(\"something else\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8ee98ee9",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "## User Input"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6b2e3c6e",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "source": [
- "You can use `input()` to read user input: "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "a1178a62",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "inputted_variable = float(input())\n",
- "print(inputted_variable) # Print what we just input!\n",
- "type(inputted_variable)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "52819c95",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "## Reading and Writing Files"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "a0966820",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "source": [
- "**Opening a File**\n",
- "\n",
- "Two things to note here:\n",
- " - My object `my_file` is different from my file `\"testfile.txt\"`!\n",
- " - There are different modes:\n",
- " - read: `'r'`\n",
- " - (over-)write: `'w'`\n",
- " - append: `'a'`\n",
- " - read+write: `'w+'` or `'r+'`\n",
- "\n",
- "[Python Documentation (mode)](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "38966db9",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "# Using a with statement\n",
- "with open('./data/data_file.txt', 'r') as text_file:\n",
- " content = text_file.read()\n",
- " print(content)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "aadba414",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**Reading from a File**\n",
- "\n",
- "Here are some file functions for reading from a file:\n",
- "\n",
- "* file.read() - Read the entire file content as a newline (\\n) separated string.\n",
- "* file.readlines() - Read all lines into a List object. Each element in the list will contain one line including its newline character (\\n)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "7f0a6056",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "with open(\"./data/data_file.txt\") as text_file:\n",
- " print(text_file.read())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "ec567746",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "with open(\"./data/data_file.txt\") as text_file:\n",
- " print(text_file.readlines())"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "1f1185e6",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**Writing to a File**"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "179f132a",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "One way we can write to files is using the `write()` function."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "e998f8fe",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "Using the `with` keyword:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "911d44b3",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "with open(\"./data/testfile.txt\", \"w\") as text_file:\n",
- " text_file.write(\"Some words \\n\")\n",
- " text_file.write(str(25))"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b61a8691",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "## Lists"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "9c82edee",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "source": [
- "Python can work not only with basic data types mentioned before, but also with compound ones. Compound data types in Python are a powerful tool for organizing and storing data. Among the most commonly used is _lists_ which we learned about in the beginner's course.\n",
- "\n",
- "* [List](https://docs.python.org/3/tutorial/datastructures.html#Lists)\n",
- "\n",
- "We'll learn about other data structures later today."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8bb91a15",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "source": [
- "An ordered list of items, accessed by a numerical index (starting at `0`). Elements within a list can be removed, modified, or accessed by their index, and a list can have values added to it.\n",
- "\n",
- "Defined using square brackets `[]`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "fdc8600e",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "odd_list = [1, 3, 5, 7, 9] # A list of odd numbers\n",
- "print(odd_list)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "efc747a1",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**List Access** \n",
- "You can access a list using its index value, starting from `0`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "2b2c4dee",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "3f87d56b",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "b686d58d",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "odd_list[-1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1f916c68",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[-2]"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b8369a1a",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**Slicing** \n",
- "You can also use slicing for specific partial list access."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "e0fac9bf",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "odd_list[1:4] # Slicing from index 1 (inclusive), to 4 (exclusive)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "586edf14",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[3:] # Slicing from index 3 to end."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "9f0dea38",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[:3] # Slicing from beginning to index 3"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "bab8e8a0",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "odd_list[::2] # Slicing with a step of 2"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "fe6ffaf2",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**Empty List** \n",
- "You can create empty lists:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "0edf3fa6",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "new_list = [] # An empty list\n",
- "print(new_list)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "f87cfa0f",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "## Repetitions and Conditions\n",
- "\n",
- "First, let's recap boolean logic. Boolean logic uses just two values: True, and False."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "9ca15865",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "#### Boolean"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "d7b7abb8",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "b1 = False\n",
- "b2 = True\n",
- "print(b1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "9296bb88",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "type(True) # boolean"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "eae67d96",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**Logical Operators** \n",
- "We can determine conditions based on Boolean logic: using `and`, `or`, `not` to *evaluate* boolean expressions:"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b38b2739",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "**AND:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "db33e773",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "a = True\n",
- "b = False\n",
- "\n",
- "a and b"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "5c8055b9-5863-486e-a741-de11eee3faa9",
- "metadata": {},
- "source": [
- "We can display the results of the `AND` operation using a \"truth table\":\n",
- "| *and* | True | False |\n",
- "| --------- | ----- | ----- |\n",
- "| __True__ | True | False |\n",
- "| __False__ | False | False |"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8b00b658",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "---\n",
- "**OR:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "725d0173",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "a = True\n",
- "b = False\n",
- "\n",
- "a or b"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "4a4f35dc-5107-4589-984a-75f7a929958c",
- "metadata": {},
- "source": [
- "| *or* | True | False |\n",
- "| --------- | ----- | ----- |\n",
- "| __True__ | True | True |\n",
- "| __False__ | True | False |\n",
- "\n"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "6a10fe2f",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- }
- },
- "source": [
- "---\n",
- "**NOT:**"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "27378611",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "a = True\n",
- "\n",
- "not a"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "bf19d081-ce51-4b07-bc67-94c4569b9ca9",
- "metadata": {},
- "source": [
- "| *not* | True | False |\n",
- "| ----- | ----- | ----- |\n",
- "| | False | True |"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "9303b3ff",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "**Conditions** \n",
- "In order to control program flow, and whether or not code is executed, we can do conditions based on variable values."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "7fd769b8-28d3-416a-823f-21f7ffcceef7",
- "metadata": {},
- "outputs": [],
- "source": [
- "if a:\n",
- " print(\"a is True!\")\n",
- "else:\n",
- " print(\"a is False.\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "a0af6ea1",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "x = 5\n",
- "\n",
- "if x > 2:\n",
- " print(\"x =\", x, \"is GREATER THAN 2\")\n",
- "\n",
- "if x == 3:\n",
- " print(\"x =\", x, \"is EQUAL TO 3\")\n",
- "\n",
- "if x >= 3:\n",
- " print(\"x =\", x, \"is GREATER THAN or EQUAL TO 3\")\n",
- "\n",
- "if x != 0:\n",
- " print(\"x =\", x, \"IS NOT 0\")\n",
- "\n",
- "if x < 4:\n",
- " print(\"x =\", x, \"is LESS THAN 4\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "3246bbe1",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**Else/Elif** \n",
- "\n",
- "We may also specify either/or with `if/else`, and even add extra conditions with `elif`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "8cab77e6",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "x = 10\n",
- "\n",
- "if x > 0:\n",
- " print(\"x is positive\")\n",
- "elif x < 0:\n",
- " print(\"x is negative\")\n",
- "else:\n",
- " print(\"x is zero\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "8c87d3f8",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "**Repetitions** \n",
- "There are a number of ways for us to repeat lines or blocks of code within our program, to do this we use `loops`:"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "095edef4",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "For Each Loop: \n",
- "A for each loop will execute its code for each item specified within the loop definition"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "c7dc2e03",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "animal_list = [\"Cat\", \"Dog\", \"Bird\"]\n",
- "\n",
- "for animal in animal_list:\n",
- " print(\"Current animal is:\", animal)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "76e99a09",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- }
- },
- "source": [
- "The above describes `for each animal in animal_list`."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "e5b05a2c",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "---\n",
- "We are able to use a range-based for loop:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "b14f546d",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "for i in range(0, 5):\n",
- " print(i * 2) # Here we are manipulating the value each time!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "713884f6",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "for i in range(0, 10, 2): # Here we define a step of 2!\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "b3078796",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "for i in range(10, 0, -1): # Counting down from 10, in steps of 1\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "99b52e73",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- },
- "tags": []
- },
- "source": [
- "While Loop \n",
- "A `while` loop will continue until the defined condition has been met, which can potentially not happen and cause an infinite loop.\n",
- "\n",
- "Here we are printing `n`, then incrementing it by 1, while `n` is LESS THAN 10."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "136268d9",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "n = 0\n",
- "\n",
- "while n < 10:\n",
- " print(n)\n",
- " n += 1"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "89cd06cc",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- }
- },
- "source": [
- "_NOTE_: The `break` keyword can be used to stop execution of a loop. \n",
- "_NOTE_: Use `ctrl+C` to break execution in your terminal if needed."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b6c7b631",
- "metadata": {
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "## Functions"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "ad3766a1",
+ "cell_type": "markdown",
+ "id": "ad3766a1",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1721,47 +792,7 @@
"\n",
"We define a function using the `def` keyword, then parenthesis, which may contain the function `parameters` or `arguments` (different terms, same thing).\n",
"\n",
- "_NOTE_: You should give your functions and arguments explanative names."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b83eb9fd",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "**Parameters/Arguments** \n",
- "A function can receive 0 or more variables as arguments which are 'passed' through and are used during the execution of the function as required."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "25dddacf",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "**Returns** \n",
- "A function may return a single variable, or variables, it may also return nothing where it is required to just execute some logic."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "aa2983d4",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
+ "_NOTE_: You should give your functions and arguments explanative names.\n",
"Here is a very basic example of a function:"
]
},
@@ -1785,68 +816,6 @@
"print(calculated_value)"
]
},
- {
- "cell_type": "markdown",
- "id": "429c78c2",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "Note that we are able to save the result of `sum_numbers`, to a variable which we have aptly named `calculated_value`, which can be reused later."
- ]
- },
- {
- "cell_type": "markdown",
- "id": "f64d31aa",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "---\n",
- "Here we are modifying setting a default value for our `name` argument:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "643f4038",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "def generate_greeting(name='Guest'):\n",
- " \"\"\"\n",
- " Generate the default greeting message.\n",
- " Name will default to 'guest'.\n",
- " \"\"\"\n",
- " print(f\"Hello, {name}!\")\n",
- "\n",
- "generate_greeting()\n",
- "generate_greeting(\"User\")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "b07b62ef",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- }
- },
- "source": [
- "Without specifying the `name` argument, the `generate_greeting` will use the default value specified within the function definition."
- ]
- },
{
"cell_type": "markdown",
"id": "7e95592e",
From 961222fc7969c1c1215af2b1dfa3468611eb0df2 Mon Sep 17 00:00:00 2001
From: A Naden <46694302+a-naden@users.noreply.github.com>
Date: Wed, 20 May 2026 14:40:35 +0100
Subject: [PATCH 2/2] Fixes comments left as part of PR #72.
---
Intermediate.ipynb | 106 ++++++++++++++++++++++++++++------------
Intermediate_full.ipynb | 90 +++++++++++++++++++++++++---------
2 files changed, 141 insertions(+), 55 deletions(-)
diff --git a/Intermediate.ipynb b/Intermediate.ipynb
index f5c284d..f570cae 100644
--- a/Intermediate.ipynb
+++ b/Intermediate.ipynb
@@ -297,10 +297,38 @@
"source": [
"enrolled_students = 728\n",
"welcome_message = \"Welcome!\"\n",
- "---\n",
+ "---\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e1bd297e",
+ "metadata": {
+ "slideshow": {
+ "slide_type": "fragment"
+ },
+ "tags": [],
+ "editable": false
+ },
+ "source": [
"**NOTE:** Values can be overwritten!\n",
"\n",
- "If we define a new `welcome_message`, it changes.\n",
+ "If we define a new `welcome_message`, it overwrites the first.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ea0415db",
+ "metadata": {
+ "slideshow": {
+ "slide_type": ""
+ },
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "welcome_message = \"A different welcome\"\n",
"print(welcome_message)"
]
},
@@ -370,8 +398,22 @@
"source": [
"## Comments\n",
"Use # to add single line comments.\n",
- "or use ''' or \"\"\" to start and end multiline comments\n.",
- "to describe what your code is doing."
+ "or use ''' or \"\"\" to start and end multiline comments\n",
+ ".to describe what your code is doing."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "10c24749",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print(\"This is executed\") # this comment isn't executed\n",
+ "'''\n",
+ "neither is this\n",
+ "'''\n",
+ "print(\"This is executed again\")"
]
},
{
@@ -810,16 +852,20 @@
{
"cell_type": "markdown",
"id": "ad3766a1",
- "source": [
- "## Functions"
- ],
"metadata": {
+ "slideshow": {
+ "slide_type": "slide"
+ },
+ "tags": [],
"editable": false
- }
+ },
+ "source": [
+ "## Functions"
+ ]
},
{
"cell_type": "markdown",
- "id": "ad3766a1",
+ "id": "354a04f1",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -2899,11 +2945,11 @@
"cell_type": "markdown",
"id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": ""
},
- "tags": []
+ "tags": [],
+ "editable": false
},
"source": [
"Modules let us:\n",
@@ -3032,11 +3078,11 @@
"cell_type": "markdown",
"id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": "slide"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
"source": [
"## Why Namespaces Matter\n",
@@ -3049,7 +3095,6 @@
"execution_count": null,
"id": "4de72e4c-2795-4b4d-8542-10729a74f44a",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -3068,11 +3113,11 @@
"cell_type": "markdown",
"id": "36f46ead-b9a1-481f-8798-edbfc58b5504",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": ""
},
- "tags": []
+ "tags": [],
+ "editable": false
},
"source": [
"Without namespaces, Python, and your reader, couldn't tell them apart."
@@ -3082,11 +3127,11 @@
"cell_type": "markdown",
"id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": "fragment"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
"source": [
"----\n",
@@ -3186,11 +3231,11 @@
"cell_type": "markdown",
"id": "e34aedb6",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": "slide"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
"source": [
"## String Methods - Working with String Objects"
@@ -3200,10 +3245,10 @@
"cell_type": "markdown",
"id": "ffef8f84",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": ""
- }
+ },
+ "editable": false
},
"source": [
"In Python, strings are objects, and objects have **methods** - functions that belong to and operate on that object.\n",
@@ -3224,7 +3269,6 @@
"execution_count": null,
"id": "d5bd4280",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -3274,10 +3318,10 @@
"cell_type": "markdown",
"id": "a4fa9b5b",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": ""
- }
+ },
+ "editable": false
},
"source": [
"These methods help you locate substrings within a string, returning their position or checking for their presence:\n",
@@ -3431,10 +3475,10 @@
"cell_type": "markdown",
"id": "80b12b8b",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": ""
- }
+ },
+ "editable": false
},
"source": [
"These methods create modified versions of strings by replacing content, changing case, or removing unwanted characters. _**Remember**: strings are immutable, so these methods always return a new string._\n",
@@ -3517,10 +3561,10 @@
"cell_type": "markdown",
"id": "71a4546c",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": ""
- }
+ },
+ "editable": false
},
"source": [
"These methods allow you to break strings apart into lists of substrings, or combine lists of strings into a single string. They're particularly useful for parsing text data or formatting output:\n",
@@ -4834,7 +4878,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3 (ipykernel)",
+ "display_name": "Python 3",
"language": "python",
"name": "python3"
},
@@ -4848,7 +4892,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.13.0"
+ "version": "3.6.8"
}
},
"nbformat": 4,
diff --git a/Intermediate_full.ipynb b/Intermediate_full.ipynb
index 23d096c..1ef7dae 100644
--- a/Intermediate_full.ipynb
+++ b/Intermediate_full.ipynb
@@ -282,10 +282,37 @@
"source": [
"enrolled_students = 728\n",
"welcome_message = \"Welcome!\"\n",
- "---\n",
+ "---\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e1bd297e",
+ "metadata": {
+ "slideshow": {
+ "slide_type": "fragment"
+ },
+ "tags": []
+ },
+ "source": [
"**NOTE:** Values can be overwritten!\n",
"\n",
- "If we define a new `welcome_message`, it changes.\n",
+ "If we define a new `welcome_message`, it overwrites the first.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ea0415db",
+ "metadata": {
+ "slideshow": {
+ "slide_type": ""
+ },
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "welcome_message = \"A different welcome\"\n",
"print(welcome_message)"
]
},
@@ -321,7 +348,7 @@
"pi = 3.14159\n",
"#bool - True or False\n",
"event = True\n"
- ]
+ ]
},
{
"cell_type": "markdown",
@@ -351,8 +378,31 @@
"source": [
"## Comments\n",
"Use # to add single line comments.\n",
- "or use ''' or \"\"\" to start and end multiline comments\n.",
- "to describe what your code is doing."
+ "or use ''' or \"\"\" to start and end multiline comments\n",
+ ".to describe what your code is doing."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "10c24749",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "This is executed\n",
+ "but this is\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(\"This is executed\") # this comment isn't executed\n",
+ "'''\n",
+ "neither is this\n",
+ "'''\n",
+ "print(\"This is executed again\")"
]
},
{
@@ -709,7 +759,6 @@
"source": [
"x = 5\n",
"y = 6\n",
-
"\n",
"if ((x > 2) and (y < 12)):\n",
" print(\"x =\", x, \"is GREATER THAN 2 AND y =\",y ,\"is LESS THAN 12\")\n"
@@ -771,16 +820,22 @@
" n += 1"
]
},
- {
- "cell_type": "markdown",
+ {
+ "cell_type": "markdown",
"id": "ad3766a1",
+ "metadata": {
+ "slideshow": {
+ "slide_type": "slide"
+ },
+ "tags": []
+ },
"source": [
"## Functions"
]
},
{
"cell_type": "markdown",
- "id": "ad3766a1",
+ "id": "354a04f1",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -3046,7 +3101,6 @@
"cell_type": "markdown",
"id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -3177,7 +3231,6 @@
"cell_type": "markdown",
"id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "slide"
},
@@ -3194,7 +3247,6 @@
"execution_count": null,
"id": "4de72e4c-2795-4b4d-8542-10729a74f44a",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -3213,7 +3265,6 @@
"cell_type": "markdown",
"id": "36f46ead-b9a1-481f-8798-edbfc58b5504",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -3227,7 +3278,6 @@
"cell_type": "markdown",
"id": "ccbef34a-9226-4735-859d-378640ee9cc7",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "notes"
},
@@ -3242,7 +3292,6 @@
"cell_type": "markdown",
"id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "fragment"
},
@@ -3343,7 +3392,6 @@
"cell_type": "markdown",
"id": "e34aedb6",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "slide"
},
@@ -3357,7 +3405,6 @@
"cell_type": "markdown",
"id": "ffef8f84",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -3381,7 +3428,6 @@
"execution_count": null,
"id": "d5bd4280",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -3429,7 +3475,6 @@
"cell_type": "markdown",
"id": "a4fa9b5b",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -3518,7 +3563,6 @@
"cell_type": "markdown",
"id": "6ac1c80f",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "notes"
}
@@ -3611,7 +3655,6 @@
"cell_type": "markdown",
"id": "80b12b8b",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -3696,7 +3739,6 @@
"cell_type": "markdown",
"id": "71a4546c",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -5210,7 +5252,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3 (ipykernel)",
+ "display_name": "Python 3",
"language": "python",
"name": "python3"
},
@@ -5224,7 +5266,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.13.0"
+ "version": "3.6.8"
}
},
"nbformat": 4,