Using any one method either max() function or for loop, you can write a program to find the largest of n numbers in Python.
In this tutorial, we will use only for loop to write a program to find the largest of n numbers in Python.
Python program to find the largest of n numbers using for loop
A simple example code takes an input number for the length of the list using the Python input() function. Using a list to store numbers.
We create a custom function to find max.
def find_max(list):
max = list[0]
for a in list:
if a > max:
max = a
return max
num = int(input('How many numbers: '))
lst = []
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum Number is :", find_max(lst))
Output:
Or just use the below line
print("Maximum element in the list is :", max(lst))
Do comment if you have any doubts or suggestions on this Python 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.