summaryrefslogtreecommitdiff
path: root/tutorials/module_2/notebook_2/debugging_code.ipynb
diff options
context:
space:
mode:
authorChristian Kolset <christian.kolset@gmail.com>2025-04-24 16:25:31 -0600
committerChristian Kolset <christian.kolset@gmail.com>2025-04-24 16:25:31 -0600
commit652f88728eb91bae1c4f30b63d1fbe60788ea938 (patch)
tree65cfe591da183b969885e8c557b1ac5810727ec8 /tutorials/module_2/notebook_2/debugging_code.ipynb
parent42fca6122f4baf847ec2794b172abbc6a2193407 (diff)
Added jupyter notebook converter script. Converts markdown (.md) tutorials to jupyter notebook (.ipynb).
Diffstat (limited to 'tutorials/module_2/notebook_2/debugging_code.ipynb')
-rw-r--r--tutorials/module_2/notebook_2/debugging_code.ipynb138
1 files changed, 138 insertions, 0 deletions
diff --git a/tutorials/module_2/notebook_2/debugging_code.ipynb b/tutorials/module_2/notebook_2/debugging_code.ipynb
new file mode 100644
index 0000000..3bf0cd3
--- /dev/null
+++ b/tutorials/module_2/notebook_2/debugging_code.ipynb
@@ -0,0 +1,138 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Debugging Code\n",
+ "\n",
+ "## Introduction\n",
+ "\n",
+ "Have you ever had a piece of code not work the way you expected? What\n",
+ "did you do? You may have, asked a friend or used an AI assistant. In\n",
+ "this section, the following concepts are introduced - definition of a\n",
+ "bug, common types of bugs and debugging techniques.\n",
+ "\n",
+ "A *software bug* is an unintentional mistake or defect with a program,\n",
+ "this comes either from when the programmer makes a mistake in writing\n",
+ "the code or the code works in a way which has consequences that were not\n",
+ "foreseen by the programmer. Debugging is the act removing the bugs in\n",
+ "the software. Debugging is a normal part of programming that even\n",
+ "experiences developers spend a lot of time on.\n",
+ "\n",
+ "## Types of Bugs\n",
+ "\n",
+ "When writing code you are guaranteed to have bugs in your code. These\n",
+ "bugs can be categorized in the following three groups.\n",
+ "\n",
+ "- **Syntax errors** - this type of error occurs when the code fails\n",
+ " due to missing colons, missing indentation or a typo in code - some\n",
+ " languages like python are case sensitive meaning that the a capital\n",
+ " letter are different symbols.\n",
+ "- **Runtime errors** - e.g., dividing by zero or file not found.\n",
+ "- **Logical errors** - this may be the most dangerous that we need to\n",
+ " be careful with because this error can occur without any error\n",
+ " messages but it gives you the wrong result.\n",
+ "\n",
+ "## Debugging Techniques\n",
+ "\n",
+ "#### Print Debugging\n",
+ "\n",
+ "Insert print statements to check values of variables throughout the\n",
+ "program.\n",
+ "\n",
+ "``` python\n",
+ "def add(x, y):\n",
+ " print(f\"x = {x}, y = {y}\")\n",
+ " return x + y\n",
+ "```\n",
+ "\n",
+ "In the example above the print statement gives us feedback on what the\n",
+ "code is doing. The function in this example is obviously very simple,\n",
+ "but when we start applying more complex equations or function then\n",
+ "checking to see if the input variables are correct can indicate whether\n",
+ "there is an issue lies within the `add()` function or if the function is\n",
+ "given an incorrect input.\n",
+ "\n",
+ "#### Rubber Duck Debugging\n",
+ "\n",
+ "This is a technique by which you explaining your code line by line in\n",
+ "natural language to someone else, yourself or an inanimate object like a\n",
+ "rubber duck. This can help you spot your mistake in the code.\n",
+ "\n",
+ "#### Commenting Out Code\n",
+ "\n",
+ "Using comments to temporarily suppress parts of your code help you\n",
+ "isolate and find the bug.\n",
+ "\n",
+ "#### IDE Debugging tools\n",
+ "\n",
+ "Depending if you use an IDE, they often come with some sort of debugging\n",
+ "tools such as breakpoints, step into/over and variables explorers.\n",
+ "\n",
+ "#### AI Chat\n",
+ "\n",
+ "AI chat bots can help you find typo or fix logic in your code. You may\n",
+ "find yourself going through the steps above when using an AI assistant\n",
+ "to help you debug the code. However *never* assume that the code AI\n",
+ "gives you works the way you intend it to work.\n",
+ "\n",
+ "## Interactive Debugging Activity\n",
+ "\n",
+ "In the following code snippets, debug the code and document the\n",
+ "following: - What the bug is - How you found it (technique used) - What\n",
+ "actions you took to fix the bug\n",
+ "\n",
+ "#### Code 1\n",
+ "\n",
+ "``` python\n",
+ "def greet(name)\n",
+ " print(\"Hello, \" + Name)\n",
+ "greet(\"John\")\n",
+ "```\n",
+ "\n",
+ "#### Code 2\n",
+ "\n",
+ "``` python\n",
+ "import numpy as np\n",
+ "\n",
+ "x = np.linspace(0,5,100)\n",
+ "y = 1/x\n",
+ "\n",
+ "print(\"Result:\", y[0])\n",
+ "```\n",
+ "\n",
+ "#### Code 3\n",
+ "\n",
+ "``` python\n",
+ "def f(x):\n",
+ " return x**2 - 4 # Root at x = ±2\n",
+ "\n",
+ "def bisection(a, b, tol=1e-5, max_iter=100):\n",
+ " if f(a) * f(b) >= 0:\n",
+ " print(\"Bisection method fails. f(a) and f(b) should have opposite signs.\")\n",
+ " return None\n",
+ "\n",
+ " for i in range(max_iter):\n",
+ " c = (a + b) / 2\n",
+ " if abs(f(c)) < tol:\n",
+ " return c\n",
+ " elif f(c) * f(b) < 0:\n",
+ " a = c\n",
+ " else:\n",
+ " b = c\n",
+ " return (a + b) / 2\n",
+ "```\n",
+ "\n",
+ "## Reflection\n",
+ "\n",
+ "- What was the most challenging bug you found?\n",
+ "- What debugging method did you find most useful?"
+ ],
+ "id": "2949265c-5700-4fda-b785-ca5d7e913aa5"
+ }
+ ],
+ "nbformat": 4,
+ "nbformat_minor": 5,
+ "metadata": {}
+}