• Home
  • Python
    • Introduction to Python
    • Python Developer
    • Expert Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
    • Expert JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
    • Expert React Developer
  • Linux
    • CyberSecurity - Introduction to Linux
    • CyberSecurity - Linux Administrator
    • CyberSecurity - Expert Linux Administrator
  • Active Directory
    • CyberSecurity - Introduction to Active Directory
    • CyberSecurity - Active Directory Administrator
    • CyberSecurity - Expert Active Directory Administrator
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
      • Expert Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
      • Expert JavaScript Developer
    • React
      • Introduction to React
      • React Developer
      • Expert React Developer
    • Linux
      • CyberSecurity - Introduction to Linux
      • CyberSecurity - Linux Administrator
      • CyberSecurity - Expert Linux Administrator
    • Active Directory
      • CyberSecurity - Introduction to Active Directory
      • CyberSecurity - Active Directory Administrator
      • CyberSecurity - Expert Active Directory Administrator
  • Interactive Training
  • Pricing
  • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Brainstorm

Courses

  • Python
    • Introduction to Python
    • Python Developer
    • Expert Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
    • Expert JavaScript Developer
  • React
    • Introduction to React
    • React Developer
    • Expert React Developer
    • Professional Master React Developer
  • Linux
    • CyberSecurity - Introduction to Linux
    • CyberSecurity - Linux Administrator
    • CyberSecurity - Expert Linux Administrator
  • Active Directory
    • CyberSecurity - Introduction to Active Directory
    • CyberSecurity - Active Directory Administrator
    • CyberSecurity - Expert Active Directory Administrator

Newsletter

Subscribe to our monthly newsletter, for a quick update on Python, JavaScript, React news

© 2024 - 2026 STEMTrainingGrounds. All Rights Reserved.

Lesson 13

Lesson 13 of 34

Lambda Functions

Lesson Progress: 0%

Code Example

# lambda function
add = lambda a, b: a + b
print(add(2, 3))

# the lambda function does the same as
def add(a, b):
    return a + b
print(add(2, 3))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • A lambda is a short way to make a small function in one line.
  • It can take inputs like a and b and quickly give back a result.
  • The lambda example and the def example do the same job.
  • Use print() to see the answer that the function returns.

Code Example

double = lambda x: x * 2
print(double(4))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • A lambda is a mini-tool that lets you write a whole math rule on just one short line.
  • You give it a name and tell it what to do, then you can use that name whenever you want to run your rule.

Code Example

# A quick way to say hello to someone
greet = lambda name: "Hello " + name

print(greet("Sam"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • You can use a lambda to build a quick "greeting machine" that adds words together for you.
  • Just give the machine a name like greet, and it will put "Hello" in front of any name you type.

Code Example

shout = lambda word: word.upper()

print(shout("hello"))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • You can make a mini-tool that changes how words look, like turning small letters into big LOUD letters.
  • By using .upper() inside your lambda, you tell Python to "shout" whatever word you give it.

Code Example

is_even = lambda num: num % 2 == 0

print(is_even(4))
print(is_even(5))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • You can build a quick "number checker" that knows if a number is even or odd.
  • The % 2 == 0 part is a math trick that asks Python if a number can be split perfectly into two groups.
  • If the number is even, the machine gives you a True answer; if it's odd, it gives you a False answer.

Code Example

even_or_odd = lambda n: "Even" if n % 2 == 0 else "Odd"

print(even_or_odd(4))
print(even_or_odd(7))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • This lambda quickly checks a number and decides if it is even or odd.
  • If the number can be divided by 2, it returns “Even”, otherwise it returns “Odd”.
  • The print() lines show the result for each number you test.

Code Example

# Rule: If the score is 50 or more, you Pass. Otherwise, you Fail.
grade = lambda score: "Pass" if score >= 50 else "Fail"

print(grade(75))
print(grade(30))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • This mini-tool works like a smart judge that looks at a score and makes a choice between two answers.
  • It uses the words if and else to decide which message to send back to you.
  • If the number is high enough, it says "Pass," but if it's too low, it automatically switches to say "Fail."

Code Example

# Rule: If the hour is before 18 (6 PM), it is Day. Otherwise, it is Night.
time_of_day = lambda hour: "Day" if hour < 18 else "Night"

print(time_of_day(10))
print(time_of_day(20))

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Python Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • This lambda function checks the hour and chooses between “Day” or “Night”.
  • If the hour is less than 18, it returns “Day”; otherwise it returns “Night”.
  • The print() lines show what the function returns for different times.
Lesson Progress: 0%
Lambda Functions
Lesson Incomplete
← Previous: Multiple Function Arguments
Lesson 13 of 34
Next: Closures →