Skip to content

Commit

Permalink
Modules and Imports
Browse files Browse the repository at this point in the history
  • Loading branch information
s2t2 committed Jul 2, 2024
1 parent 91c416b commit e40123a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 27 deletions.
16 changes: 9 additions & 7 deletions docs/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,15 @@ website:
- section:
href: notes/python-modules/index.qmd
text: "Python Modules"
# contents:
# - section:
# href: notes/python-modules/datetime.qmd
# - section:
# href: notes/python-modules/random.qmd
# - section:
# href: notes/python-modules/statistics.qmd
contents:
- section:
href: notes/python-modules/math.qmd
- section:
href: notes/python-modules/random.qmd
- section:
href: notes/python-modules/statistics.qmd
- section:
href: notes/python-modules/datetime.qmd

#
# DATA PROCESSING
Expand Down
52 changes: 34 additions & 18 deletions docs/notes/python-modules/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,54 @@ execute:

# Python Modules

Some functionality comes from built-in libraries called modules.
Some functionality in Python comes from built-in libraries called [modules](https://docs.python.org/3/library/index.html).

## Import Strategies and Namespaces

When we use a variable or function that has been defined within one of these modules, we must **import** that functionality, using an `import` statement.

There are two different ways to import in Python.

## Survey of Popular Modules
First, we can import the entire module. If we do, this loads all the functions defined within, and we'll access those functions through the module **namespace**:

## The `math` Module
```python
# EXAMPLE HYPOTHETICAL PSEUDOCODE
import module_name

```{python}
import math
print(math.pi)
print(math.ceil(6.2))
module_name.function_name()
```

Importing only the function(s) you care about:
Alternatively, we can import only the function(s) we care about. This is slightly more performant, as unused functions are not loaded. If we do, we can invoke the functions directly:

```{python}
from math import pi, ceil
```python
# EXAMPLE HYPOTHETICAL PSEUDOCODE
from module_name import function_name

print(pi)
print(ceil(6.2))
function_name()
```

With either approach, we can use an **alias** to give the module or function(s) a short-hand name. If we do, we'll access those functions through the short-hand namespace:


Optional alias for imported module:
```python
# EXAMPLE HYPOTHETICAL PSEUDOCODE
import module_name as m

m.function_name()
```

```{python}
import math as m
```python
# EXAMPLE HYPOTHETICAL PSEUDOCODE
from module_name import function_name as fn

print(m.pi)
print(m.ceil(6.2))
fn()
```

## Survey of Python Modules

Here are some common modules in Python, and more information about how to use each:

+ [The `math` Module](./math.qmd)
+ [The `random` Module](./random.qmd)
+ [The `statistics` Module](./statistics.qmd)
+ [The `datetime` Module](./datetime.qmd)
31 changes: 31 additions & 0 deletions docs/notes/python-modules/math.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

---
format:
html:
code-fold: false
jupyter: python3
execute:
cache: true # re-render only when source changes
---

# The `math` Module

The [`math` module](https://docs.python.org/3/library/math.html) contains mathematical functions.

Constants such as PI:

```{python}
import math
math.pi
```

Floor or ceiling operations:

```{python}
math.floor(6.2)
```

```{python}
math.ceil(6.2)
```
21 changes: 19 additions & 2 deletions docs/notes/python-modules/random.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,38 @@ execute:

# The `random` Module

The [`random` module](https://docs.python.org/3/library/random.html) contains randomness related functions.

Random number generator:

```{python}
import random
random.random() #> 0.6357193086559119
print(random.random())
print(random.random())
print(random.random())
```

Random integer between lower and upper bound:

```{python}
random.randint(1, 100) #> 43
print(random.randint(1, 100))
print(random.randint(1, 100))
print(random.randint(1, 100))
```


Choose an item from a list at random:

```{python}
options = ["van", "choc", "straw"]
flavor = random.choice(options)
print("FLAVOR:", flavor)
flavor = random.choice(options)
print("FLAVOR:", flavor)
flavor = random.choice(options)
print("FLAVOR:", flavor)
```
3 changes: 3 additions & 0 deletions docs/notes/python-modules/statistics.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ execute:

# The `statistics` Module

The [`statistics` module](https://docs.python.org/3/library/statistics.html) contains statistical related functions, such as measures of central tendency:

```{python}
from statistics import mean, median, mode
my_nums = [1, 2, 2, 3, 4, 6, 7, 8, 9]
print(mean(my_nums))
print(median(my_nums))
print(mode(my_nums))
```

0 comments on commit e40123a

Please sign in to comment.