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
Output of Python program | Set 17 Prerequisite - Tuples and Dictionaryin Python Predict the output of the following Python programs. 1.What is the output of the following program? Python numberGames = {} numberGames[(1,2,4)] = 8 numberGames[(4,2,1)] = 10 numberGames[(1,2)] = 12 sum = 0 for k in numberGames: sum += numberGames[k] pri
2 min read
Output of Python Programs | Set 20 (Tuples) Prerequisite: Tuples Note: The output of all these programs is tested on Python3 1. What will be the output of the following program? Python3 tuple = (1, 2, 3, 4) tuple.append( (5, 6, 7) ) print(len(tuple)) Options: 125Error Output: 4. ErrorExplanation: In this case an exception will be thrown as tu
2 min read
numpy.maximum() in Python numpy.maximum() function is used to find the element-wise maximum of array elements. It compares two arrays and returns a new array containing the element-wise maxima. If one of the elements being compared is a NaN, then that element is returned. If both elements are NaNs then the first is returned.
2 min read
numpy.maximum_sctype() function â Python numpy.maximum_sctype() function return the scalar type of highest precision of the same kind as the input. Syntax : numpy.maximum_sctype(t) Parameters : t : [dtype or dtype specifier] The input data type. This can be a dtype object or an object that is convertible to a dtype. Return : [dtype] The hi
1 min read
What is the maximum possible value of an integer in Python ? Consider below Python program. Python3 # A Python program to demonstrate that we can store # large numbers in Python x = 10000000000000000000000000000000000000000000 x = x + 1 print (x) Output : 10000000000000000000000000000000000000000001 In Python, value of an integer is not restricted by the numb
2 min read
Find Maximum of two numbers in Python Finding the maximum of two numbers in Python helps determine the larger of the two values. For example, given two numbers a = 7 and b = 3, you may want to extract the larger number, which in this case is 7. Let's explore different ways to do this efficiently.Using max()max() function is the most opt
2 min read