thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

04.01 · Transform and filter values / Learn · 9 min

Write a small function as one expression

A lambda receives arguments and returns the value of one expression.

Activity 1 of 6 in Transform and filter values

All courses

Python 3: Lists / 04.01

Understand lambda

Not started

Read a lambda expression, compare it with def and use a simple sorting key.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob traces two inputs, 3 and 5, through an addition expression to a returned 8.
Rob’s comic strip · Transform and filter values

Arguments go before the colon; the expression follows

lambda first, second: first + second creates a function with two parameters. Calling it with 3 and 5 evaluates the expression and returns 8. There is no return keyword inside the expression.

The source lesson is text-only, so there is no video to unlock here. This example gives the function a name so you can call and inspect it. For an ordinary reusable named function, def is usually clearer.

Arguments go before the colon; the expression follows
collections.pypython · example
add = lambda first, second: first + second
print(add(3, 5))  # 8

def add_with_def(first, second):
    return first + second

print(add_with_def(3, 5))  # 8

Use a small expression where another function is expected

A sorting key function receives one item and returns the value used for comparison. Here each record is a two-item list: a name and a score. record[1] selects its score. The original records are retained in the sorted result.

The source also previews tuple and dictionary records. These examples use lists so you can concentrate on the function without needing a new collection type. A lambda is not automatically faster or better than a named function.

Use a small expression where another function is expected
collections.pypython · example
records = [["Ada", 80], ["Rob", 60], ["Grace", 90]]
by_score = sorted(records, key=lambda record: record[1])
print(by_score)
print(records)  # original order

Keep it readable

A lambda body is one expression, not a block of statements. Use def when the task needs several statements, a clearer name, comments or more explanation.

The next practice uses a named square lambda to make the returned values easy to inspect. Then map and filter will show where passing a short function directly can be useful.

Build it one step at a time

Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

3 steps · Saved work · Instant feedback

YOUR TURN

Return the square, not the double

Change square to a one-expression square function. Keep its calls for 7 and -3 and print their stored results, 49 then 9.

Expected output
49
9

Keep the starting inputs. Print these lines in this order, without extra output.

  • Store the requested seven result
  • Store the requested negative result
  • Output 1 matches the requested result
  • Output 2 matches the requested result

Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Square is not double

Multiply number by itself or raise it to the power 2. Let the expression use its argument.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

A lambda creates a callable expression. Its result comes from evaluating that expression for each call.