0% found this document useful (0 votes)
14 views6 pages

Lambda Function Examples

The document provides a comprehensive list of lambda function examples in Python, covering various operations such as addition, string manipulation, and list processing. Each example includes a brief explanation and output for clarity. Additionally, a summary table at the end consolidates the functions, their purposes, and example outputs.

Uploaded by

Jawad Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Lambda Function Examples

The document provides a comprehensive list of lambda function examples in Python, covering various operations such as addition, string manipulation, and list processing. Each example includes a brief explanation and output for clarity. Additionally, a summary table at the end consolidates the functions, their purposes, and example outputs.

Uploaded by

Jawad Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Table of Contents

Lambda Function examples in python.............................................................................................1


1. Add Two Numbers.........................................................................................................................1
2. Find Maximum of Two Numbers.................................................................................................1
3. Square of a Number.......................................................................................................................1
4. Check Even or Odd.......................................................................................................................1
5. Calculate Cube of a Number.........................................................................................................1
6. Find the Length of a String...........................................................................................................2
7. Multiply Two Numbers.................................................................................................................2
8. Find Minimum of Two Numbers..................................................................................................2
10. Reverse a String...........................................................................................................................3
11. Convert Celsius to Fahrenheit....................................................................................................3
12. Filter Even Numbers from a List................................................................................................3
13. Double Each Element in a List....................................................................................................3
14. Sort a List of Tuples by Second Element...................................................................................3
15. Check if a String is Palindrome..................................................................................................4
16. Find Sum of Digits of a Number.................................................................................................4
18. Check Leap Year.........................................................................................................................4
19. Extract First Letters from a List of Words................................................................................5
20. Sort Strings by Their Length......................................................................................................5
Summary Table.....................................................................................................................................5
Lambda Function examples in python
1. Add Two Numbers
add = lambda x, y: x + y
print(add(10, 20)) # Output: 30

Explanation: Adds two numbers using a lambda function.

Output: 30

2. Find Maximum of Two Numbers


maximum = lambda x, y: x if x > y else y
print(maximum(15, 25)) # Output: 25

Explanation: Compares two numbers and returns the greater one.

Output: 25

3. Square of a Number
square = lambda x: x ** 2
print(square(7)) # Output: 49

Explanation: Returns the square of a given number.

Output: 49

4. Check Even or Odd


is_even = lambda x: "Even" if x % 2 == 0 else "Odd"
print(is_even(9)) # Output: Odd

Explanation: Checks whether a number is even or odd using a conditional expression.

Output: Odd

5. Calculate Cube of a Number


cube = lambda x: x ** 3
print(cube(4)) # Output: 64

Explanation: Returns the cube of a number.

Output: 64
6. Find the Length of a String
length = lambda s: len(s)
print(length("Python")) # Output: 6

Explanation: Returns the number of characters in a string.

Output: 6

7. Multiply Two Numbers


multiply = lambda x, y: x * y
print(multiply(3, 5)) # Output: 15

Explanation: Multiplies two numbers.

Output: 15

8. Find Minimum of Two Numbers


minimum = lambda x, y: x if x < y else y
print(minimum(20, 15)) # Output: 15

Explanation: Returns the smaller of the two numbers.

Output: 15

9. Concatenate Two Strings


concat = lambda s1, s2: s1 + s2
print(concat("Hello, ", "World!")) # Output: Hello, World!

Explanation: Joins two strings together.

Output: Hello, World!

10. Reverse a String


reverse = lambda s: s[::-1]
print(reverse("Python")) # Output: nohtyP

Explanation: Reverses the given string using slicing.

Output: nohtyP
Advanced Examples
11. Convert Celsius to Fahrenheit
c_to_f = lambda c: (c * 9/5) + 32
print(c_to_f(25)) # Output: 77.0

Explanation: Converts Celsius temperature to Fahrenheit.

Output: 77.0

12. Filter Even Numbers from a List


numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers) # Output: [2, 4, 6]

Explanation: Uses filter() with a lambda to extract even numbers.

Output: [2, 4, 6]

13. Double Each Element in a List


numbers = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, numbers))
print(doubled) # Output: [2, 4, 6, 8]

Explanation: Uses map() with a lambda to double each number.

Output: [2, 4, 6, 8]

14. Sort a List of Tuples by Second Element


tuples = [(1, 3), (2, 1), (4, 2)]
sorted_tuples = sorted(tuples, key=lambda x: x[1])
print(sorted_tuples) # Output: [(2, 1), (4, 2), (1, 3)]

Explanation: Sorts tuples based on their second value.

Output: [(2, 1), (4, 2), (1, 3)]


15. Check if a String is Palindrome
is_palindrome = lambda s: s == s[::-1]
print(is_palindrome("madam")) # Output: True

Explanation: Checks whether the given string reads the same forwards and backwards.

Output: True

16. Find Sum of Digits of a Number


sum_of_digits = lambda num: sum(map(int, str(num)))
print(sum_of_digits(123)) # Output: 6

Explanation: Converts the number to a string, maps digits to integers, and sums them.

Output: 6

17. Find Factorial of a Number


from functools import reduce
factorial = lambda n: reduce(lambda x, y: x * y, range(1, n + 1))
print(factorial(5)) # Output: 120

Explanation: Uses reduce() and lambda to calculate factorial of a number.

Output: 120

18. Check Leap Year


is_leap = lambda year: year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
print(is_leap(2024)) # Output: True

Explanation: Determines if a year is a leap year based on leap year rules.

Output: True

19. Extract First Letters from a List of Words


words = ["apple", "banana", "cherry"]
first_letters = list(map(lambda x: x[0], words))
print(first_letters) # Output: ['a', 'b', 'c']
Explanation: Extracts the first character from each word in a list.

Output: ['a', 'b', 'c']

20. Sort Strings by Their Length


strings = ["apple", "pear", "banana", "kiwi"]
sorted_strings = sorted(strings, key=lambda x: len(x))
print(sorted_strings) # Output: ['pear', 'kiwi', 'apple', 'banana']

Explanation: Sorts strings in ascending order based on their length.

Output: ['pear', 'kiwi', 'apple', 'banana']

Summary Table
No. Function Purpose Example Output
1 Add Two Numbers Addition 30
2 Maximum Compare two values 25
3 Square Power operation 49
4 Even/Odd Conditional check Odd
5 Cube Power 3 64
6 String Length Length calculation 6
7 Multiply Multiplication 15
8 Minimum Compare values 15
9 Concatenate Join strings Hello, World!
10 Reverse String slicing nohtyP
11 Celsius→Fahrenheit Conversion 77.0
12 Filter Evens filter() usage [2,4,6]
13 Double List map() usage [2,4,6,8]
14 Sort Tuples Custom key [(2,1),(4,2),(1,3)]
15 Palindrome Check equality True
16 Sum of Digits Digit manipulation 6
17 Factorial reduce() usage 120
18 Leap Year Boolean logic True
19 First Letters Extract initials ['a','b','c']
20 Sort by Length Custom key sort ['pear','kiwi','apple','banana']

You might also like