def filter_list(values, min_value=None, max_value=None):
"""Filters a list of numbers to keep only the ones between one or two thresholds.
You need to set at least one of min_value or max_value!
Parameters
----------
values : list
a list of numbers (floats or ints)
min_value : float or int, optional
the minimum threshold to filter the data (inclusive)
max_value : float or int, optional
the maximum threshold to filter the data (inclusive)
Examples
--------
>>> a = [1, 3, 4, 2.2]
>>> filter_list(a, min_value=2)
[3, 4, 2.2]
>>> a # a is unchanged
[1, 3, 4, 2.2]
>>> filter_list(a, max_value=3)
[1, 3, 2.2]
>>> filter_list(a, min_value=2, max_value=3)
[3, 2.2]
>>> filter_list([1, 2, 3, 4])
Traceback (most recent call last):
...
ValueError: Need to set at least one of min_value or max_value!
"""
if min_value is None and max_value is None:
raise ValueError('Need to set at least one of min_value or max_value!')
= []
output
# <your own code here>
return output
Assignment #02
Workshop 2
We will have a long break from our class and resume at Fri 3 November 2023. Make sure you are ready to start it off after the long break where we left it today. I recommend you do a little bit of coding every now and then, since regular practice is everything when it comes to learning a new language.
Reading material
Functions
- Work through the second half of the online course PY4E on Functions.
- From Section Adding new functions to the end.
Loops
- Work through the online course PY4E on Iterations.
Strings
- Work through select sections of the online course PY4E on Strings.
- Sections Traversal…, Looping…, Parsing strings, Debugging, Glossary
- Read Section 7.1.1 of the python documentation on f-strings.
Lists
- Work through the online course PY4E on Lists.
Exercises
Learning checklist
- I know the difference between parameters and arguments.
- I have a foundational understanding of the flow of program execution.
- I understand the advantages of using functions to build modular code that can be reused.
- I understand that loops are fundamental building blocks of programs.
- I can implement
for
andwhile
loops, and know how to skip individual iterations or break the loop entirely. - I know that strings are immutable.
- I can iterate through strings.
- I have a deeper skillset working with strings, such as parsing strings and applying useful string methods.
- I can use f-strings to write and format variables into strings.
- I have seen how to write a more thorough function documentation with Parameters and Examples sections.