Python Program Maximum of Three Number using Lambda
Last Updated :
09 Feb, 2024
Here we will create a lambda function to find the maximum among three numbers in Python.
Example:
Input: a=10, b=24, c=15
Output: 24 # using Lambda
Finding Maximum Among Three Numbers in Python
Below are some of the ways by which we can find the maximum among three numbers in Python.
- Using lambda with max() Function
- Using lambda with nested if-else block
- Using lambda with reduce() Function
- Using lambda with sorted() Function
Finding Maximum Among Three Numbers Using lambda with max() Function
The below code defines a lambda function find_max that takes three parameters and uses the max() function to determine the maximum among them. The result is then printed, demonstrating a concise approach for finding the maximum among three numbers using a lambda function with the max().
Python3
# using lambda with max() function
find_max = lambda a, b, c: max(a, b, c)
result = find_max(10, 5, 8)
print("Maximum Number :", result)
OutputMaximum Number : 10
Time Complexity: O(1)
Space complexity: O(1)
Finding Maximum Among Three Numbers Using lambda with if-else block
The below code defines a lambda function find_max that takes three parameters and uses an inline if-else block to find the maximum among them. It then applies this lambda function to three numbers (num1, num2, num3) and prints the result, indicating the maximum number.
Python3
# Define lambda function
find_max = lambda x, y, z: (x if (x > y and x > z) else y if y > z else z)
num1 = 14
num2 = 15
num3 = 16
# Find maximum using lambda function
result = find_max(num1, num2, num3)
#printing result
print("Maximum number is:", result)
OutputMaximum number is: 16
Time Complexity: O(1)
Space complexity: O(1)
Finding Maximum Among Three Numbers Using lambda with reduce() Function
The below approach code uses the reduce() function from the functools module along with a lambda function to iteratively compare pairs of numbers and find the maximum among three numbers (num1, num2, num3). The final result is then printed, indicating the maximum number.
Python3
from functools import reduce
num1 = 14
num2 = 18
num3 = 16
# reduce() with lambda to find maximum among three numbers
result = reduce(lambda x, y: x if x > y else y, [num1, num2, num3])
# print the result
print("Maximum number is:", result)
OutputMaximum number is: 18
Time Complexity: O(n)
Space complexity: O(1)
Finding Maximum Among Three Numbers Using lambda with sorted() Function
The below approach code defines a lambda function within big_in_three that uses the sorted() function to arrange the given arguments in ascending order and returns the maximum. It then applies this function to three numbers (num1, num2, num3) and prints the result, indicating the maximum number.
Python3
def big_in_three(*args):
return sorted(args, key=lambda x: x)[-1]
# Example usage
num1 = 10
num2 = 25
num3 = 15
result = big_in_three(num1, num2, num3)
print(f"Maximum Number: {result}")
Time Complexity: O(n log n)
Space Complexity: O(n)
Conclusion
In conclusion, lambda functions offer a simple and efficient way to find the maximum among three numbers in Python. The use of lambda with the max() function provides a straightforward and readable approach, while the lambda with the if-else block and reduce() function offers alternative methods. However, it's important to consider the time and space complexities of each approach. The lambda with max() function stands out for its simplicity and optimal time and space complexity, making it an elegant choice for this specific task.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read