In this article, we will learn about python program to find largest among **n** Numbers with examples. We will see different built-in method to achieve this task.

## Getting Started

The task is to find the maximum number among **N** given numbers using python program. For example,

If **a = 4.1**, **b = 6.3**, **c = 6.7** and **d = 7.4**, then output should be **d = 7.4**

We can do it in below ways –

## Using if-else Condition

We can simply compare value using **if-else** and write python program to find largest among **N** numbers as shown below –

def findLargest(numbers): if not numbers: return None largest = numbers[0] for num in numbers: if num > largest: largest = num return largest # Take input from user n = int(input("Enter the number of elements: ")) elements = [] for i in range(n): element = float(input(f"Enter element {i + 1}: ")) elements.append(element) result = findLargest(elements) print("The largest number is:", result)

Here,

- Inside
**findLargest**function, we have use a python for loop. - At first, defined a variable
**largest**to store largest value. Initially, it is initialized with first element of list**numbers**. - Then, run a for loo. In each iteration, we are comparing if current number is greater than value stored in
**largest**variable. If yes, we are overriding the value in**largest**variable. If no, we move to next iteration. - Finally, value stored in
**largest**variable is returned and printed using print() function.

Output:

Enter the number of elements: 5 Enter element 1: 1.2 Enter element 2: 5.3 Enter element 3: 6.3 Enter element 4: 8.7 Enter element 5: 9.3 The largest number is: 9.3

## Using max() Function

We can also use **max()** function to find maximum number among **N** numbers in python as shown below –

# Take input from user n = int(input("Enter the number of elements: ")) elements = [] for i in range(n): element = float(input(f"Enter element {i + 1}: ")) elements.append(element) # Find maximum using max() function result = max(elements) print("The largest number is:", result)

Here,

- At first, we take N numbers of input using
**input()**method. Then, we convert it into list and store in elements variable. - Then, we use
**max()**function to return maximum element among these input numbers. - Finally, result is printed using
**print()**Function.

Output:

Enter the number of elements: 8 Enter element 1: 1.2 Enter element 2: 5.3 Enter element 3: -2.4 Enter element 4: 6.34 Enter element 5: 0.45 Enter element 6: 23.5 Enter element 7: 25.78 Enter element 8: 23.46 The largest number is: 25.78

## Using reduce() Function

We can also use **reduce()** function present inside **functools** module to write python program to find largest among **N** numbers as shown below –

from functools import reduce # Take input from user n = int(input("Enter the number of elements: ")) elements = [] for i in range(n): element = float(input(f"Enter element {i + 1}: ")) elements.append(element) # Find maximum using reduce() function result = reduce(lambda x, y: x if x > y else y, elements) print("The largest number is:", result)

Here,

- At first input is taken using
**input()**function and converted to list and stored in**elements**variable. - After that, we use
**reduce()**function to find the maximum element among these input numbers. - Finally, maximum number is returned by
**reduce()**function and stored in**result**variable. - Then, we print this maximum number using
**print()**function.

Output:

Enter the number of elements: 6 Enter element 1: 1.7 Enter element 2: 8.5 Enter element 3: 9.56 Enter element 4: 46.56 Enter element 5: 0.98 Enter element 6: 4.6 The largest number is: 46.56

## Using sorted() Function

We can also use **sorted()** function to write python program to find largest among **N** numbers as shown below –

from functools import reduce # Take input from user n = int(input("Enter the number of elements: ")) elements = [] for i in range(n): element = float(input(f"Enter element {i + 1}: ")) elements.append(element) # Find maximum using max() function result = sorted(elements)[-1] print("The largest number is:", result)

Here,

- We can
**N**numbers of elements from user, then covert them into list and store in**elements**variable. - After that, we use
**sorted()**function.**sorted()**function sorts the elements in ascending order. Then, we get last number in sorted order using**[-1]**indexing. - Finally, result is printed using
**print()**function.

Output:

Enter the number of elements: 8 Enter element 1: 1.2 Enter element 2: 7.5 Enter element 3: 4.3 Enter element 4: 6.8 Enter element 5: 9.8 Enter element 6: 34.2 Enter element 7: 5.4 Enter element 8: 65.76 The largest number is: 65.76

Thus, we can write python program to find largest among **N** numbers using **if-else**, built-in methods such as **reduce()**, **max()** and **sorted()** functions.

Learn about python at official Doc