Funció Python min().

Funció min() de Python retorna el més petit dels valors o l'element més petit d'un iterable passat com a paràmetre.

Exemple: Cerqueu l'enter min de Python a la llista

Python




numbers> => [> 23> ,> 25> ,> 65> ,> 21> ,> 98> ]> print> (> min> (numbers))>

Sortida

21 

Sintaxi de la funció Python min().

min(a, b, c, …, tecla=func)

El min() funció en Python pot agafar qualsevol tipus d'objecte de tipus similar i retornar el més petit entre ells. En el cas de les cadenes, retorna lexicogràficament el valor més petit.

Paràmetres

  • a, b, c, ..: tipus de dades similars.
  • clau (opcional): Una funció per personalitzar l'ordre d'ordenació

Tornar

Retorna l'element més petit.

Què és la funció min() a Python?

La funció min() de Python s'utilitza per trobar el valor mínim. És la funció contrària de màxim () .

Podeu utilitzar aquesta funció en qualsevol iterable com una cadena, una llista, una tupla, etc., cosa que la fa molt útil.

Com utilitzar la funció min() a Python?

Utilitzar la funció min() a Python és molt fàcil. Només cal passar la llista com a paràmetre a la funció min i retornarà el nombre mínim.

Exemple: Podem trobar fàcilment Python min de dos nombres.

Python




print> (> min> (> 45> ,> 56> ))>

Sortida

45 

Més exemples de Python min().

En aquest exemple, trobem l'element mínim en diferents referències utilitzant Python min().

Exemple 1: Trobeu Python Min of List

En aquest exemple, estem utilitzant min() per localitzar l'element més petit Python en una llista.

Python




numbers> => [> 3> ,> 2> ,> 8> ,> 5> ,> 10> ,> 6> ]> small> => min> (numbers);> print> (> 'The smallest number is:'> , small)>

Sortida

The smallest number is: 2 

Exemple 2: cerqueu min a la llista de cadena

En aquest exemple, estem utilitzant min() per localitzar la cadena més petita de Python en una llista.

Python




languages> => [> 'Python'> ,> 'C Programming'> ,> 'Java'> ,> 'JavaScript'> ,> 'PHP'> ,> 'Kotlin'> ]> small> => min> (languages)> print> (> 'The smallest string is:'> , small)>

Sortida

The smallest string is: C Programming 

Exemple 3: Element mínim en un diccionari

En aquest exemple, estem utilitzant min() per trobar el element mínim en a diccionari .

Python




square> => {> 5> :> 25> ,> 8> :> 64> ,> 2> :> 4> ,> 3> :> 9> ,> -> 1> :> 1> ,> -> 2> :> 4> }> print> (> 'The smallest key:'> ,> min> (square))> # -2> key2> => min> (square, key> => lambda> k: square[k])> print> (> 'The smallest value:'> , square[key2])> # 1>

Sortida

The smallest key: -2 The smallest value: 1 

En aquest article, hem comentat la definició, la sintaxi i els exemples de la funció min() de Python. La funció min() en Python és molt versàtil i es pot utilitzar amb qualsevol iterable.

Espero que aquest article us hagi ajudat a entendre com utilitzar la funció min() i la podeu utilitzar de manera efectiva als vostres projectes.

Llegeix més Funcions integrades de Python

Lectures similars:

  • Trobeu Min/Max en una llista heterogènia
  • max() i min() a Python
  • Ús de min() i max() a Python