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

  1. Work through the second half of the online course PY4E on Functions.
    • From Section Adding new functions to the end.

Loops

  1. Work through the online course PY4E on Iterations.

Strings

  1. Work through select sections of the online course PY4E on Strings.
    • Sections Traversal…, Looping…, Parsing strings, Debugging, Glossary
  2. Read Section 7.1.1 of the python documentation on f-strings.

Lists

  1. Work through the online course PY4E on Lists.

Exercises

Classroom exercises

Do the exercises we didn’t do in class:

  • #02-03 Classification of numpy array
  • #02-04 More advanced numpy modifications

I also recommend you recap the classroom exercises that we did do together.

#02-06: Robustify years2days()

Today we coded a function years2days().

  1. So far, the error message (“traceback”) that we get when providing the function with a different data type other than int or float is very cryptic and a user might not learn from the message what they did wrong or why the function fails. Modify the function, so that it prints a meaningful message when the user tries to execute years2days("ten").
  2. So far, n_leapyears is simply added to the conversion result. The function would happily execute the statement years2days(1, n_leapyears=2), which is obviously meaningless. Implement a check that prints a message and does not execute the conversion if n_leapyears is more than years.
#02-07: Hello, conditional execution

Write a program that uses input to prompt a user for their name and preferred language, and then welcomes them. The output should look like

Enter your name: Florian
Enter your language (EN, DE, XX): EN
How's it, Florian!

The allowed languages are English, German, and a third language of your choice. If the language is not one of these three options, the program should print an error message and exit.

Try to make your program code as organised and easy-to-read as possible. To do so, pack the language classification that selects the welcome message into a function welcome_message(name, language) that you use in the main part of your script.

#02-08: Your own equivalent of numpy.array_equal()

Earlier we used numpy.array_equal() to test whether two arrays had the same shape and same values. Code your own function that does the very same! Test whether your own function and the numpy function yield the same results.

Exercise #02-09: Define a function that filters lists

Below, I pasted the skeleton of a function I want you to code. Most importantly, I provide you with the documentation of the function. We read function documentation in the past (using help()), and we saw how to write the docstring for a function that we wrote ourselves, too. Here, you see a more comprehensive example of a function documentation. Carefully read the documentation and try to understand what the function is supposed to do. The examples help illustrate the information provided by the docstring and the parameter definitions.

  • None: We have not yet talked about the None keyword, but if you consult the Internet you will quickly see that it is easy to understand.
  • So far, we have used the print() function to craft (“error”) messages to the user. In the function below, I demonstrate you how a “proper” error can be raised.
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
  1. You will have to iterate through the provided list of values …
  2. … and use a well-crafted conditional expression to decide which values …
  3. … need to be ‘added’ to the output list (find the right function yourself in our classroom material!)

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 and while 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.