Find elements

Python program to find largest and smallest elements in a list

Python program to find largest and smallest elements in a list

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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago