Python program maximum of three
Last Updated :
03 May, 2025
The task of finding the maximum of three numbers in Python involves comparing three input values and determining the largest among them using various techniques. For example, if a = 10, b = 14, and c = 12, the result will be 14.
Using max()
max() is the straightforward approach to find the maximum of three numbers . It is a built-in function that is specifically designed to compare multiple values and return the largest one. This method is highly recommended as it is simple to use, reduces code complexity and is optimal for finding the largest number from a small set like three values.
Python
a = 10
b = 14
c = 12
res = max(a, b, c)
print(res)
Explanation: max() compare the values of a, b and c. It evaluates all three numbers and returns the largest value among them.
Using ternary conditional operator
ternary conditional operator is a more compact approach that allows us to find the maximum of three numbers in a single line. It evaluates conditions in a nested format, making the code concise. Although it can sometimes reduce readability for beginners, it is an efficient way to compare values quickly when we prefer minimal lines of code.
Python
a = 10
b = 14
c = 12
res = a if (a >= b and a >= c) else (b if b >= c else c)
print(res)
Explanation: ternary conditional expression determines the largest value among a, b and c by first checking if a is greater than or equal to both b and c. If true, a is assigned to res otherwise, it compares b and c, assigning the larger one to res.
Using sorted()
sorted() can be used to find the maximum by sorting the three numbers in descending order and selecting the first element. While this approach is readable, it is not considered the best choice for only three numbers. Sorting is generally more suitable for larger collections and for just three values, it introduces unnecessary operations.
Python
a= 10
b = 14
c = 12
res = sorted([a, b, c], reverse=True)[0]
print(res)
Explanation: sorted() with reverse=True sorts the list [a, b, c] in descending order . The largest number will be at index 0 after sorting in descending order. The value at this position is assigned to the variable res.
Using heapq.nlargest
heapq.nlargest() from the heapq module is another way to find the maximum number from a list. It is mainly useful when working with large datasets where finding the top elements is needed. However, for the simple task of finding the maximum of three numbers, this approach is excessive and not as intuitive as other methods.
Python
import heapq
a = 10
b = 14
c = 12
res = heapq.nlargest(1, [a, b, c])[0]
print(res)
Explanation: heapq.nlargest() find the largest element from the list [a, b, c]. It returns the top 1 largest value as a list, so [0] is used to extract the element and result is assigned to res.
Related Articles:
Similar Reads
Python Program Maximum Of Four Python, a versatile and widely used programming language, offers multiple ways to find the maximum value among four given numbers. Whether you're a beginner or an experienced developer, understanding different approaches can enhance your problem-solving skills. In this article, we will explore simpl
2 min read
Python Program Maximum of Three Number using Lambda Here we will create a lambda function to find the maximum among three numbers in Python. Example: Input: a=10, b=24, c=15Output: 24 # using LambdaFinding Maximum Among Three Numbers in PythonBelow are some of the ways by which we can find the maximum among three numbers in Python. Using lambda with
3 min read
Python - Row with Maximum Product We can have an application for finding the lists with the maximum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development. Method #1 : Using reduce() + lambda The
4 min read
Python Program for Number of pairs with maximum sum Write a python program for a given array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j. Example: Input : arr[] = {1, 1, 1, 2, 2, 2}Output: 3Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the
3 min read
Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read