Find the smallest number in 3 easy ways in Python

Mondo Technology Updated on 2024-02-01

Given a list, find the smallest number in the given list. Let's do it with 3 easy ways.

min() is a built-in function in Python that takes a list as a parameter and returns the number with the smallest value in the list.

lst = [9, -8, 12, -19, 6]

small_num = min(lst)

print(f"The minimum value in the list: is:")

sort() is a built-in method in Python that arranges items in ascending order by default. By sorting the list, you can use the index to access the first element of the list, which will be the smallest number in the list.

lst = [9, -8, 12, -19, 6]

lst.sort()

small_num = lst[0]

print(f"The minimum value in the list: is:")

Set a variable small num and assign it to the first element of the list, assuming the first element is the minimum. Use the len() function to calculate the length of a given list, and use the for loop to iterate over the list. Check that the loop variable is less than the value of small num. If the result is true, the value of the loop variable is assigned to small num. After the iteration is complete, the small num stores the minimum value of the list.

lst = [9, -8, 12, -19, 6]

small_num = lst[0]

lst_len = len(lst)

for i in range(lst_len):

if lst[i] small_num = lst[i]

print(f"The minimum value in the list: is:")

It's not easy to create an article, if you like this article, please follow, like and share it with your friends. If you have comments and suggestions, please give us feedback in the comments.

Related Pages