Add initial version of chapter 02, part 2
This commit is contained in:
parent
559c8e7b71
commit
9179521763
3 changed files with 1718 additions and 0 deletions
|
@ -62,6 +62,11 @@ Alternatively, the content can be viewed in a web browser
|
|||
- [exercises <img height="12" style="display: inline-block" src="static/link/to_nb.png">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/chapter_02_functions/01_exercises.ipynb)
|
||||
[<img height="12" style="display: inline-block" src="static/link/to_mb.png">](https://mybinder.org/v2/gh/webartifex/intro-to-python/develop?urlpath=lab/tree/chapter_02_functions/01_exercises.ipynb)
|
||||
(Volume of a Sphere)
|
||||
- [content 2 <img height="12" style="display: inline-block" src="static/link/to_nb.png">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/chapter_02_functions/02_content.ipynb)
|
||||
[<img height="12" style="display: inline-block" src="static/link/to_mb.png">](https://mybinder.org/v2/gh/webartifex/intro-to-python/develop?urlpath=lab/tree/chapter_02_functions/02_content.ipynb)
|
||||
(Standard Library: `math` & `random` Modules;
|
||||
Third-party Packages: `numpy` Library;
|
||||
Writing one's own Modules)
|
||||
|
||||
|
||||
#### Videos
|
||||
|
|
1637
chapter_02_functions/02_content.ipynb
Normal file
1637
chapter_02_functions/02_content.ipynb
Normal file
File diff suppressed because it is too large
Load diff
76
chapter_02_functions/sample_module.py
Normal file
76
chapter_02_functions/sample_module.py
Normal file
|
@ -0,0 +1,76 @@
|
|||
"""This is a sample module.
|
||||
|
||||
It defines three functions average(), average_evens(), and average_odds().
|
||||
The point is to show how we can put Python code in a .py file to be re-used
|
||||
in some other place.
|
||||
|
||||
We should never forget to document the code as well, both on the module
|
||||
level (i.e., this docstring) but also in every function it defines.
|
||||
|
||||
When imported, Python modules are executed top to bottom before the flow of
|
||||
execution returns to wherever they were imported into.
|
||||
|
||||
An important convention is to prefix variables and functions that are not to
|
||||
be used outside the module with a single underscore "_". This way, we can
|
||||
design the code within a module in a modular fashion and only "export" what we
|
||||
want.
|
||||
|
||||
Here, all three functions internally forward parts of their computations
|
||||
to the utility functions _round_all() and _scaled_average() that contain all
|
||||
the logic common to the three functions.
|
||||
|
||||
While this example is stylized, it shows how Python modules are often
|
||||
designed.
|
||||
"""
|
||||
|
||||
def _round_all(numbers):
|
||||
"""Internal utility function to round all numbers in a list."""
|
||||
return [round(n) for n in numbers]
|
||||
|
||||
|
||||
def _scaled_average(numbers, scalar):
|
||||
"""Internal utility function to calculate scaled averages."""
|
||||
average = sum(numbers) / len(numbers)
|
||||
return scalar * average
|
||||
|
||||
|
||||
def average(numbers, *, scalar=1):
|
||||
"""Calculate the average of all numbers in a list.
|
||||
|
||||
Args:
|
||||
numbers (list of int's/float's): numbers to be averaged;
|
||||
if non-whole numbers are provided, they are rounded
|
||||
scalar (float, optional): multiplies the average; defaults to 1
|
||||
|
||||
Returns:
|
||||
scaled_average (float)
|
||||
"""
|
||||
return _scaled_average(_round_all(numbers), scalar)
|
||||
|
||||
|
||||
def average_evens(numbers, *, scalar=1):
|
||||
"""Calculate the average of all even numbers in a list.
|
||||
|
||||
Args:
|
||||
numbers (list of int's/float's): numbers to be averaged;
|
||||
if non-whole numbers are provided, they are rounded
|
||||
scalar (float, optional): multiplies the average; defaults to 1
|
||||
|
||||
Returns:
|
||||
scaled_average (float)
|
||||
"""
|
||||
return _scaled_average([n for n in _round_all(numbers) if n % 2 == 0], scalar)
|
||||
|
||||
|
||||
def average_odds(numbers, *, scalar=1):
|
||||
"""Calculate the average of all odd numbers in a list.
|
||||
|
||||
Args:
|
||||
numbers (list of int's/float's): numbers to be averaged;
|
||||
if non-whole numbers are provided, they are rounded
|
||||
scalar (float, optional): multiplies the average; defaults to 1
|
||||
|
||||
Returns:
|
||||
scaled_average (float)
|
||||
"""
|
||||
return _scaled_average([n for n in _round_all(numbers) if n % 2 != 0], scalar)
|
Loading…
Reference in a new issue