Home » Python program to find the largest and smallest number in a list

Python program to find the largest and smallest number in a list

Python program to find the largest and smallest number in a list

Hello everybody, welcome back to programminginpython.com. Here in this post, I am going to discuss a Python program that finds out the smallest and largest number on the list.

Here we use 2 predefined functions min() and max() which check for the smallest and largest number in a list respectively.

Master the basics of data analysis in Python. Expand your skillset by learning scientific computing with numpy.

Take the course on Introduction to Python on DataCamp here https://bit.ly/datacamp-intro-to-python

 

You can watch the video on YouTube here

Program on GitHub

Largest and Smallest in a list – Code Visualization

Task :

To find the largest and smallest number in a list.

Approach :

  • Read the input number asking for the length of the list using input() or raw_input().
  • Initialize an empty list lst = [].
  • Read each number using a for loop.
  • In the for loop append each item to the list, lst.append().
  • Now we use a predefined function max() to find the largest element in a list.
  • Similarly, we use another predefined function min() to find the smallest element in a list.

Program on GitHub

Program :

lst = []
num = int(input('How many numbers: '))

for n in range(num):
    numbers = int(input('Enter number '))
    lst.append(numbers)

print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :", min(lst))

Output :

Smallest and Largest element in a List
The smallest and Largest element in a List

Program on GitHub

That is for the post, here I have found the largest and the smallest element in a given list, using max() and min() built-in functions of the list which give us the maximum and minimum element from a given list.

Course Suggestion

Want to learn Python with strong fundamentals? If yes, I strongly suggest you take the course below.
Course: Fundamentals of Programming in Python

 

Online Python Compiler

Leave a Reply

Your email address will not be published. Required fields are marked *