Site icon Codeforcoding

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.

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

 

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

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
Exit mobile version