1 of 27

Week 12

Advanced Course in Programming

27.11.2025

2 of 27

Last Week

List comprehension

Filtering items in comprehensions

Dictionary comprehensions

Recursion

3 of 27

Feedback from Week 11

Recursion still seems confusing – why use it, how is it useful?

​

What's the difference between a function and a method?

​

​

4 of 27

Sorting

Problematic if we want to sort something like tuples or our own objects

​

E.g. tuples are by default sorted based on their first item

5 of 27

Solution: provide your own value function

6 of 27

Defining functions inside other functions

A "helper function" that is not needed elsewhere can be defined inside other function

7 of 27

Lambda-expression

Creates an anonymous function

​

Syntax:

lambda <parameters> : <expression>

8 of 27

For example

9 of 27

That means that…

10 of 27

Min and max

Functions min and max also have an optional key parameter

11 of 27

Function as an argument

In Python, a function can be passed as an argument:

12 of 27

Generators

Sometimes it would be useful to return values from a "series" one at a time without needing to generate the entire list

​

For this reason, we can use generator functions

13 of 27

For example

Generator which returns values until maximum

14 of 27

StopIteration

Generator throws a StopIteration event when there are no more values to fetch

15 of 27

Generator "comprehension"

An alternative syntax for creating a generator with a single expression

16 of 27

Functional programming

A programming paradigm where the changes in state are avoided

Lambda and expressions are examples of this

Other paradigms:

  • Imperative
  • Procedural
  • Object-oriented

17 of 27

map

Performs the given operation for all items in the sequence

​

18 of 27

Return value of map

map does not return a list; instead, it returns a sequence which can be iterated once

19 of 27

filter

Only selects some of the items in the original sequence based on a condition

20 of 27

reduce

Reduces the iterable sequence into a single value

21 of 27

Regular Expressions

A "language" for filtering and searching for strings

​

Own syntax for defining the set of accepted strings

22 of 27

In Python

23 of 27

Rules

Alternative choices can be defined with a vertical bar

24 of 27

Rules (2)

A group of accepted characters (or substrings) is given in square brackets

25 of 27

Rules (3)

Number required:

​

* zero or more

+ one or more

{m} exactly m

26 of 27

Other special characters

Dot denotes any character

^ means that the match must be in the beginning

$ means that the match must be in the end

27 of 27

Next Week

One more lecture.

Game programming with Pygame

​