|
6 | 6 | "source": [
|
7 | 7 | "# **xSoc Python Course** - Week 6\n",
|
8 | 8 | "\n",
|
9 |
| - "### *Efficiency, Compactness, Readability*\n", |
| 9 | + "### *Exceptions, Efficiency, Compactness & Readability*\n", |
10 | 10 | "\n",
|
11 | 11 | "🖋️ *Written by Alia & Keegan from the [Computing Society](https://go.uwcs.uk/links)*"
|
12 | 12 | ]
|
|
43 | 43 | "cell_type": "markdown",
|
44 | 44 | "metadata": {},
|
45 | 45 | "source": [
|
46 |
| - "Seems fine? But what if the user decides they *don't* want to enter a number, what if, e.g., they enter \"Hello\"? Python doesn't know how to convert a string to an integer, so it will throw a **ValueError** and exit the program.\n", |
| 46 | + "Seems fine? But what if the user decides they *don't* want to enter a number, what if, e.g., they enter \"Hello\"? Python doesn't know how to convert a string to an integer, so it will throw a `ValueError` and exit the program.\n", |
47 | 47 | "\n",
|
48 |
| - "In reality, entering the wrong type of data isn't the end of the world. To stop Python from exiting immeadiately, we can surround any potentially problem-causing code in a `try-expect` block like this:" |
| 48 | + "In reality, entering the wrong type of data isn't the end of the world. To stop Python from exiting immediately, we can surround any potentially problem-causing code in a `try-except` block like this:" |
49 | 49 | ]
|
50 | 50 | },
|
51 | 51 | {
|
|
66 | 66 | "cell_type": "markdown",
|
67 | 67 | "metadata": {},
|
68 | 68 | "source": [
|
69 |
| - "The `try` statement tells the Python interpreter that something could go wrong in the following statements, but we know and have ways to deal with it. \n", |
| 69 | + "The `try` statement tells the Python interpreter that something could go wrong in the following statements, but we know and have ways to deal with it. Any statements after the `try-except` block will execute normally, even if a `ValueError` is raised.\n", |
70 | 70 | "\n",
|
71 | 71 | "Note: we need `num *= 2` and `print(num)` in the `try` block because if the user inputs something wrong, `num` won't be defined. If the statements were *after* the try statement, this could cause an error as `num` wouldn't be defined.\n",
|
72 | 72 | "\n",
|
|
95 | 95 | "source": [
|
96 | 96 | "All exceptions are a type of class, all inheriting from the **Exception** class. Also, certain exceptions are also subclasses of other exceptions, e.g. `ZeroDivisionError` is a subclass of `ArithmeticError`, here's a full hierarchy: \n",
|
97 | 97 | "\n",
|
98 |
| - "***Image of Exception hierarchy goes here***" |
| 98 | + "" |
99 | 99 | ]
|
100 | 100 | },
|
101 | 101 | {
|
|
129 | 129 | "source": [
|
130 | 130 | "Try running the above block and input a string. Now input 0. We've caused two different errors, but the same statement is being executed. Why?\n",
|
131 | 131 | "\n",
|
132 |
| - "Since all exceptions are a subclass of **Exception**, Python considers them to be of type *Exception*. Why does this matter? Python executes the *first relevant* catch statement - in this case it will *always* be an Exception, so the first statement will execute *regardless* of what type of exception we're throwing. You need to make sure that you put any catch statements for subclasses *above* those of their parent classes, otherwise the subclasses will never execute:" |
| 132 | + "Since all exceptions are a subclass of **Exception**, Python considers them to be of type **Exception**. Why does this matter? Python executes the *first relevant* except statement - in this case it will *always* be an Exception, so the first statement will execute *regardless* of what type of exception we're throwing. You need to make sure that you put any except statements for subclasses *above* those of their parent classes, otherwise the subclasses will never execute:" |
133 | 133 | ]
|
134 | 134 | },
|
135 | 135 | {
|
|
184 | 184 | "cell_type": "markdown",
|
185 | 185 | "metadata": {},
|
186 | 186 | "source": [
|
187 |
| - "We can also write our own custom exceptions. We define them the same way we define a class, making sure to say it inherits from **Exception**. Then, we can call it anywhere in our code by using `raise`:" |
| 187 | + "We can also write our own custom exceptions. We define them the same way we define a class, making sure it inherits from **Exception**. Then, we can call it anywhere in our code by using `raise`:" |
188 | 188 | ]
|
189 | 189 | },
|
190 | 190 | {
|
|
396 | 396 | "cell_type": "markdown",
|
397 | 397 | "metadata": {},
|
398 | 398 | "source": [
|
399 |
| - "***There should be a diagram of the exploding recursive call tree here.***" |
| 399 | + "" |
400 | 400 | ]
|
401 | 401 | },
|
402 | 402 | {
|
|
476 | 476 | "cell_type": "markdown",
|
477 | 477 | "metadata": {},
|
478 | 478 | "source": [
|
479 |
| - "***Image of first 6-7 rows of Pascal's Triangle Here, with n on the y axis going down, and k on the x-axis going right***" |
| 479 | + "" |
480 | 480 | ]
|
481 | 481 | },
|
482 | 482 | {
|
|
640 | 640 | "cell_type": "markdown",
|
641 | 641 | "metadata": {},
|
642 | 642 | "source": [
|
643 |
| - "The fancier name for this is a **binary search**, because at each step, we're dividing the search space we have left into 2. So, how much faster is this method than a regular, linear search? Well, for small lists you won't see much improvement, but for larger lists, say, with $n$ items, we only need to check $\\log(n)$ of them in the worst case! The difference couldn't be clearer:" |
| 643 | + "The fancier name for this is a **Binary Search**, because at each step, we're dividing the search space we have left into 2. So, how much faster is this method than a regular, linear search? Well, for small lists you won't see much improvement, but for larger lists, say, with $n$ items, we only need to check $\\log(n)$ of them in the worst case! The difference couldn't be clearer:" |
644 | 644 | ]
|
645 | 645 | },
|
646 | 646 | {
|
647 | 647 | "cell_type": "markdown",
|
648 | 648 | "metadata": {},
|
649 | 649 | "source": [
|
650 |
| - "***Comparison between y = x and y = log(x) on a graph***" |
| 650 | + "" |
651 | 651 | ]
|
652 | 652 | },
|
653 | 653 | {
|
|
1208 | 1208 | "orig_nbformat": 4,
|
1209 | 1209 | "vscode": {
|
1210 | 1210 | "interpreter": {
|
1211 |
| - "hash": "bb33f6d328f18c07440802b8c66874c52744b86bff5cfe8eb1d71afeb55a2150" |
| 1211 | + "hash": "b852fe1f214fe6a0387799bd52159c64dfd73c3955c21916f0ce4da0f34f6d48" |
1212 | 1212 | }
|
1213 | 1213 | }
|
1214 | 1214 | },
|
|
0 commit comments