24. Functions - advanced#

Author: Tue Nguyen

24.1. Outline#

  • Lambda functions

  • Scope of variables

  • Flexible arguments

  • Document a function

24.2. Lambda functions#

  • We use def to define regular functions

  • We use lambda quickly define simple functions (normally in one line)

  • Sometimes, we want to quickly define a function without a name (thinking of a name may be time-consuming)

    • They are called anonymous functions

    • We define anonymous functions using lambda

    • Anonymous functions are used to support other operations such as sorting and filtering

24.2.1. Named lambda functions#

  • Syntax: func_name = lambda params: code

  • code can be a statement or an expression

  • No need to use return because Python will return the value of the expression

a) Ex 1: greeting

# Define the function
say_hi = lambda name: print(f"Hi {name}")
# Call
say_hi("Tom")
Hi Tom

b) Ex 2: add 2 numbers

# Define
add = lambda a, b: a + b
# Call
add(2, 3)
5

Ex 3: \(f(x) = x^2 + 2x + 5\)

# Define
f = lambda x: x**2 + 2*x + 5
# Call
f(1)
8

Ex 4: \(g(x, y) = x^2 + 2xy + 3y^2\)

# Define
g = lambda x, y: x**2 + 2*x*y + 3*y**2
# Call
g(2, 1)
11

24.2.2. Anonymous lambda function#

  • Syntax: lambda params: code

  • An anonymous function cannot stand on its own but must be used in another operation such as sorting or filter

a) Ex 1: sorting

  • Suppose we have a list of students

    • Each element of this list is a tuple of 2 entries

    • The first entry is the student’s name

    • The second entry is the student grade

  • We might want to sort the student by name or grade

# Init the list of students
students = [("John", 6), ("Alex", 5.5), ("Jane", 9), ("Peter", 8)]
students
[('John', 6), ('Alex', 5.5), ('Jane', 9), ('Peter', 8)]
# Case 1: Sort by name (ascending order)
sorted(students, key=lambda x: x[0])
[('Alex', 5.5), ('Jane', 9), ('John', 6), ('Peter', 8)]

Comments

  • The function sorted will sort the list students by key

  • key expects a function that does something to each element of the list and returns a value for that element

  • sorted will then sort students based on those returned values

  • In the above example, key=lambda x: x[0] means that

    • Iterate through each element of the list students using variable x

    • At each element, return the first entry of x (or x[0])

    • Then sort the list students by those first entries

  • As a result, you can see the list students is sorted by name in the ascending order

# Case 2: Sort by name (descendin order)
# Just pass reverse=True
sorted(students, key=lambda x: x[0], reverse=True)
[('Peter', 8), ('John', 6), ('Jane', 9), ('Alex', 5.5)]
# Case 3: Sort by grade (ascending order)
# Very similar to Case 1
# Just need to change the expression to x[1]
sorted(students, key=lambda x: x[1])
[('Alex', 5.5), ('John', 6), ('Peter', 8), ('Jane', 9)]

Remarks

  • For now, the use of lambda functions is very limited including

    • Quickly define short and simple functions

    • Define anonymous function to use in sorted

  • However, anonymous lambda functions will be very convenient for real-life data manipulation when used with pandas (which will be discussed in a separate series)

24.3. Scope of variables#

To be updated

24.4. Flexible arguments#

To be updated

24.5. Document a function#

To be updated