Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Python Program to find Smallest Number in a List

by suresh

In this article, we will show you, How to write a Python Program to find Smallest Number in a List with practical example. Before you start, please refer List article to understand everything about Lists.

Python Program to find Smallest Number in a List Example 1

The min function in python will return the minimum value in a List

# Python Program to find Smallest Number in a List 

a = [10, 50, 60, 80, 20, 15]

print("The Smallest Element in this List is : ", min(a))

OUTPUT

Python Program to find Smallest Number in a List 1

Python Program to find Smallest Number in a List Example 2

This python program is same as above but this time we are allowing user to enter the length of a List. Next, we used For Loop to add numbers to the list.

# Python Program to find 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))

OUTPUT

Python Program to find Smallest Number in a List 2

Python Program to find Smallest Number in a List Example 3

The Sort function in python will sort List elements in ascending order. Next, we are using the Index position 0 to print first element in a List

# Python Program to find Smallest Number in a List 

a = [10, 50, 60, 80, 20, 15]

a.sort()
print("The Smallest Element in this List is : ", a[0])

OUTPUT

Python Program to find Smallest Number in a List 3

Python Program to find Smallest Number in a List Example 4

This is same as above but this time we are allowing user to enter his own list items.

# Python Program to find 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)

NumList.sort()
print("The Smallest Element in this List is : ", NumList[0])

OUTPUT

Python Program to find Smallest Number in a List 4

Python Program to find Smallest Number in a List Example 5

In this program we are not using any built-in function such as sort, or min function

# Python Program to find 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)

smallest = NumList[0]    
for j in range(1, Number):
    if(smallest > NumList[j]):
        smallest = NumList[j]
        position = j

print("The Smallest Element in this List is : ", smallest)
print("The Index position of the Smallest Element is : ", position)

OUTPUT

Python Program to find Smallest Number in a List 5

ANALYSIS
From the above screenshot you can observe that, User inserted values are
NumList[5] = {223, 43, 22, 67, 54}
smallest = NumList[0] = 223

First Iteration – for 1 in range(1, 5) – Condition is true
So, it will start executing If statement inside the loop until the condition fails.
If (smallest > NumList[j]) inside the for loop is True because (223 > 43)
smallest = NumList[1]
smallest = 43
position = 1

Second Iteration: for 2 in range(1, 5) – Condition is true
If (smallest > NumList[2])  =  (43 > 22) – Condition True
smallest = NumList[2]
smallest = 22
Position = 2

Third Iteration: for 3 in range(1, 5) – Condition is true
If (smallest > NumList[3])  =  (22 > 67) – Condition False
smallest = 22
Position = 2

Fourth Iteration: for 4 in range(1, 5) – Condition is true
If (22 > 54) – Condition False
smallest = 22
Position = 2

Fifth Iteration: for 5 in range(1, 5) – Condition is False
So it will exit from the loop.

Placed Under: Python, Python Examples

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy