codescracker
python

Python Program to Find Smallest Number in a List



« Previous Program Next Program »

This article is created to cover some programs in Python, that find and prints smallest number (element) in a list entered by user. Here are the list of programs covered in this article:

Find Smallest Number in List of 10 Numbers

The question is, write a Python program to find smallest number in a list without using any predefined method. Here is its answer:

nums = []
print("Enter 10 Elements (Numbers) for List: ")
for i in range(10):
  nums.append(int(input()))
small = nums[0]
for i in range(10):
  if small>nums[i]:
    small = nums[i]
print("\nSmallest Number is: ")
print(small)

Here is the initial output of this program's sample run:

python find smallest number in list

Now supply the input say 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 as ten numbers to find and print the smallest from the given list of numbers as shown in the snapshot of sample's run given below:

find smallest number in list python

Find Smallest Number in List of n Numbers

This is the modified version of previous program. This program allows user to define the size of list before entering elements for list. The end used in this program to skip insertion of an automatic newline. The str() converts any type of value to a string type.

nums = []
print(end="Enter the Size: ")
numsSize = int(input())
print(end="Enter " +str(numsSize)+ " Numbers: ")
for i in range(numsSize):
  nums.append(int(input()))
small = nums[0]
for i in range(numsSize):
  if small>nums[i]:
    small = nums[i]
print("\nSmallest Number = " + str(small))

Here is its sample run with user input, 5 as size and 5, 4, 3, 1, 2 as five elements:

python program smallest number in list

Find Smallest Number in List using min()

The question is, write a Python program to find smallest number in list using min(). Here is its answer:

nums = list()
print(end="Enter the Size: ")
numsSize = int(input())
print(end="Enter " +str(numsSize)+ " Numbers: ")
for i in range(numsSize):
  nums.append(int(input()))

print("\nSmallest Number = " + str(min(nums)))

Find Smallest Number in List using sort()

This program uses sort() method to sort the list in ascending order (by default) and then by using list indexing, I've printed the smallest number in the given list like shown in the program given below:

nums = list()
print(end="Enter the Size: ")
numsSize = int(input())
print(end="Enter " +str(numsSize)+ " Numbers: ")
for i in range(numsSize):
  nums.append(int(input()))
nums.sort()
print("\nSmallest Number = " + str(nums[0]))

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker