|
6 | 6 | "source": [
|
7 | 7 | "# **xSoc Python Course** - Week 6\n",
|
8 | 8 | "\n",
|
9 |
| - "### *Efficiency, Compactness, Readability*\n", |
| 9 | + "### *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",
|
|
139 | 139 | "source": [
|
140 | 140 | "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",
|
141 | 141 | "\n",
|
142 |
| - "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:" |
| 142 | + "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:" |
143 | 143 | ]
|
144 | 144 | },
|
145 | 145 | {
|
|
194 | 194 | "cell_type": "markdown",
|
195 | 195 | "metadata": {},
|
196 | 196 | "source": [
|
197 |
| - "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`:" |
| 197 | + "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`:" |
198 | 198 | ]
|
199 | 199 | },
|
200 | 200 | {
|
|
497 | 497 | "metadata": {},
|
498 | 498 | "source": [
|
499 | 499 | ""
|
500 |
| - ] |
| 500 | + ] |
501 | 501 | },
|
502 | 502 | {
|
503 | 503 | "cell_type": "markdown",
|
|
671 | 671 | "cell_type": "markdown",
|
672 | 672 | "metadata": {},
|
673 | 673 | "source": [
|
674 |
| - "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:" |
| 674 | + "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:" |
675 | 675 | ]
|
676 | 676 | },
|
677 | 677 | {
|
|
684 | 684 | "metadata": {},
|
685 | 685 | "source": [
|
686 | 686 | ""
|
687 |
| - ] |
| 687 | + ] |
688 | 688 | },
|
689 | 689 | {
|
690 | 690 | "cell_type": "markdown",
|
|
1370 | 1370 | },
|
1371 | 1371 | "vscode": {
|
1372 | 1372 | "interpreter": {
|
1373 |
| - "hash": "7c57092efe4ff60dfd9ba12cd3127c3cb8001227526172b4b54478afc6e523e7" |
1374 |
| - } |
| 1373 | + "hash": "7c57092efe4ff60dfd9ba12cd3127c3cb8001227526172b4b54478afc6e523e7"} |
1375 | 1374 | }
|
1376 | 1375 | },
|
1377 | 1376 | "nbformat": 4,
|
|
0 commit comments