Python Program to Find Largest of 4 Numbers

The following python program uses the built-in function max() to find the largest of 4 numbers. The max function can take any number of arguments and hence can be used to find maximum of 3 or 5 numbers as well,

num1 = 10
num2 = 20
num3 = 30
num4 = 40

print(max(num1,num2,num3,num4)) # prints 40

The following python program accepts any number of numbers from the user and prints the maximum value. In this example we add all the user inputs to a list and then the list is passed to the max function to get the maximum value. The program assumes that to stop input of numbers user will enter zero.

numbers = []
while True:
    number = int(input("Please enter a number. Enter 0 to finish.: "))
    if number == 0:
        break
    numbers.append(number)

print("maximum of {} is {}".format(numbers,max(numbers)))

Here is a sample output from the above program,

Please enter a number. Enter 0 to finish.: 5
Please enter a number. Enter 0 to finish.: 30
Please enter a number. Enter 0 to finish.: 10
Please enter a number. Enter 0 to finish.: 0
maximum of [5, 30, 10] is 30