|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Function\n", |
| 7 | + "**Table of contents**<a id='toc0_'></a> \n", |
| 8 | + "- [Function](#toc1_) \n", |
| 9 | + "- [Python Function Types](#toc2_) \n", |
| 10 | + " - [Built-in functions](#toc2_1_) \n", |
| 11 | + "- [User-defined Functions](#toc3_) \n", |
| 12 | + "- [Python Function Arguments](#toc4_) \n", |
| 13 | + " - [Function Argument with Default Values](#toc4_1_) \n", |
| 14 | + " - [Keyword Argument](#toc4_2_) \n", |
| 15 | + "- [Function With Arbitrary Arguments](#toc5_) \n", |
| 16 | + "- [Python Recursive Function](#toc6_) \n", |
| 17 | + "- [Python Lambda/Anonymous Function](#toc7_) \n", |
| 18 | + "- [Python Modules](#toc8_) \n", |
| 19 | + " - [Python Standard Library Modules](#toc8_1_) \n", |
| 20 | + "- [The dir() built-in function](#toc9_) \n", |
| 21 | + "- [Python Package](#toc10_) \n", |
| 22 | + " - [Package Model Structure in Python Programming](#toc10_1_) \n", |
| 23 | + " - [Importing module from a package](#toc10_1_1_) \n", |
| 24 | + "\n", |
| 25 | + "<!-- vscode-jupyter-toc-config\n", |
| 26 | + "\tnumbering=false\n", |
| 27 | + "\tanchor=true\n", |
| 28 | + "\tflat=false\n", |
| 29 | + "\tminLevel=1\n", |
| 30 | + "\tmaxLevel=6\n", |
| 31 | + "\t/vscode-jupyter-toc-config -->\n", |
| 32 | + "<!-- THIS CELL WILL BE REPLACED ON TOC UPDATE. DO NOT WRITE YOUR TEXT IN THIS CELL -->" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "metadata": {}, |
| 38 | + "source": [ |
| 39 | + "# <a id='toc1_'></a>[Function](#toc0_)\n", |
8 | 40 | "\n",
|
9 | 41 | "- Function is a code block that is used to perform a specific task.\n",
|
10 | 42 | "- Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.\n",
|
11 | 43 | "\n",
|
12 |
| - "# Python Function Types\n", |
| 44 | + "# <a id='toc2_'></a>[Python Function Types](#toc0_)\n", |
13 | 45 | "\n",
|
14 | 46 | "- Built-in functions (aka Standard library functions)\n",
|
15 | 47 | "- User-defined functions\n",
|
16 | 48 | "\n",
|
17 |
| - "## Built-in functions\n", |
| 49 | + "## <a id='toc2_1_'></a>[Built-in functions](#toc0_)\n", |
18 | 50 | "\n",
|
19 | 51 | "- Function that already exist in programming language to peform specific task in optimized way. \n",
|
20 | 52 | "- Exp: `print()`, `type()`, `id()`, `pow(2,3)`\n",
|
|
210 | 242 | "cell_type": "markdown",
|
211 | 243 | "metadata": {},
|
212 | 244 | "source": [
|
213 |
| - "# User-defined Functions\n", |
| 245 | + "# <a id='toc3_'></a>[User-defined Functions](#toc0_)\n", |
214 | 246 | "\n",
|
215 | 247 | "- These functions are custom functions that are defined to perform a specific task repeatedly. \n",
|
216 | 248 | "\n",
|
217 |
| - "# Python Function Arguments\n", |
| 249 | + "# <a id='toc4_'></a>[Python Function Arguments](#toc0_)\n", |
218 | 250 | "- In computer programming, an argument is a value that is accepted by a function.\n",
|
219 | 251 | "\n",
|
220 |
| - "## Function Argument with Default Values\n", |
| 252 | + "## <a id='toc4_1_'></a>[Function Argument with Default Values](#toc0_)\n", |
221 | 253 | "- provide default values to function arguments\n",
|
222 | 254 | "\n",
|
223 | 255 | "\n",
|
|
227 | 259 | " print('Sum:', sum)\n",
|
228 | 260 | "```\n",
|
229 | 261 | "\n",
|
230 |
| - "## Keyword Argument\n", |
| 262 | + "## <a id='toc4_2_'></a>[Keyword Argument](#toc0_)\n", |
231 | 263 | "- In keyword arguments, arguments are assigned based on the name of arguments.\n",
|
232 | 264 | "\n",
|
233 | 265 | "```\n",
|
|
239 | 271 | "```\n",
|
240 | 272 | "\n",
|
241 | 273 | "\n",
|
242 |
| - "# Function With Arbitrary Arguments\n", |
| 274 | + "# <a id='toc5_'></a>[Function With Arbitrary Arguments](#toc0_)\n", |
243 | 275 | "- When we do not know in advance the number of arguments that will be passed into a function. To handle this kind of situation, we can use arbitrary arguments in Python.\n",
|
244 | 276 | "- We use an asterisk (*) before the parameter name to denote this kind of argument\n",
|
245 | 277 | "\n",
|
|
434 | 466 | "cell_type": "markdown",
|
435 | 467 | "metadata": {},
|
436 | 468 | "source": [
|
437 |
| - "# Python Recursive Function\n", |
| 469 | + "# <a id='toc6_'></a>[Python Recursive Function](#toc0_)\n", |
438 | 470 | "- In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as `recursive functions`.\n",
|
439 | 471 | "\n",
|
440 | 472 | "**Base condition (Terminating conditon)**\n",
|
|
478 | 510 | "cell_type": "markdown",
|
479 | 511 | "metadata": {},
|
480 | 512 | "source": [
|
481 |
| - "# Python Lambda/Anonymous Function\n", |
| 513 | + "# <a id='toc7_'></a>[Python Lambda/Anonymous Function](#toc0_)\n", |
482 | 514 | "\n",
|
483 | 515 | "- In Python, a lambda function is a special type of function without the function name. \n",
|
484 | 516 | "\n",
|
|
615 | 647 | "cell_type": "markdown",
|
616 | 648 | "metadata": {},
|
617 | 649 | "source": [
|
618 |
| - "# Python Modules\n", |
| 650 | + "# <a id='toc8_'></a>[Python Modules](#toc0_)\n", |
619 | 651 | "\n",
|
620 | 652 | "Module is a file that contains code to perform a specific task. A module may contain variables, functions, classes etc.\n",
|
621 | 653 | "\n",
|
|
625 | 657 | "- `Access Function defined in Module` >> `arithmetic.add()`\n",
|
626 | 658 | "\n",
|
627 | 659 | "\n",
|
628 |
| - "## Python Standard Library Modules\n", |
| 660 | + "## <a id='toc8_1_'></a>[Python Standard Library Modules](#toc0_)\n", |
629 | 661 | "\n",
|
630 | 662 | "- The Python standard library contains well over 200 modules.\n",
|
631 | 663 | "\n",
|
|
637 | 669 | "print(\"The value of pi is\", math.pi)\n",
|
638 | 670 | "```\n",
|
639 | 671 | "\n",
|
640 |
| - "# The dir() built-in function\n", |
| 672 | + "# <a id='toc9_'></a>[The dir() built-in function](#toc0_)\n", |
641 | 673 | "\n",
|
642 | 674 | "In Python, we can use the dir() function to list all the function names in a module.\n",
|
643 | 675 | "\n",
|
|
690 | 722 | "cell_type": "markdown",
|
691 | 723 | "metadata": {},
|
692 | 724 | "source": [
|
693 |
| - "# Python Package\n", |
| 725 | + "# <a id='toc10_'></a>[Python Package](#toc0_)\n", |
694 | 726 | "\n",
|
695 | 727 | "\n",
|
696 | 728 | "- A package is `a container` that contains various functions to perform specific tasks. For example, the `math` package includes the `sqrt()` function to perform the square root of a number.\n",
|
|
703 | 735 | "- A directory must contain a file named __init__.py in order for Python to consider it as a package. \n",
|
704 | 736 | " - This file can be left empty but we generally place the initialization code for that package in this file.\n",
|
705 | 737 | "\n",
|
706 |
| - "## Package Model Structure in Python Programming\n", |
| 738 | + "## <a id='toc10_1_'></a>[Package Model Structure in Python Programming](#toc0_)\n", |
707 | 739 | "\n",
|
708 | 740 | "Suppose we are developing a game. One possible organization of packages and modules could be as shown in the figure below.\n",
|
709 | 741 | "\n",
|
|
717 | 749 | "\n",
|
718 | 750 | "\n",
|
719 | 751 | "\n",
|
720 |
| - "### Importing module from a package\n", |
| 752 | + "### <a id='toc10_1_1_'></a>[Importing module from a package](#toc0_)\n", |
721 | 753 | "```\n",
|
722 | 754 | "import Game.Level.start\n",
|
723 | 755 | "Game.Level.start.select_difficulty(2)\n",
|
|
0 commit comments