diff --git a/docs/_quarto.yml b/docs/_quarto.yml index a6b6b83..91ebfe4 100644 --- a/docs/_quarto.yml +++ b/docs/_quarto.yml @@ -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 diff --git a/docs/notes/python-modules/index.qmd b/docs/notes/python-modules/index.qmd index c65c755..e19c788 100644 --- a/docs/notes/python-modules/index.qmd +++ b/docs/notes/python-modules/index.qmd @@ -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) diff --git a/docs/notes/python-modules/math.qmd b/docs/notes/python-modules/math.qmd new file mode 100644 index 0000000..6fb20bf --- /dev/null +++ b/docs/notes/python-modules/math.qmd @@ -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) +``` diff --git a/docs/notes/python-modules/random.qmd b/docs/notes/python-modules/random.qmd index 5da5374..2a2b969 100644 --- a/docs/notes/python-modules/random.qmd +++ b/docs/notes/python-modules/random.qmd @@ -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) ``` diff --git a/docs/notes/python-modules/statistics.qmd b/docs/notes/python-modules/statistics.qmd index c18f4be..4f6af71 100644 --- a/docs/notes/python-modules/statistics.qmd +++ b/docs/notes/python-modules/statistics.qmd @@ -9,6 +9,7 @@ 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 @@ -16,6 +17,8 @@ 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)) ```