You can use your own logic to compare each item of the list using for loop to get the largest number from a list in Python. Or use max() function in build function in Python.
Python program to get the largest number from a list
Simple example code has a list of numbers and the task is to write a Python program to find the largest number in the given list.
Without using built in functions
def max_num(lst): m = lst for a in lst: if a > m: m = a return m print(max_num([1, 2, -1, 0]))
Using max() method
list1 = [10, 20, 4, 45, -99] # printing the maximum element print("Largest element is:", max(list1))
Output: Largest element is: 45
Using sort method
Sort the list in ascending order and print the last element in the list.
list1 = [10, 20, 4, 45, 99] list1.sort() # printing the last element print(list1[-1])
Do comment if you have any doubts or suggestions on this Python program code.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.