Skip to content
Merged
Show file tree
Hide file tree
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
587 changes: 106 additions & 481 deletions 01_getting_started.ipynb → 01.1_getting_started.ipynb

Large diffs are not rendered by default.

1,014 changes: 1,014 additions & 0 deletions 01.2_getting_started.ipynb

Large diffs are not rendered by default.

874 changes: 0 additions & 874 deletions 02.1_getting_started.ipynb

This file was deleted.

165 changes: 47 additions & 118 deletions 02.2_libraries.ipynb → 02.1_libraries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,41 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"![Practicum AI Logo image](https://github.com/PracticumAI/practicumai.github.io/blob/main/images/logo/PracticumAI_logo_250x50.png?raw=true)\n",
"![Practicum AI Logo image](https://github.com/PracticumAI/practicumai.github.io/blob/main/images/logo/PracticumAI_logo_250x50.png?raw=true) <img src='https://github.com/PracticumAI/practicumai.github.io/blob/main/images/icons/practicumai_python.png?raw=true' align='right' width=50>\n",
"\n",
"# *Practicum AI Python*: Libraries\n",
"\n",
"***\n",
"This exercise adapted from the <a href=\"https://github.com/swcarpentry/python-novice-gapminder\">Software Carpentries</a>\n",
"\n",
"(15 Minutes: Presentation)\n",
"\n",
"# Libraries\n",
"## 1. The power of a programming language is in its libraries\n",
"\n",
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Most of the power of a programming language is in its libraries."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* A *library* is a collection of files (called *modules*) that contains functions for use by other programs.\n",
" * May also contain data values (e.g., numerical constants) and other things.\n",
" * Library's contents are supposed to be related, but there's no way to enforce that.\n",
"* The Python [standard library](https://docs.python.org/3/library/) is an extensive suite of modules that comes with Python itself.\n",
"* Many additional libraries are available from [PyPI](https://pypi.python.org/pypi/) (the Python Package Index).\n",
"* We will see later how to write new libraries."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Libraries and modules"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A library is a collection of modules, but the terms are often used interchangeably, especially since many libraries only consist of a single module, so don't worry if you mix them."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A program must import a library module before using it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* We will see later how to write new libraries.\n",
"\n",
"### 1.1 Libraries and modules\n",
"\n",
"A library is a collection of modules, but **the terms are often used interchangeably**, especially since many libraries only consist of a single module.\n",
"\n",
"### 1.2. A program must import a library module before using it\n",
"\n",
"* Use `import` to load a library module into a program's memory.\n",
"* Then refer to things from the module as `module_name.thing_name`.\n",
" * Python uses `.` to mean \"part of\".\n",
"* Using `math`, one of the modules in the standard library:"
"* Here is an example of using the `math` library, one of the libraries in the standard library:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pi is 3.141592653589793\n",
"cos(pi) is -1.0\n"
]
}
],
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"\n",
Expand All @@ -90,21 +50,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"* Have to refer to each item with the module's name.\n",
"* You have to refer to each item using the containing module's name.\n",
" * `math.cos(pi)` won't work: the reference to `pi` doesn't somehow \"inherit\" the function's reference to `math`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use `help` to learn about the contents of a library module."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3. Use `help` to learn about the contents of a library module.\n",
"\n",
"Works just like help for a function."
]
},
Expand Down Expand Up @@ -150,30 +105,17 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import specific items from a library module to shorten programs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Use `from ... import ...` to load only specific items from a library module.\n",
"## 2. Import specific items from a library module to shorten programs\n",
"\n",
"* >Use `from ______ import _____` to load only specific items from a library.\n",
"* Then refer to them directly without library name as prefix."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cos(pi) is -1.0\n"
]
}
],
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from math import cos, pi\n",
"\n",
Expand All @@ -184,33 +126,20 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create an alias for a library module when importing it to shorten programs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Use `import ... as ...` to give a library a short *alias* while importing it.\n",
"## 3. Create an alias for a library module when importing it to shorten programs\n",
"\n",
"* Use `import _____ as _____` to give a library a short *alias* while importing it.\n",
"* Then refer to items in the library using that shortened name."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cos(pi) is -1.0\n"
]
}
],
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math as m\n",
"b\n",
"\n",
"print('cos(pi) is', m.cos(m.pi))"
]
},
Expand All @@ -219,8 +148,9 @@
"metadata": {},
"source": [
"* Commonly used for libraries that are frequently used or have long names.\n",
" * E.g., `matplotlib` plotting library is often aliased as `mpl`.\n",
"* But can make programs harder to understand, since readers must learn your program's aliases."
" * E.g., The `pandas` library is often aliased as `pd`.\n",
"* But can make programs harder to understand, since readers must learn your program's aliases.\n",
"* While you *can* use anything for an alias, if there is a commonly used alias, like `pd`, use the convention! "
]
},
{
Expand Down Expand Up @@ -256,11 +186,12 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"tags": []
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -314,7 +245,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
Expand Down Expand Up @@ -390,7 +321,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
Expand Down Expand Up @@ -447,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
Expand Down Expand Up @@ -496,7 +427,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
Expand Down Expand Up @@ -572,7 +503,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
Expand Down Expand Up @@ -630,11 +561,9 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
}
"tags": []
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -685,7 +614,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
Expand Down
Loading