max and min in list in python

 Minimum And Maximum in List In Python



Given an unsorted List of integers number and To find maximum and minimum value from the List


Testcase1:

Input:[1,22,34,-5,222]

Ouput:

Maximum :222

Minimum:-5

Testcase2:

Input:[88,67,453,13,0,44,1]

Output:

Maximum:453

Minimum:0

This is least efficient approach but will get the work done.  Below’s implementation in Python.


by clicking to copy code:

"""
WAP to print maximum and minimum value in the list
"""
#let understand and intialise and program and use input() to input the list

lst=eval(input("Enter a number in the list:"))

#Now assume that first element of the list is maximum and minimum value
max,min=lst[0],lst[0]

#Take a loop to go upto first index value to ending value of the list
for i in range(1,len(lst)):
#Use if else to test the condition  if the condition is true then 
#Compare the value of assume number with second element of list if it greater then put value of i index value maximum
    if lst[i]>max:
        max=lst[i]
    elif lst[i]<min:

        
        min=lst[i]
#after test the condition use pass keyword in else part
    else:
        pass
#Now use print function to print maximum and minimum vale from the list
print("Maximum is :",max)
print("Minimum is:",min)
    



Input:

Enter a number in the list :[35,67,34,355,35,-22,-222]


Maximum value is:355

Minimum value is :-222


>>>You can do this program using in built function 

Copy this code it is done by using inbuilt function to find maximum and minimu value from the list:\


Testcase1:

Input:[22,11,23,133]

Output:

maximum :133

Minimum:11

"""
WAP to print maximum and minimum value in the list
"""
#let understand and intialise the program and use input() to input the list

lst=eval(input("Enter a number in the list:"))

#use inbuilt function that is max() and min() 

#max() function is used to find maximum value and min() function is used to find minimum value 
maximum=max(lst)
minimum=min(lst)        
        
#Now use print function to print maximum and minimum value from the list
print("Maximum  value is :",maximum)
print("Minimum  value is:",minimum)
    


Output:



Attention reader! Don’t stop learning now. Get hold of all the important Python and Collections concepts with the Fundamentals of Python and Java Collections Course at a student-friendly price and become industry ready. 


































No comments:

Powered by Blogger.