Notes

`min()` Function in Python [ English ]

< Prev Next >

1. Introduction

The min() function in Python is a built-in function used to find the smallest element among a group of values. It can be used with multiple values or with an iterable object such as a list, tuple, set, or string.

The function compares the values and returns the minimum (smallest) value.

It is commonly used in programs where we need to identify the lowest number, smallest item, or earliest value in a dataset.

2. Syntax

Syntax 1: Using multiple arguments

min(value1, value2, value3, ...)

Syntax 2: Using an iterable

min(iterable, key=None)

3. Parameters

  1. value1, value2, ... Two or more values to compare.

  2. iterable A sequence such as a list, tuple, or set whose smallest element needs to be found.

  3. key (optional) A function used to customize the comparison.

4. Return Value

5. Basic Example

print(min(10, 5, 20, 3))

Output

3

Explanation

6. Example with a List

numbers = [8, 2, 15, 4, 10]

print(min(numbers))

Output

2

Explanation

7. Example with a Tuple

numbers = (12, 7, 25, 5)

print(min(numbers))

Output

5

Explanation

8. Example with a String

text = "python"

print(min(text))

Output

h

Explanation

9. Example Using key Parameter

words = ["apple", "banana", "kiwi", "cherry"]

print(min(words, key=len))

Output

kiwi

Explanation

10. Practical Example

marks = [85, 90, 78, 88]

lowest = min(marks)

print("Lowest Marks:", lowest)

Output

Lowest Marks: 78

Explanation

11. Summary

< Prev Next >