Python program to print largest and smallest number in list

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 : 78

Python 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:

Input : list1 = [10, 20, 4] Output : 4 Input : list2 = [20, 10, 20, 1, 100] Output : 1

Method 1 : Sort the list in ascending order and print the first element in the list.

Python3




# 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: 4

Method 2 : Using min[] method

Python3




# 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.

Python3




# 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: 11

Method 4: Find the smallest element in list.

Python3




# 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,-5

Output:

The list is ['23', '-1', '45', '22.6', '78', '100','-5'] The smallest element in the list is -5

Attention 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 List

Tutorialsrack 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[] functions

Program 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]]
Output

Enter 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[] function

Program 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]]
Output

Enter 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 function

Program 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]
Output

Enter 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 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
#pyton program to find smallest elements in a list #create a list of elements[number] number=[54,67,43,89,21,34] #sorting the list number.sort[] #print the last element in the list for largest print["largest elements is: ",number[5]] #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: 89 Smallest elements is: 21

Program 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

#pyton program to find largest smallest elements in a list #create a empty list of elements[number] number=[] #asking number of elements to add in the list n=int[input["Enter the number of elements in list: "]] for i in range[1, n + 1]: element=int[input["Enter elements: "]] number.append[element] #sort the list number.sort[] print["Largest element is:",number[n-1]] print["Smallest element is:",number[0]]

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: 34

This 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.34

Program 2

Takes input from the user

#pyton program to find largest smallest elements in a list #create a empty list of elements[number] number=[] #asking number of elements to add in the list n=int[input["Enter the number of elements in list: "]] for i in range[1, n + 1]: element=float[input["Enter elements: "]] number.append[element] #sort the list number.sort[] print["Largest element is:",number[n-1]] print["Smallest element is:",number[0]]

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.2

This 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
listNum=[] num=int[input["How many numbers in list: "]] for n in range[num]: numbers =int[input["Enter numbers: "]] listNum.append[numbers] print["Maximum elements in the list: ",max[listNum]] print["Minimum elements in the list: ",min[listNum]]

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: 23

Suggested 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

Video liên quan

Bài mới nhất

Chủ Đề