Functions

Welcome back to the fourth week of the SCIF10002 course. This week we shall be looking at functions!

Table of Contents

Today’s Material

In this week’s class, you should start to notice how the core skills we’ve been building over the past few weeks begin to come together. Our focus will be on organizing code into well-structured, clearly delineated blocks called functions. As in the previous two classes, this week you have a choice of working through one of three notebooks.

Important: For today, please choose just one of the options below. You’ll have many opportunities to revisit the other concepts later in the course, so focus on building confidence with the Beginner material before tackling more advanced work.

Option 1: Beginner

If you are not very familiar with functions in Python, or would like some practice, today you should work through the Defining Functions notebook below:

This notebook introduces what functions are and shows you how to create them in Python. Be sure to read the explanations carefully and try each exercise along the way!

Option 2: Intermediate

This week’s intermediate notebook is intended only for those who are already confident with the material in the beginner notebook. lease ensure that you are able to complete the exercises in the beginner notebook before attempting this option. The intermediate material for this week is given below:

This notebook will build on your understanding of functions by introducing positional-only and keyword-only arguments, the use of arbitrary arguments, functions with multiple return values, and the concept of a recursive function.

Option 3: Advanced

If you already have solid experience with Python programming, start by reviewing the exercises in the following notebooks to confirm that you’re fully comfortable with the fundamentals:

Once you feel confident with these concepts, proceed to the advanced material:

In this notebook, you’ll be introduced to the lambda operator and the idea of a higher-order function - a function that takes other functions as input. You will then explore three widely used higher-order functions: map, filter, and reduce. These functions are especially useful when it comes to writing clear and concise code.

Getting Help

There is a lot to remember when first learning Python. To get more help see the Python API. Another useful link is the W3 schools series on Python which is very good, especially for new users. Another way to get help if you are ever unsure what a function is doing is to use thehelp function in the Python terminal like so:

In class, you can ask for help from the lecturer, demonstrators and your peers around you. We advise you do not ask AI for help unless you are really stuck.