Activity Sequence: Python

This is as an illustrative example for how Expression Tutor activities could be used in an advanced Python course. The content on this page may not be understandable for someone without significant prior knowledge of Python.

Python supports higher order functions, such as map. Such functions take other functions as arguments, and often those arguments are provided as anonymous functions, or lambdas. However, lambdas are not used very often in Python. The "Python way" is to use list comprehensions instead. Here is an example of using map to map a certain lambda over a list, followed by an example of how the same can be achieved using a list comprehension.

Lambdas

Given a list of strings (representing beautiful towns in Ticino, the mediterranean part of Switzerland):

['lugano', 'paradiso', 'morcote']

We would like to get a list where each town is capitalized (pun intended—the capital of Ticino isn't amongst our list). To do so, we map an anonymous function across our list. That function takes a string and returns a capitalized version of that string.

list(map(lambda x: x.capitalize(), ['lugano', 'paradiso', 'morcote']))

Below is a tree representation of the above expression. You can see nodes.

List Comprehensions

Assume.

[x.capitalize() for x in ['lugano', 'paradiso', 'morcote']]

Below is a tree representation of the above expression. You can see nodes.