Write A Python Program To Get The Smallest Number From The List

Given a list of numbers as an input to the Python program the program should iterate all the numbers present in the list and find out the smallest number present in the list.

Once the program has found the smallest number, finally print the smaller number present in the list.

 

 

Write A Python Program To Get The Smallest Number From The List

 

 
Write A Python Program To Get The Smallest Number From The List

Let's take a variable named As length and using the import function call ask a user to enter the list length and then convert it into an integer.

Using an empty list of numbers literate from zero to list length  and  accept the user input using input function call,  append  all the user input to list of numbers
 
length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
  temp = int(input("Enter List Item : "))
  numbers.append(temp)




Set the first element of the list of numbers as the smallest using a variable.  use the for loop to ITErate all the items present in the list of numbers and compare it with the item if the smallest is greater than the item then set the smallest to the item.

smallest = numbers[0]
for item in numbers:
   if smallest > item:
       smallest = item



Once the for loop gets completed the smallest variable will store the smallest in the list of numbers and using the print function call print the variable smallest.

print("Smallest number present in list is : ", smallest)




Python code
===============

length = int(input("Enter List Length : "))
numbers = []
for _ in range(length):
  temp = int(input("Enter List Item : "))
  numbers.append(temp)

smallest = numbers[0]
for item in numbers:
   if smallest > item:
       smallest = item

print("Smallest number present in list is : ", smallest)




Output
=============
Enter List Length : 5
Enter List Item : 12
Enter List Item : 34
Enter List Item : 4
Enter List Item : 90
Enter List Item : 2
Smallest number present in list is :  2



Conclusion
================
Execute the above Python program by modifying the integer type conversion to a floating point use the floating point numbers again put to the above Python program and note down the results.


Comment it down below if you have any suggestions to improve the above Python program






Post a Comment

0 Comments