How do you find the smallest number in a range in python?

Hello there! This article is for beginners who wish to understand the basic code for finding the smallest number in Python. So let’s begin.

We aim to find the smallest number in Python of all the numbers given in a list.

Say if the list is: [32, 54, 67, 21]

The output should be: 21

In this article, we will understand 3 different methods to do this.

1. Using Python min()

Min() is a built-in function in python that takes a list as an argument and returns the smallest number in the list. An example is given below-

#declaring a list
list1 = [-1, 65, 49, 13, -27] 
print ("list = ", list1)

#finding smallest number
s_num = min (list1)
print ("The smallest number in the given list is ", s_num)

Output:

list = [-1, 65, 49, 13, -27]
The smallest number in the given list is  -27

This is one of the simplest methods to find the smallest number. All you need to do is to pass the list to min() as an argument.

2. Using Python sort()

Sort()is another inbuilt method in python that doesn’t return the smallestnumber of the list. Instead, it sorts the list in ascending order.

So by sorting the list, we can access the first element of the list using indexing and that will be the smallest number in that list. Let’s see the code:

#declaring a list
list1 = [17, 53, 46, 8, 71]
print ("list = ", list1)

#sorting the list
list1.sort ()

#printing smallest number
print ("The smallest number in the given list is ", list1[0])

Output:

list =  [17, 53, 46, 8, 71]
The smallest number in the given list is 8

3. Using the ‘for’ loop

ls1 = []
total_ele = int (input (" How many elements you want to enter? "))

#getting list from the user
for i in range (total_ele):
  n =int (input ("Enter a number:"))
  ls1.append(n)
print (ls1)
min = ls1[0]

#finding smallest number
for i in range (len (ls1)):
  if ls1[i] < min:
    min = ls1[i]
print ("The smallest element is ", min)

In the above code, we are using two for loops, one for getting the elements of the list from the user and the second one for finding the smallest number from the list.

After getting the elements from the user, we define the first element of the list (at 0 index) as the smallest number (min). Then with the for loop, we compare each element of the list to the min and if any element is smaller than min, it becomes the new min.

This is how we get the smallest number from the user-given list.

The output for the above code is:

How many elements you want to enter? 4
Enter a number: 15
Enter a number: 47
Enter a number: 23
Enter a number: 6
[15, 47, 23, 6]
The smallest number is  6

Conclusion

So, these were some methods to find the smallest number from the given list in python. Hope you understood this! Feel free to ask questions below, if any. Thank you! 🙂

We are given a list of numbers and our task is to write a Python program to find the smallest number in given list. For the following program we can use various methods including the built-in min method, sorting the  array and returning the last element, etc.
Example: 

Input : list1 = [10, 20, 4]
Output : 4

Input : list2 = [20, 10, 20, 1, 100]
Output : 1

Sorting the list to find smallest number in a list

In Ascending order

Here writing a Python program where we are sorting the entire list and then returning the first element as it’ll be the smallest element present in the list.

Python3

list1 = [10, 20, 4, 45, 99]

list1.sort()

print("Smallest element is:", list1[0])

Output: 

smallest element is: 4

In Descending order

Here we are sorting using the sort() function the entire list and then returning the last element as it’ll be the smallest element present in the list.

Python3

list1 = [10, 20, 4, 45, 99]

list1.sort(reverse=True)

print("Smallest element is:", list1[-1])

Output:

smallest element is: 4

Using min() Method to find smallest number in a list

Here we are using the min Method and then returning the smallest element present in the list.

Python3

list1 = [10, 20, 1, 45, 99]

print("Smallest element is:", min(list1))

Output: 

Smallest element is: 1

Find minimum list element for a user defined list

Python3

list1 = []

num = int(input("Enter number of elements in list: "))

for i in range(1, num + 1):

    ele= int(input("Enter elements: "))

    list1.append(ele)

print("Smallest element is:", min(list1))

Output: 

Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 11
Enter elements: 99
Smallest element is: 11

Find the smallest element in list comparing every element

Python3

l=[ int(l) for l in input("List:").split(",")]

print("The list is ",l)

min1 = l[0]

for i in range(len(l)):

    if l[i] < min1:

        min1 = l[i]

print("The smallest element in the list is ",min1)

Input: 

List: 23,-1,45,22.6,78,100,-5

Output: 

The list is ['23', '-1', '45', '22.6', '78', '100','-5']
The smallest element in the list is  -5

Using the lambda function to find smallest number in a list

Here we are using the lambda function to print the smallest number present in the list.

Python3

lst = [20, 10, 20, 1, 100]

print(min(lst, key=lambda value: int(value)) )

Output:

1

Using the enumerate function to find smallest number in a list

Here we are iterating over the list using the enumerate() function and returning the last element.

Python3

lst = [20, 10, 20, 1, 100]

a,i = min((a,i) for (i,a) in enumerate(lst))

print(a)

Output:

1

How do you find the lowest number in a list in Python?

The Python min() function returns the lowest value in a list of items. min() can be used to find the smallest number in a list or first string that would appear in the list if the list were ordered alphabetically.

What does MIN () do in Python?

The min() function returns the item with the lowest value, or the item with the lowest value in an iterable. If the values are strings, an alphabetically comparison is done.

How do you find the small number in an array in Python?

Method 1 : Take a variable say mini to store the minimum element of the array. Set mini = arr[0] Run a loop over the array. Check if(arr[i]<mini) then set mini = arr[i]

How do I find the smallest number between two numbers in Python?

Python Number min() Method Python number method min() returns the smallest of its arguments: the value closest to negative infinity.