Find the Maximum of two numbers in Python

Last Updated : 18 May, 2026

Given two numbers, the task is to find the greater number among them. For example:

Input: a = 10, b = 8
Output: 10

Let's explore different ways to find the larger of two numbers.

Using max()

max() function compares two or more values and returns the largest one. It performs the comparison internally and eliminates the need to write explicit conditional statements.

Python
a = 7
b = 3
print(max(a, b))

Output
7

Using ternary operator

Ternary conditional operator evaluates a condition and returns one of two values depending on whether the condition is true or false in a single expression.

Python
a = 7
b = 3
print(a if a > b else b)

Output
7

Explanation: if a > b else b checks if a is greater than b. If true, it returns a; otherwise, it returns b. In this case, since 7 > 3, it returns 7, which is then printed.

Using if-Else statement

if-else statement evaluates a condition and executes one of two code blocks based on whether the condition is true or false. It is used when the logic needs to be written explicitly in separate steps.

Python
a = 7
b = 3

if a > b:
    print(a)
else:
    print(b)

Output
7

Explanation: if statement checks whether a is greater than b. If true, it prints a; otherwise, it prints b.

Comment