Skip to content

Write a Python function to find the max of three numbers | Example code

  • by

You have to use if-elif to write a Python function to find the max of three numbers.

Python function to find the max of three numbers

Simple example code finds the largest number among the three numbers.

def maximum(a, b, c):
    if (a >= b) and (a >= c):
        largest = a

    elif (b >= a) and (b >= c):
        largest = b
    else:
        largest = c

    return largest


res = maximum(2, 4, 7)
print("Largest Number: ", res)

Output:

Write a Python function to find the max of three numbers

Another example using 2 function

def max_of_two(x, y):
    if x > y:
        return x
    return y


def max_of_three(x, y, z):
    return max_of_two(x, max_of_two(y, z))


print(max_of_three(3, 6, -5))

Output: 6

User input numbers

n1 = int(input("Enter first number: "));

n2 = int(input("Enter second number: "));

n3 = int(input("Enter Third number: "));


def f_max():
    if (n1 >= n2) and (n1 >= n3):
        l = n1

    elif (n2 >= n1) and (n2 >= n3):
        l = n2
    else:
        l = n3
    print("A Largest number among the three is", l)


f_max()

Output:

Enter first number: 5
Enter second number: 15
Enter Third number: 10
A largest number among the three is 15

Do comment if you have any doubts or suggestions on this Python basic program.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.