Python Program to find the Largest and Smallest Number in a List Example 1
Thispython programallows user to enter the length of a List. Next, we usedFor Loopto add numbers to the list. Here, the minand max functions in Pythonreturns the smallest and largest numbers or minimum and maximum values in a List.
# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int[input["Please enter the Total Number of List Elements: "]] for i in range[1, Number + 1]: value = int[input["Please enter the Value of %d Element : " %i]] NumList.append[value] print["The Smallest Element in this List is : ", min[NumList]] print["The Largest Element in this List is : ", max[NumList]]Python largest and smallest list number output
Please enter the Total Number of List Elements: 5 Please enter the Value of 1 Element : 50 Please enter the Value of 2 Element : 45 Please enter the Value of 3 Element : 33 Please enter the Value of 4 Element : 78 Please enter the Value of 5 Element : 66 The Smallest Element in this List is : 33 The Largest Element in this List is : 78Python program to find smallest number in a list
Given a list of numbers, the task is to write a Python program to find the smallest number in given list.
Examples:
Method 1 : Sort the list in ascending order and print the first element in the list.
# Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort[]
# printing the first element
print["Smallest element is:", *list1[:1]]
|
Output:
smallest element is: 4Method 2 : Using min[] method
# Python program to find smallest
# number in a list
# list of numbers
list1 = [10, 20, 1, 45, 99]
# printing the maximum element
print["Smallest element is:", min[list1]]
|
Output:
Smallest element is: 1
Method 3 : Find min list element on inputs provided by user.
# Python program to find smallest
# number in a list
# creating empty list
list1 = []
# asking number of elements to put in list
num = int[input["Enter number of elements in list: "]]
# iterating till num to append elements in list
for i in range[1, num + 1]:
ele= int[input["Enter elements: "]]
list1.append[ele]
# print maximum element
print["Smallest element is:", min[list1]]
|
Output:
Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 11 Enter elements: 99 Smallest element is: 11Method 4: Find the smallest element in list.
# Python program to find smallest
# number in a list
l=[ int[l] for l in input["List:"].split[","]]
print["The list is ",l]
# Assign first element as a minimum.
min1 = l[0]
for i in range[len[l]]:
# If the other element is min than first element
if l[i] < min1:
min1 = l[i] #It will change
print["The smallest element in the list is ",min1]
|
Input:
List: 23,-1,45,22.6,78,100,-5Output:
The list is ['23', '-1', '45', '22.6', '78', '100','-5'] The smallest element in the list is -5Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python program to find largest and smallest elements in a list
Python program to find largest and smallest elements in a list
Contents
- 1 Python program to find largest and smallest elements in a list
- 1.1 To find the largest and smallest number in an integer list in Python
- 1.2 To find the largest and smallest number in a float list in Python
- 1.3 find the largest and smallest number in the list using max[] and min[] function
- 1.4 Related posts:
In this tutorial, we will discuss the concept of Python program to find largest and smallest elements in the list
In this post, we will learn how to find the largest and smallest elements of a list in Python programming language
Python program to find largest and smallest elements in a list
To find the largest and smallest number in an integer list in Python
Program 1
In this programs, we can see step by step approach for completion of the program.
- Create a number list in Python
- Sort the list in ascending order
- Display last elements in the list for largest
- Display first elements in the list for smallest
When the above code is executed, it produces the following results
largest elements is: 89 Smallest elements is: 21Program 2
Takes input from the user
In this programs, we can see step by step approach for completion of the program.
- Create an empty list in Python
- Takes the input to add the number of elements in the list from the user
- Takes the input elements of the list one by one from the user
- read each number and append each number to the list using for loop
- sort the list elements in ascending order
- Displays last and first elements for largest and smallest
When the above code is executed, it produces the following results
Enter the number of elements in list: 4 Enter elements: 34 Enter elements: 67 Enter elements: 87 Enter elements: 65 Largest element is: 87 Smallest element is: 34This program gets “n” number of elements and Enter the elements of the list as input from the user. then, this program finds and displays the largest smallest elements from the list
To find the largest and smallest number in a float list in Python
Program 1
#pyton program to find smallest elements in a list #create a list of elements[number] number=[54.5,67.7,34.43,65.89,21.34] #sorting te list number.sort[] #print the last element in the list for lrgest print["largest elements is: ",number[4]] #print the first element in the list for smallest print["Smallest elements is: ",number[0]]When the above code is executed, it produces the following results
largest elements is: 67.7 Smallest elements is: 21.34Program 2
Takes input from the user
When the above code is executed, it produces the following results
Enter the number of elements in list: 5 Enter elements: 34.2 Enter elements: 56.43 Enter elements: 67.89 Enter elements: 87.65 Enter elements: 48.7 Largest element is: 87.65 Smallest element is: 34.2This program gets “n” number of elements and Enter the elements of the list as input from the user. then, this program finds and displays the largest smallest elements from the list.
find the largest and smallest number in the list using max[] and min[] function
In this programs, we can see step by step approach for completion of the program.
- Create an empty list in Python
- Takes the input to add the number of elements in the list from the user
- Takes the input elements of the list one by one from the user
- read each number and append each number to the list using for loop
- predefined max[] function use to find maximum value
- predefined min[] function use to find minimum value
- Displays last and first elements for largest and smallest
When the above code is executed, it produces the following results
How many numbers in list: 5 Enter numbers: 36 Enter numbers: 76 Enter numbers: 98 Enter numbers: 23 Enter numbers: 67 Maximum elements in the list: 98 Minimum elements in the list: 23Suggested for you
Operator in Python
For loop in Python