Hello everybody, welcome back toprogramminginpython.com. Here in this post, I am going to discuss a Python program that finds out the smallest and largest number on thelist.
Here we use 2 predefined functions min() and max() which check for the smallest and largest number in alistrespectively.
Master the basics of data analysis in Python. Expand your skillset by learning scientific computing with numpy.
Take the course on Introduction to Python onDataCamp herehttps://bit.ly/datacamp-intro-to-python
You can watch the video on YouTubehere
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()
orraw_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 :
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 :
That is for the post, here I have found the largest and the smallest element in a given list, usingmax()
andmin()
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