Python program to print largest and smallest number in list
Python Program to find the Largest and Smallest Number in a List Example 1Thispython 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. Show
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 listGiven a list of numbers, the task is to write a Python program to find the smallest number in given list. Method 1 : Sort the list in ascending order and print the first element in the list. Python3
Output: smallest element is: 4Method 2 : Using min() method Python3
Output: Smallest element is: 1 Method 3 : Find min list element on inputs provided by user. Python3
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. Python3
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
Article Tags :
Python Python Programs
Python list-programs python-list Practice Tags :
python-list
Read Full Article
Program source code# Python Program to find Largest and Smallest Number in a List
# Creating list
nlist = []
# Reading numbers from user
n = int(input("Enter the Total Number of List Elements: "))
# Using for loop to add numbers in list
for i in range(1, n + 1):
value = int(input("Enter the Value of %d Element : " %i))
nlist.append(value)
# Sort list elements
nlist.sort()
# Displaying smallest and largest element in list
print("The Smallest Element in this List is : ", nlist[0])
print("The Largest Element in this List is : ", nlist[n - 1])
Python Program to Find the Largest and Smallest Number in a ListTutorialsrack 01/05/2020 Python In this Python program, we will learn how to find the largest and smallest number in a given list. In this program, we will use python built-in functions such as max(), min(), sort(), or without using any built-in function to find the largest and smallest number in a given list. Here is the source code of the program to find the largest and smallest number in a given list. Program 1: Using min() and max() functionsProgram 1: Using min() and max() functions# Python Program to find the Largest and Smallest Number in a List using max() and min() function
NumList = []
# Take the input from user
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList: ",NumList)
print("\nThe Smallest Element in this List is: ", min(NumList))
print("The Largest Element in this List is: ", max(NumList))
OutputEnter the Total Number of List Elements: 6 Enter the Value of 1 Element: 62 Enter the Value of 2 Element: 641 Enter the Value of 3 Element: 8451 Enter the Value of 4 Element: 1233 Enter the Value of 5 Element: 4613 Enter the Value of 6 Element: 852 List: [62, 641, 8451, 1233, 4613, 852] The Smallest Element in this List is: 62 The Largest Element in this List is: 8451 Program 2: using sort() functionProgram 2: using sort() function# Python Program to find the Largest and Smallest Number in a List using sort() function
NumList = []
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element : " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList: ",NumList)
NumList.sort()
print("\nThe Smallest Element in this List is : ", NumList[0])
print("The Largest Element in this List is : ", NumList[Number - 1])
OutputEnter the Total Number of List Elements: 6 Enter the Value of 1 Element : 123 Enter the Value of 2 Element : 523 Enter the Value of 3 Element : 4562 Enter the Value of 4 Element : 665 Enter the Value of 5 Element : 265 Enter the Value of 6 Element : 452 List: [123, 523, 4562, 665, 265, 452] The Smallest Element in this List is : 123 The Largest Element in this List is : 4562 Program 3: Without using any built-in functionProgram 3: Without using any built-in function# Python Program to find Largest and Smallest Number in a List Without Using any Built-in Function
NumList = []
# Take the input from the User
Number = int(input("Enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Enter the Value of %d Element: " %i))
NumList.append(value)
# Print the List Entered by the User
print("\nList: ",NumList)
smallest = largest = NumList[0]
for j in range(1, Number):
if(smallest > NumList[j]):
smallest = NumList[j]
min_position = j
if(largest < NumList[j]):
largest = NumList[j]
max_position = j
print("The Smallest Element in this List is: ", smallest)
print("The Index position of Smallest Element in this List is: ", min_position)
print("The Largest Element in this List is: ", largest)
print("The Index position of Largest Element in this List is: ", max_position)
OutputEnter the Total Number of List Elements: 6 Enter the Value of 1 Element: 456 Enter the Value of 2 Element: 123 Enter the Value of 3 Element: 523 Enter the Value of 4 Element: 458 Enter the Value of 5 Element: 689 Enter the Value of 6 Element: 475 List: [456, 123, 523, 458, 689, 475] The Smallest Element in this List is: 123 The Index position of Smallest Element in this List is: 1 The Largest Element in this List is: 689 The Index position of Largest Element in this List is: 4 Python program to find largest and smallest elements in a listPython program to find largest and smallest elements in a listContents
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 PythonProgram 1 In this programs, we can see step by step approach for completion of the program.
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.
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 PythonProgram 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() functionIn this programs, we can see step by step approach for completion of the program.
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 Python program to find Average of numbers in a list Python program to find sum of elements in a list Related posts:Program to print even or odd numbers in given range using recursion Cpp program to find factorial using function Java Program to smallest among three numbers Calculate average of odd and even numbers in Java |