Lesson 15 of 55
9 mins readPython Anonymous Functions, Lambdas & Do-Block Syntax
In Plain English
Anonymous functions allow creating quick callback functions inline with the -> operator. Julia do-block syntax provides an elegant way to pass multi-line closures as the first argument.
Deep Dive: How It Works
Lambda Syntax: x -> x^2 creates an anonymous function taking x and returning x^2. Multi-argument: (x, y) -> x + y.
Do-Block Syntax: map([1, 2, 3]) do x ... end translates to map(x -> begin ... end, [1, 2, 3]). It provides clean syntax for higher-order functions.
Core Rules to Remember

Arrow Operator ->: Create concise anonymous functions inline: filter(x -> x > 0, nums).

do-block Syntax: Elegant multi-line callback construct without awkward nested parenthesis.
Live Interactive Example
Hit Run Code to see it liveAnonymous Functions and Do-Blocks
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: Do-block passes the anonymous function as the first argument to map.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Double Array Elements with Lambda
Given `data = [10, 20, 30]`.
Use `map(x -> x * 2, data)` and print `"Doubled: "` followed by the result.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.