Write a Python program to get the smallest number from a list | Example code

  • by

Using the min() function, you can easily write a Python program to get the smallest number from a list. You can also do list sorting and get the first element in the list.

Python program to get the smallest number from a list

Simple example code gets the smallest number from a list.

list1 = [10, 20, 100, 40, 90]

res = min(list1)

print("Smallest number is:", res)

Output:

Write a Python program to get the smallest number from a list

Another way by doing sorting the list

Sort the list in ascending order and the first element in the list will be the smallest number. The list index starts from 0, so the first element will be list[0].

list1 = [10, 20, 100, 40, 90]

list1.sort()

print(list1[0])

Output: 10

Using for loop with a custom function

list1 = [10, 20, 100, 40, 90]


def small_num(list):
    min = list[0]
    for a in list:
        if a < min:
            min = a
    return min


res = small_num(list1)

print(res)

Do comment if you have any doubts or suggestions on this Python List topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.