Javaexercise.com

Python Program To Find Smallest Number In The Python List?

In this article, we are going to see how we can find the smallest number present in the list. Let's understand the topic with the help of some sample data.

For Examples:

Input: [22, 53, 75, 64, 24]
Output: 22
​
Input: [54, 23, 18, 83, 66]
Output: 18

The different ways to find out the smallest element present in the list are:

  1. Using sort() method

  2. Using min() method

  3. Without using built-in functions

Find the smallest number in the list Using the sort() method in Python

In this approach, we sort the list of numbers in ascending order with the help of the sort() method and then we print the first element present in the list.

Algorithm

Step 1: Create a list of numbers

Step 2: Use the sort() method to sort the list in ascending order

Step 3: Print the first element of the list

See the below Python code implementation of the above algorithm.

# Python program to find the smallest
# number in a list using sort()
​
# list of the numbers
list1 = [54, 23, 18, 83, 66]
​
# sorting the list
list1.sort()
​
# printing the last element
print("The smallest element is:", list1[0])

Output:

The smallest element is: 18

Find the smallest number in the list Using the min() method in Python

In this method, we will use the built-in function min() which will find out the smallest number present in the given list and return that element.

Algorithm

Step 1: Create a list of numbers

Step 2: Use min() method to get smallest number

Step 3: Print that smallest number

See the below Python code implementation of the above algorithm.

# Python program to find the smallest
# number in a list using min()
​
# list of the numbers
list2 = [54, 23, 18, 83, 66] 
​
# printing the smallest element
print("The smallest element is:", min(list2))

Output:

The smallest element is: 18

Find the smallest number in the list Using for loop in Python

In this approach, we will find out the smallest element present in the list using a for loop.

Algorithm

Step 1: Define a function that will find the smallest number

Step 2: Declare the variable that will store the smallest number

Step 3: Set the value of the variable to the first element of the list

Step 4: Start a for loop

Step 5: Compare each element with the variable which stores the largest value

Step 6: If the element is smaller than the value in the variable then set that element as the value of the variable

Step 7: Return the result

Step 9: Print the result

See the below Python code implementation of the above algorithm.

# Python program to find smallest
# number in a list without using built-in functions
​
def myLar(list1):
​
    # Assume the first number in the list is smallest
    # initially and assign it to variable "small"
    small = list1[0]
​
    # Now traverse through the list and compare
    # each number with "small" value. Whichever is
    # smallest assign that value to "small'.
    for x in list1:
        if x < small:
            small = x
​
    # after complete traversing the list
    # return the "small" value
    return small
​
​
# Driver code
list1 = [54, 23, 18, 83, 66]
print("The smallest element is:", myLar(list1))

Output:

The smallest element is: 18