Checking Odd/Even Using Lambda

Write a Python program that uses a lambda function to check whether a given number is even or odd. The function should return ‘Even’ for even numbers and ‘Odd’ for odd numbers.

Example 1:

Input: 4 
Output: 'Even'

Example 2:

Input: 7 
Output: 'Odd'

Use a lambda function to create an anonymous function that checks whether a number is even or odd.

# Define the lambda function
check_even_odd = lambda x: 'Even' if x % 2 == 0 else 'Odd'

# Test the function
print(check_even_odd(4)) # Output: 'Even'
print(check_even_odd(7)) # Output: 'Odd'

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!