diff --git a/Intermediate.ipynb b/Intermediate.ipynb
index 2216356..323487d 100644
--- a/Intermediate.ipynb
+++ b/Intermediate.ipynb
@@ -158,8 +158,7 @@
"- How to simplify your code with _comprehensions_ and _conditional expressions_.\n",
"- How to perform advanced _string manipulation_ techniques.\n",
"- How to import and use _modules_ to extend your programs.\n",
- "- How to work with various data structures and understand the concept of _immutability_.\n",
- "- How to create and use basic _classes_ to implement object-oriented programming principles."
+ "- How to work with various data structures and understand the concept of _immutability_.\n"
]
},
{
@@ -232,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",
@@ -240,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)"
]
},
{
@@ -295,50 +295,8 @@
},
"outputs": [],
"source": [
- "enrolled_students = 728"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1489fa3d",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "work_hours = 7.5"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "fb6110d3",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "is_loaded = False"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "ac31e498",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "welcome_message = \"Welcome!\""
+ "enrolled_students = 728\n",
+ "welcome_message = \"Welcome!\"\n"
]
},
{
@@ -352,31 +310,15 @@
"editable": false
},
"source": [
- "---\n",
"**NOTE:** Values can be overwritten!\n",
"\n",
- "If we define a new `welcome_message`, it changes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "94b6e716",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "welcome_message = \"Welcome, User\""
+ "If we define a new `welcome_message`, it overwrites the first.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "d11dd969",
+ "id": "ea0415db",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -385,6 +327,7 @@
},
"outputs": [],
"source": [
+ "welcome_message = \"A different welcome\"\n",
"print(welcome_message)"
]
},
@@ -413,259 +356,150 @@
"editable": false
},
"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"
+ "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": "markdown",
- "id": "c33c20f5",
+ "id": "b68a5bbf",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
- "tags": [],
"editable": false
},
"source": [
- "#### Numerical"
+ "**Operators** \n",
+ "\n",
+ "- Numerical data: `+`, `-`, `*`, `/`, `%`, `**`, built-in functions `abs`, ...\n",
+ "- Strings: `+`, `\"apple,banana,orange\".split(\",\")`\n",
+ "\n"
]
},
{
"cell_type": "markdown",
- "id": "7a797eab",
+ "id": "d20cb9b3",
"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 |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "c13d9a15",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "pi = 3.14159\n",
- "print(\"Pi =\", pi)"
+ "## 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": "26b8760c",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
+ "id": "10c24749",
+ "metadata": {},
"outputs": [],
"source": [
- "type(pi) # float"
+ "print(\"This is executed\") # this comment isn't executed\n",
+ "'''\n",
+ "neither is this\n",
+ "'''\n",
+ "print(\"This is executed again\")"
]
},
{
"cell_type": "markdown",
- "id": "b9324c88",
+ "id": "8ee98ee9",
"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)"
+ "## User Input\n",
+ "Use `variable = input()` to recieve user input from the terminal"
]
},
{
"cell_type": "markdown",
- "id": "397f9f86",
+ "id": "52819c95",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
+ "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`"
+ "## Reading and Writing Files"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "3342a922",
+ "cell_type": "markdown",
+ "id": "a0966820",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "n = 3\n",
- "print(n)"
+ "**Opening a File**\n",
+ "\n",
+ "[Python Documentation (mode)](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "36e671e1",
+ "id": "38966db9",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "type(n) # integer"
+ "# 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": "b68a5bbf",
+ "id": "aadba414",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
+ "tags": [],
"editable": false
},
"source": [
- "**Numerical Operators** \n",
+ "**Reading from a File**\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",
+ "Here are some file functions for reading from a file:\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)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "7e7ef933",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Subtraction:\", 1 - 2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "28810d42",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Multiplication:\", 5 * 10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "8b66a0a4",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Division:\", 10 / 5)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "6984dbe1",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Modulus:\", 10 % 3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "74e37711",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Exponentiation:\", 2 ** 3)"
+ "* 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": "a5e3ab30",
+ "id": "1f1185e6",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -674,43 +508,27 @@
"editable": false
},
"source": [
- "#### String"
+ "**Writing to a File**"
]
},
{
"cell_type": "markdown",
- "id": "ba570bf4",
+ "id": "179f132a",
"metadata": {
"slideshow": {
"slide_type": "fragment"
},
+ "tags": [],
"editable": false
},
"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)"
+ "One way we can write to files is using the `write()` function inside a with block."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "e3c09d2d",
+ "id": "911d44b3",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -719,58 +537,46 @@
},
"outputs": [],
"source": [
- "type(s) # string"
+ "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": "50a4347d",
+ "id": "b61a8691",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": [],
"editable": false
},
"source": [
- "**String Concatenation** \n",
- "\n",
- "We can combine strings together."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "d9fadfdc",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "# String concatenation\n",
- "hello_world = \"Hello,\" + \" World!\"\n",
- "print(hello_world)"
+ "## Lists"
]
},
{
"cell_type": "markdown",
- "id": "d20cb9b3",
+ "id": "9c82edee",
"metadata": {
"slideshow": {
- "slide_type": "slide"
+ "slide_type": ""
},
"tags": [],
"editable": false
},
"source": [
- "## Comments"
+ "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": "7ac031a7",
+ "id": "8bb91a15",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -779,13 +585,15 @@
"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."
+ "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": "41b7cdfe",
+ "id": "fdc8600e",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -794,817 +602,13 @@
},
"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 line-by-line\n",
- "* file.readlines() - Read all lines into a List object"
- ]
- },
- {
- "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`:"
+ "letter_list = [\"a\", \"b\", \"c\", \"d\", \"e\"] # A list of odd letters\n",
+ "print(letter_list)"
]
},
{
"cell_type": "markdown",
- "id": "095edef4",
+ "id": "efc747a1",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -1613,60 +617,43 @@
"editable": false
},
"source": [
- "For Each Loop: \n",
- "A for each loop will execute its code for each item specified within the loop definition"
+ "**List Access** \n",
+ "You can access a list using its index value, starting from `0`."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "c7dc2e03",
+ "id": "2b2c4dee",
"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"
- },
- "editable": false
- },
- "source": [
- "The above describes `for each animal in animal_list`."
+ "letter_list[3] # access the 4th element in the list (starts counting at 0)"
]
},
{
"cell_type": "markdown",
- "id": "e5b05a2c",
+ "id": "b8369a1a",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": [],
"editable": false
},
"source": [
- "---\n",
- "We are able to use a range-based for loop:"
+ "**Slicing** \n",
+ "You can also use slicing for specific partial list access."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "b14f546d",
+ "id": "e0fac9bf",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1675,29 +662,28 @@
},
"outputs": [],
"source": [
- "for i in range(0, 5):\n",
- " print(i * 2) # Here we are manipulating the value each time!"
+ "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": "713884f6",
+ "cell_type": "markdown",
+ "id": "fe6ffaf2",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "for i in range(0, 10, 2): # Here we define a step of 2!\n",
- " print(i)"
+ "**Empty List** \n",
+ "You can create empty lists:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "b3078796",
+ "id": "0edf3fa6",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1705,13 +691,13 @@
},
"outputs": [],
"source": [
- "for i in range(10, 0, -1): # Counting down from 10, in steps of 1\n",
- " print(i)"
+ "new_list = [] # An empty list\n",
+ "print(new_list)"
]
},
{
"cell_type": "markdown",
- "id": "99b52e73",
+ "id": "f87cfa0f",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -1720,123 +706,127 @@
"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."
+ "## Repetitions and Conditions\n"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "136268d9",
+ "cell_type": "markdown",
+ "id": "eae67d96",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": [],
+ "editable": false
},
- "outputs": [],
"source": [
- "n = 0\n",
- "\n",
- "while n < 10:\n",
- " print(n)\n",
- " n += 1"
+ "**Logical Operators** \n",
+ "We can determine conditions based on Boolean logic: using `and`, `or`, `not` to *evaluate* boolean expressions:"
]
},
{
"cell_type": "markdown",
- "id": "89cd06cc",
+ "id": "b38b2739",
"metadata": {
"slideshow": {
"slide_type": "fragment"
},
+ "tags": [],
"editable": false
},
"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."
+ "**AND:**"
]
},
{
- "cell_type": "markdown",
- "id": "b6c7b631",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "db33e773",
"metadata": {
"slideshow": {
- "slide_type": "slide"
+ "slide_type": ""
},
- "editable": false
+ "tags": []
},
+ "outputs": [],
"source": [
- "## Functions"
+ "a = True\n",
+ "b = False\n",
+ "\n",
+ "a and b\n",
+ "a or b\n",
+ "not a\n"
]
},
{
"cell_type": "markdown",
- "id": "ad3766a1",
+ "id": "9303b3ff",
"metadata": {
"slideshow": {
- "slide_type": ""
+ "slide_type": "slide"
},
- "tags": [],
"editable": false
},
"source": [
- "A function is a reusable block of code used to perform a specific task.\n",
- "\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."
+ "**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": "markdown",
- "id": "b83eb9fd",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a0af6ea1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
- },
- "tags": [],
- "editable": false
+ "slide_type": ""
+ }
},
+ "outputs": [],
"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."
+ "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": "25dddacf",
+ "id": "3246bbe1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"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."
+ "**Else/Elif** \n",
+ "\n",
+ "We may also specify either/or with `if/else`, and even add extra conditions with `elif`."
]
},
{
"cell_type": "markdown",
- "id": "aa2983d4",
+ "id": "8c87d3f8",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": [],
"editable": false
},
"source": [
- "Here is a very basic example of a function:"
+ "**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": "d4ca99fa",
+ "id": "c7dc2e03",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1845,47 +835,56 @@
},
"outputs": [],
"source": [
- "def sum_numbers(val_one, val_two):\n",
- " \"\"\"Sums and returns two numbers\"\"\"\n",
- " return val_one + val_two # Sum of both numbers\n",
+ "#for\n",
+ "animal_list = [\"Cat\", \"Dog\", \"Bird\"]\n",
"\n",
- "calculated_value = sum_numbers(1, 1)\n",
- "print(calculated_value)"
+ "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": "429c78c2",
+ "id": "ad3766a1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"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."
+ "## Functions"
]
},
{
"cell_type": "markdown",
- "id": "f64d31aa",
+ "id": "354a04f1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": ""
},
"tags": [],
"editable": false
},
"source": [
- "---\n",
- "Here we are modifying setting a default value for our `name` argument:"
+ "A function is a reusable block of code used to perform a specific task.\n",
+ "\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.\n",
+ "Here is a very basic example of a function:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "643f4038",
+ "id": "d4ca99fa",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1894,28 +893,12 @@
},
"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",
+ "def sum_numbers(val_one, val_two):\n",
+ " \"\"\"Sums and returns two numbers\"\"\"\n",
+ " return val_one + val_two # Sum of both numbers\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."
+ "calculated_value = sum_numbers(1, 1)\n",
+ "print(calculated_value)"
]
},
{
@@ -2301,11 +1284,72 @@
},
"outputs": [],
"source": [
- "# Initialising a tuple with values\n",
- "\n",
+ "# Initialising a tuple with values\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7e873494-6a43-4402-beee-5bc2d507f33a",
+ "metadata": {
+ "remove_code": "non-comments",
+ "slideshow": {
+ "slide_type": ""
+ }
+ },
+ "outputs": [],
+ "source": [
"# Initialising a tuple with one value\n"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "812d5a14",
+ "metadata": {
+ "editable": false,
+ "slideshow": {
+ "slide_type": "fragment"
+ },
+ "tags": []
+ },
+ "source": [
+ "Tuple Unpacking"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "22e2a236-df2d-4130-9810-5bfd4f0ccb49",
+ "metadata": {
+ "editable": true,
+ "remove_code": "non-comments",
+ "slideshow": {
+ "slide_type": ""
+ },
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "# The most basic example of tuple unpacking\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d2638ed7-4344-4297-b31b-bcdd649889c7",
+ "metadata": {
+ "editable": true,
+ "remove_code": "non-comments",
+ "slideshow": {
+ "slide_type": ""
+ },
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "# You can use tuple unpacking when a function returns multiple values, or a tuple!\n"
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -2347,7 +1391,7 @@
},
{
"cell_type": "markdown",
- "id": "812d5a14",
+ "id": "9380a5f8",
"metadata": {
"slideshow": {
"slide_type": "fragment"
@@ -3961,11 +3005,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",
@@ -4094,11 +3138,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",
@@ -4111,7 +3155,6 @@
"execution_count": null,
"id": "4de72e4c-2795-4b4d-8542-10729a74f44a",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -4130,11 +3173,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."
@@ -4144,11 +3187,11 @@
"cell_type": "markdown",
"id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": "fragment"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
"source": [
"----\n",
@@ -4248,11 +3291,11 @@
"cell_type": "markdown",
"id": "e34aedb6",
"metadata": {
- "editable": false,
"slideshow": {
"slide_type": "slide"
},
- "tags": []
+ "tags": [],
+ "editable": false
},
"source": [
"## String Methods - Working with String Objects"
@@ -4262,10 +3305,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",
@@ -4286,7 +3329,6 @@
"execution_count": null,
"id": "d5bd4280",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -4336,10 +3378,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",
@@ -4493,10 +3535,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",
@@ -4579,10 +3621,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",
@@ -5896,7 +4938,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3 (ipykernel)",
+ "display_name": "Python 3",
"language": "python",
"name": "python3"
},
@@ -5910,7 +4952,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 c4653d8..a43add4 100644
--- a/Intermediate_full.ipynb
+++ b/Intermediate_full.ipynb
@@ -150,8 +150,7 @@
"- How to simplify your code with _comprehensions_ and _conditional expressions_.\n",
"- How to perform advanced _string manipulation_ techniques.\n",
"- How to import and use _modules_ to extend your programs.\n",
- "- How to work with various data structures and understand the concept of _immutability_.\n",
- "- How to create and use basic _classes_ to implement object-oriented programming principles."
+ "- How to work with various data structures and understand the concept of _immutability_.\n"
]
},
{
@@ -219,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",
@@ -227,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)"
]
},
{
@@ -280,50 +280,8 @@
},
"outputs": [],
"source": [
- "enrolled_students = 728"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1489fa3d",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "work_hours = 7.5"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "fb6110d3",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "is_loaded = False"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "ac31e498",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "welcome_message = \"Welcome!\""
+ "enrolled_students = 728\n",
+ "welcome_message = \"Welcome!\"\n"
]
},
{
@@ -336,31 +294,15 @@
"tags": []
},
"source": [
- "---\n",
"**NOTE:** Values can be overwritten!\n",
"\n",
- "If we define a new `welcome_message`, it changes."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "94b6e716",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- },
- "tags": []
- },
- "outputs": [],
- "source": [
- "welcome_message = \"Welcome, User\""
+ "If we define a new `welcome_message`, it overwrites the first.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "d11dd969",
+ "id": "ea0415db",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -369,6 +311,7 @@
},
"outputs": [],
"source": [
+ "welcome_message = \"A different welcome\"\n",
"print(welcome_message)"
]
},
@@ -395,254 +338,144 @@
"tags": []
},
"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"
+ "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": "markdown",
- "id": "c33c20f5",
+ "id": "b68a5bbf",
"metadata": {
"slideshow": {
"slide_type": "slide"
- },
- "tags": []
+ }
},
"source": [
- "#### Numerical"
+ "**Operators** \n",
+ "\n",
+ "- Numerical data: `+`, `-`, `*`, `/`, `%`, `**`, built-in functions `abs`, ...\n",
+ "- Strings: `+`, `\"apple,banana,orange\".split(\",\")`\n",
+ "\n"
]
},
{
"cell_type": "markdown",
- "id": "7a797eab",
+ "id": "d20cb9b3",
"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 |"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "c13d9a15",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "pi = 3.14159\n",
- "print(\"Pi =\", pi)"
+ "## 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": "26b8760c",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
+ "execution_count": 1,
+ "id": "10c24749",
+ "metadata": {},
"outputs": [],
"source": [
- "type(pi) # float"
+ "print(\"This is executed\") # this comment isn't executed\n",
+ "'''\n",
+ "neither is this\n",
+ "'''\n",
+ "print(\"This is executed again\")"
]
},
{
"cell_type": "markdown",
- "id": "b9324c88",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- }
- },
- "source": [
- "_NOTE_: Scientific notation is supported!"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "3253e3d0",
+ "id": "8ee98ee9",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "avogadros_number = 6.022e23\n",
- "c = 2.998e8\n",
- "print(\"Avogadro's number =\", avogadros_number)\n",
- "print(\"Speed of light =\", c)"
+ "## User Input\n",
+ "Use `variable = input()` to recieve user input from the terminal"
]
},
{
"cell_type": "markdown",
- "id": "397f9f86",
+ "id": "52819c95",
"metadata": {
"slideshow": {
"slide_type": "slide"
- }
+ },
+ "tags": []
},
"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`"
+ "## Reading and Writing Files"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "3342a922",
+ "cell_type": "markdown",
+ "id": "a0966820",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "n = 3\n",
- "print(n)"
+ "**Opening a File**\n",
+ "\n",
+ "[Python Documentation (mode)](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "36e671e1",
+ "id": "38966db9",
"metadata": {
"slideshow": {
"slide_type": ""
- }
+ },
+ "tags": []
},
"outputs": [],
"source": [
- "type(n) # integer"
+ "# 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": "b68a5bbf",
+ "id": "aadba414",
"metadata": {
"slideshow": {
"slide_type": "slide"
- }
+ },
+ "tags": []
},
"source": [
- "**Numerical Operators** \n",
+ "**Reading from a File**\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",
+ "Here are some file functions for reading from a file:\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)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "7e7ef933",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Subtraction:\", 1 - 2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "28810d42",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Multiplication:\", 5 * 10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "8b66a0a4",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Division:\", 10 / 5)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "6984dbe1",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Modulus:\", 10 % 3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "74e37711",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "print(\"Exponentiation:\", 2 ** 3)"
+ "* 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": "a5e3ab30",
+ "id": "1f1185e6",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -650,42 +483,26 @@
"tags": []
},
"source": [
- "#### String"
+ "**Writing to a File**"
]
},
{
"cell_type": "markdown",
- "id": "ba570bf4",
+ "id": "179f132a",
"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)"
+ "One way we can write to files is using the `write()` function inside a with block."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "e3c09d2d",
+ "id": "911d44b3",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -694,43 +511,14 @@
},
"outputs": [],
"source": [
- "type(s) # string"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "50a4347d",
- "metadata": {
- "slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
- },
- "source": [
- "**String Concatenation** \n",
- "\n",
- "We can combine strings together."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "d9fadfdc",
- "metadata": {
- "slideshow": {
- "slide_type": ""
- }
- },
- "outputs": [],
- "source": [
- "# String concatenation\n",
- "hello_world = \"Hello,\" + \" World!\"\n",
- "print(hello_world)"
+ "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": "d20cb9b3",
+ "id": "b61a8691",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -738,12 +526,12 @@
"tags": []
},
"source": [
- "## Comments"
+ "## Lists"
]
},
{
"cell_type": "markdown",
- "id": "7ac031a7",
+ "id": "9c82edee",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -751,289 +539,7 @@
"tags": []
},
"source": [
- "Used to write notes or comments about code, as well as description of what the code is doing, or the variables used."
- ]
- },
- {
- "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": []
- },
- "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 line-by-line\n",
- "* file.readlines() - Read all lines into a List object"
- ]
- },
- {
- "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",
+ "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",
@@ -1042,527 +548,23 @@
},
{
"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",
+ "id": "8bb91a15",
"metadata": {
"slideshow": {
- "slide_type": "slide"
+ "slide_type": ""
},
"tags": []
},
"source": [
- "For Each Loop: \n",
- "A for each loop will execute its code for each item specified within the loop definition"
+ "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": "c7dc2e03",
+ "id": "fdc8600e",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1571,42 +573,56 @@
},
"outputs": [],
"source": [
- "animal_list = [\"Cat\", \"Dog\", \"Bird\"]\n",
- "\n",
- "for animal in animal_list:\n",
- " print(\"Current animal is:\", animal)"
+ "letter_list = [\"a\", \"b\", \"c\", \"d\", \"e\"] # A list of odd letters\n",
+ "print(letter_list)"
]
},
{
"cell_type": "markdown",
- "id": "76e99a09",
+ "id": "efc747a1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "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": [
- "The above describes `for each animal in animal_list`."
+ "letter_list[3] # access the 4th element in the list (starts counting at 0)"
]
},
{
"cell_type": "markdown",
- "id": "e5b05a2c",
+ "id": "b8369a1a",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": []
},
"source": [
- "---\n",
- "We are able to use a range-based for loop:"
+ "**Slicing** \n",
+ "You can also use slicing for specific partial list access."
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "b14f546d",
+ "id": "e0fac9bf",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1615,29 +631,27 @@
},
"outputs": [],
"source": [
- "for i in range(0, 5):\n",
- " print(i * 2) # Here we are manipulating the value each time!"
+ "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": "713884f6",
+ "cell_type": "markdown",
+ "id": "fe6ffaf2",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "for i in range(0, 10, 2): # Here we define a step of 2!\n",
- " print(i)"
+ "**Empty List** \n",
+ "You can create empty lists:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "b3078796",
+ "id": "0edf3fa6",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1645,13 +659,13 @@
},
"outputs": [],
"source": [
- "for i in range(10, 0, -1): # Counting down from 10, in steps of 1\n",
- " print(i)"
+ "new_list = [] # An empty list\n",
+ "print(new_list)"
]
},
{
"cell_type": "markdown",
- "id": "99b52e73",
+ "id": "f87cfa0f",
"metadata": {
"slideshow": {
"slide_type": "slide"
@@ -1659,117 +673,122 @@
"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."
+ "## Repetitions and Conditions\n"
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "136268d9",
+ "cell_type": "markdown",
+ "id": "eae67d96",
"metadata": {
"slideshow": {
- "slide_type": ""
- }
+ "slide_type": "slide"
+ },
+ "tags": []
},
- "outputs": [],
"source": [
- "n = 0\n",
- "\n",
- "while n < 10:\n",
- " print(n)\n",
- " n += 1"
+ "**Logical Operators** \n",
+ "We can determine conditions based on Boolean logic: using `and`, `or`, `not` to *evaluate* boolean expressions:"
]
},
{
"cell_type": "markdown",
- "id": "89cd06cc",
+ "id": "b38b2739",
"metadata": {
"slideshow": {
"slide_type": "fragment"
- }
+ },
+ "tags": []
},
"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."
+ "**AND:**"
]
},
{
- "cell_type": "markdown",
- "id": "b6c7b631",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "db33e773",
"metadata": {
"slideshow": {
- "slide_type": "slide"
- }
+ "slide_type": ""
+ },
+ "tags": []
},
+ "outputs": [],
"source": [
- "## Functions"
+ "a = True\n",
+ "b = False\n",
+ "\n",
+ "a and b\n",
+ "a or b\n",
+ "not a\n"
]
},
{
"cell_type": "markdown",
- "id": "ad3766a1",
+ "id": "9303b3ff",
"metadata": {
"slideshow": {
- "slide_type": ""
- },
- "tags": []
+ "slide_type": "slide"
+ }
},
"source": [
- "A function is a reusable block of code used to perform a specific task.\n",
- "\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."
+ "**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": "markdown",
- "id": "b83eb9fd",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a0af6ea1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
- },
- "tags": []
+ "slide_type": ""
+ }
},
+ "outputs": [],
"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."
+ "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": "25dddacf",
+ "id": "3246bbe1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"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."
+ "**Else/Elif** \n",
+ "\n",
+ "We may also specify either/or with `if/else`, and even add extra conditions with `elif`."
]
},
{
"cell_type": "markdown",
- "id": "aa2983d4",
+ "id": "8c87d3f8",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"tags": []
},
"source": [
- "Here is a very basic example of a function:"
+ "**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": "d4ca99fa",
+ "id": "c7dc2e03",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1778,45 +797,54 @@
},
"outputs": [],
"source": [
- "def sum_numbers(val_one, val_two):\n",
- " \"\"\"Sums and returns two numbers\"\"\"\n",
- " return val_one + val_two # Sum of both numbers\n",
+ "#for\n",
+ "animal_list = [\"Cat\", \"Dog\", \"Bird\"]\n",
"\n",
- "calculated_value = sum_numbers(1, 1)\n",
- "print(calculated_value)"
+ "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": "429c78c2",
+ "id": "ad3766a1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": "slide"
},
"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."
+ "## Functions"
]
},
{
"cell_type": "markdown",
- "id": "f64d31aa",
+ "id": "354a04f1",
"metadata": {
"slideshow": {
- "slide_type": "fragment"
+ "slide_type": ""
},
"tags": []
},
"source": [
- "---\n",
- "Here we are modifying setting a default value for our `name` argument:"
+ "A function is a reusable block of code used to perform a specific task.\n",
+ "\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.\n",
+ "Here is a very basic example of a function:"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "643f4038",
+ "id": "d4ca99fa",
"metadata": {
"slideshow": {
"slide_type": ""
@@ -1825,27 +853,12 @@
},
"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",
+ "def sum_numbers(val_one, val_two):\n",
+ " \"\"\"Sums and returns two numbers\"\"\"\n",
+ " return val_one + val_two # Sum of both numbers\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."
+ "calculated_value = sum_numbers(1, 1)\n",
+ "print(calculated_value)"
]
},
{
@@ -2245,13 +1258,97 @@
"source": [
"# Initialising a tuple with values\n",
"my_tuple = (1,2,3)\n",
- "print(my_tuple)\n",
- "\n",
+ "print(my_tuple)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7e873494-6a43-4402-beee-5bc2d507f33a",
+ "metadata": {
+ "remove_code": "non-comments",
+ "slideshow": {
+ "slide_type": ""
+ }
+ },
+ "outputs": [],
+ "source": [
"# Initialising a tuple with one value\n",
"my_tuple = (1,)\n",
"print(my_tuple)"
]
},
+ {
+ "cell_type": "markdown",
+ "id": "812d5a14",
+ "metadata": {
+ "editable": true,
+ "slideshow": {
+ "slide_type": "fragment"
+ },
+ "tags": []
+ },
+ "source": [
+ "Tuple Unpacking"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "22e2a236-df2d-4130-9810-5bfd4f0ccb49",
+ "metadata": {
+ "editable": true,
+ "remove_code": "non-comments",
+ "slideshow": {
+ "slide_type": ""
+ },
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "# The most basic example of tuple unpacking\n",
+ "a, b = 1, 2\n",
+ "print(a)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d2638ed7-4344-4297-b31b-bcdd649889c7",
+ "metadata": {
+ "editable": true,
+ "remove_code": "non-comments",
+ "slideshow": {
+ "slide_type": ""
+ },
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "# You can use tuple unpacking when a function returns multiple values, or a tuple!\n",
+ "def get_full_name():\n",
+ " return \"First\", \"Last\"\n",
+ "\n",
+ "first, last = get_full_name()\n",
+ "print(first, last)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "4ef4d631-b632-4712-a659-ed59f03c676b",
+ "metadata": {
+ "editable": true,
+ "slideshow": {
+ "slide_type": "notes"
+ },
+ "tags": []
+ },
+ "source": [
+ "Speaker Notes:\n",
+ "\n",
+ "We can omit the brackets here (for the first example)."
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -2297,7 +1394,7 @@
},
{
"cell_type": "markdown",
- "id": "812d5a14",
+ "id": "9380a5f8",
"metadata": {
"slideshow": {
"slide_type": "fragment"
@@ -4078,7 +3175,6 @@
"cell_type": "markdown",
"id": "dc4b7323-f4f7-4b51-b6da-f651abf5029e",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -4209,7 +3305,6 @@
"cell_type": "markdown",
"id": "04bec6e4-8e4d-4899-a3c3-c9abd332f275",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "slide"
},
@@ -4226,7 +3321,6 @@
"execution_count": null,
"id": "4de72e4c-2795-4b4d-8542-10729a74f44a",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -4245,7 +3339,6 @@
"cell_type": "markdown",
"id": "36f46ead-b9a1-481f-8798-edbfc58b5504",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -4259,7 +3352,6 @@
"cell_type": "markdown",
"id": "ccbef34a-9226-4735-859d-378640ee9cc7",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "notes"
},
@@ -4274,7 +3366,6 @@
"cell_type": "markdown",
"id": "fbf736e6-8dfb-4a00-86fb-5e71de650e9f",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "fragment"
},
@@ -4375,7 +3466,6 @@
"cell_type": "markdown",
"id": "e34aedb6",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "slide"
},
@@ -4389,7 +3479,6 @@
"cell_type": "markdown",
"id": "ffef8f84",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -4413,7 +3502,6 @@
"execution_count": null,
"id": "d5bd4280",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
},
@@ -4461,7 +3549,6 @@
"cell_type": "markdown",
"id": "a4fa9b5b",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -4550,7 +3637,6 @@
"cell_type": "markdown",
"id": "6ac1c80f",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": "notes"
}
@@ -4643,7 +3729,6 @@
"cell_type": "markdown",
"id": "80b12b8b",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -4728,7 +3813,6 @@
"cell_type": "markdown",
"id": "71a4546c",
"metadata": {
- "editable": true,
"slideshow": {
"slide_type": ""
}
@@ -6242,7 +5326,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3 (ipykernel)",
+ "display_name": "Python 3",
"language": "python",
"name": "python3"
},
@@ -6256,7 +5340,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.13.0"
+ "version": "3.6.8"
}
},
"nbformat": 4,