Squaring a Number Using Lambda

Tags: Lambda, Square

Write a Python program that uses a lambda function to square a given number. The function should return the squared number.

Example 1:

Input: 5 
Output: 25

Example 2:

Input: 10 
Output: 100

Use a lambda function to create an anonymous function that squares a number.

# Define the lambda function
square = lambda x: x ** 2

# Test the function
print(square(5)) # Output: 25
print(square(10)) # Output: 100

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!