lettura simple

How to get minimum value in Python

In this tutorial I'll explain how to find the minimum value in a set of numbers. In the python language there is a special function. It is the min() function.

min(x)

The x parameter is a list of numbers or an iterable object such as a list, tuple, dictionary, etc.

Note. All elements of the iterable object or list must be numbers. Otherwise, the python interpreter returns an error message.

How does it work?

The function iterates through the elements and outputs the minimum value.

I'll give you an example

n=[3,4,7,1,5,8]
vm=min(n)
print(vm)

The function finds the minimum value of the iterable object and assigns the value to the variable vm.

In this case the iterable object is a list.

The output result is 1

1

I'll give you another practical example

In this script, the min() function find the minimum value in a tuple

vm=min(3,4,7,1,5,8)
print(vm)

Numeric values must be listed one after the other and separated from each other by a comma.

The script output is as follows:

1

The function found the minimum value (1) in the list of numbers.




If something isn't clear, write your question in the comments.




FacebookTwitterLinkedinLinkedin