Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 134 additions & 9 deletions Tutorials/IntroToPython.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,139 @@
"func()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Import\n",
"\n",
"<br>\n",
"\n",
"In Python, you can import libraries (also known as packages or modules) that can extend functionality of your code. Libraries contain pre-written code(such as classes and functions) that you can simply use in your code by importinhg without writing any additional code. \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In fact, this notebook incorporates common Python libraries like **math** and **random**, which provide a diverse range of functionalities including advanced mathematical computation techniques and the generation of pseudo-random numbers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To import a library, simply use the **import** keyword followed by the name of the library. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After importing the library, you can use its functions and classes in your code as required. A common way to do this is by using the dot notation which looks something like this:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"randomn_number = random.randint(1,10)\n",
"print(randomn_number)\n",
"\n",
"# randint is the function we are using from the random library."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This method imports the entire library. To import a specific class or function, use the following structure:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The square root of 25 is 5.0.\n"
]
}
],
"source": [
"from math import sqrt\n",
"\n",
"square_root = sqrt(25)\n",
"print(f'The square root of 25 is {square_root}.')\n",
"\n",
"# Import only the sqrt function from the math library. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also import libraries with aliases, which allows you to refer to them with shorter names. This can be done in the following way:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Array created with numpy: [1 2 3 4 5]\n"
]
}
],
"source": [
"import numpy as np # Using the numpy library with alias np so no need to write numpy in your code.\n",
"\n",
"array = np.array([1, 2, 3, 4, 5])\n",
"print(\"Array created with numpy:\", array)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Aliases are mostly used when the library name is too long or if the library is mentioned often in the code as aliases makes the code more concise and readable. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are plenty of libraries you can use in python so feel free to explore different libraries and use their documentation to understand how they work. This [website](https://flexiple.com/python/python-libraries) consists of 90+ libraries that can be imported in python but this list is not by any means complete. "
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -2411,16 +2544,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"- Add in how to import and use libraries (we are doing this implicitly all over the place but we should have some explicit examples of how to do this)\n",
"- The ml4a page has an \"Intro to Python\" Jupyter Notebook--skim it to see if there is anything relevant to pull over into this notebook"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -2439,7 +2564,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.10.12"
},
"toc": {
"base_numbering": 1,
Expand Down