How do you find the greatest of 4 numbers in python?

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

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a list of numbers, the task is to write a Python program to find the largest number in given list. 

    Examples:

    Input : list1 = [10, 20, 4]
    Output : 20
    
    Input : list2 = [20, 10, 20, 4, 100]
    Output : 100

    Method 1: Sort the list in ascending order and print the last element in the list. 

    Python3

    list1 = [10, 20, 4, 45, 99]

    list1.sort()

    print("Largest element is:", list1[-1])

    Output

    Largest element is: 99

    Method 2: Using max() method 

    Python3

    list1 = [10, 20, 4, 45, 99]

    print("Largest element is:", max(list1))

    Output

    Largest element is: 99

    Method 3: Find max list element on inputs provided by user 

    Python3

    list1 = []

    num = int(input("Enter number of elements in list: "))

    for i in range(1, num + 1):

        ele = int(input("Enter elements: "))

        list1.append(ele)

    print("Largest element is:", max(list1))

    Output:

    Enter number of elements in list: 4
    Enter elements: 12
    Enter elements: 19
    Enter elements: 1
    Enter elements: 99
    Largest element is: 99

    Method 4: Without using built-in functions in python: 

    Python3

    def myMax(list1):

        max = list1[0]

        for x in list1:

            if x > max:

                max = x

        return max

    list1 = [10, 20, 4, 45, 99]

    print("Largest element is:", myMax(list1))

    Output

    Largest element is: 99

    Method 5: Use the max() and def functions to find the largest element in a given list. The max() function prints the largest element in the list.  

    Python3

    def maxelement(lst):

        print(max(lst))

    lst = [20, 10, 20, 4, 100]

    maxelement(lst)

    Method: Using the lambda function

    Python3

    lst = [20, 10, 20, 4, 100]

    print(max(lst, key=lambda value: int(value)) )

    Method: Using reduce function

    Python3

    from functools import reduce

    lst = [20, 10, 20, 4, 100]

    largest_elem = reduce(max, lst)

    print(largest_elem)

    Time Complexity: O(n)

    Auxiliary Space: O(1)


    How do you find the greatest number with 4 numbers?

    Algorithm.
    START..
    INPUT FOUR NUMBERS A, B, C, D..
    IF A > B THEN. IF A > C THEN. IF A > D THEN. A IS THE GREATEST. ELSE. D IS THE GREATEST..
    ELSE IF B > C THEN. IF B > D THEN. B IS THE GREATEST. ELSE. D IS THE GREATEST..
    ELSE IF C > D THEN. C IS THE GREATEST..
    ELSE. D IS THE GREATEST..

    How do you find the greatest number in Python?

    In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.

    How do you find the greatest 3 numbers in Python?

    Source Code:.
    # Python program to find the largest number among the three input numbers..
    # take three numbers from user..
    num1 = float(input("Enter first number: ")).
    num2 = float(input("Enter second number: ")).
    num3 = float(input("Enter third number: ")).
    if (num1 > num2) and (num1 > num3):.
    largest = num1..

    How do you find the max of 5 numbers in Python?

    “python how to find largest out of 5 numbers” Code Answer.
    def highest_even(li):.
    evens = [].
    for item in li:.
    if item % 2 == 0:.
    evens. append(item).
    return max(evens).
    print(highest_even([10,2,3,4,8,11])).