Largest number in list in python assignment expert

Largest Number in the List

You are given space-separated integers as input. Write a program to print the maximum number among the given numbers.

Input

The first line of input contains space-separated integers.

Explanation

In the example, the integers given are 

1, 0, 3, 2, 9, 8. The maximum number present among them is 9. So, the output should be 9.

Sample Input 1
1 0 3 2 9 8
Sample Output 1
9
Sample Input 2
-1 -3 -4 0
Sample Output 2
0

numbers = [int[i] for i in input[].split[]]
max = numbers[0]
for i in numbers[1:]:
    if max < i:
        max = i
print[max]

Learn more about our help with Assignments: Python

Largest Number in the List:

You are given space-separated integers as input. Write a program to print the maximum number among the given numbers.

Input:

The first line of input contains space-separated integers.

Explanation:

In the example, the integers given are

1, 0, 3, 2, 9, 8. The maximum number present among them is 9. So, the output should be 9.

Sample Input 1:

1 0 3 2 9 8

Sample Output 1:

9

Sample Input 2:

-1 -3 -4 0

Sample Output 2:

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 largest number in a list 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.

    What is the largest number in Python?

    It's usually 2^31 - 1 on a 32-bit platform and 2^63 - 1 on a 64-bit platform.

    How do you print a max number in Python?

    ALGORITHM:.
    STEP 1: Declare and initialize an array..
    STEP 2: Store first element in variable max..
    STEP 3: Loop through the array from 0 to length of the array and compare the value of max with elements of the array..
    STEP 4: If any element is greater than max, max will hold the value of that element..

    How do you find the largest number in a string in Python?

    max[] is an inbuilt function in Python programming language that returns the highest alphabetical character in a string..
    Syntax:.
    Parameter: max[] method takes a string as a parameter..
    Return value:.

    Chủ Đề