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. You can create a Python function to find the maximum of three numbers like this:

def find_max_of_three(num1, num2, num3):
    # Use the max() function to find the maximum of the three numbers
    max_num = max(num1, num2, num3)
    return max_num

# Example usage:
num1 = 10
num2 = 5
num3 = 20
maximum = find_max_of_three(num1, num2, num3)
print("The maximum of the three numbers is:", maximum)

In this code, we define a function find_max_of_three that takes three parameters (num1, num2, and num3). Inside the function, we use the max() function to find the maximum value among the three input numbers and then return that maximum value.

Python function to find the max of three numbers

A 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.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. Required fields are marked *