Numeric built-in Functions in Python

The max() and min() functions

The max() and min() functions – are used to find the maximum or minimum of some numbers of a list.

Example:

list_one = [1, 2, 3, 4, 0, 2, 1]
print(min(list_one))
print(max(list_one))

some_nums = (1, 4, 9, 2, 5, 6, 8)
print(min(some_nums))
print(max(some_nums))

The terminal output will be:

ddn_ro@linux:~/Desktop$ python file.py
0
4
1
9
ddn_ro@linux:~/Desktop$

The abs() function

The abs() function – is used to find the distance of a number from zero (its absolute value).

Example:

print(abs(-53))
print(abs(27))
print(abs(27.5))

The terminal output will be:

ddn_ro@linux:~/Desktop$ python file.py
53
27
27.5
ddn_ro@linux:~/Desktop$

The round() function

The round() function – is used to round a number to a certain number of decimal places.

Example:

print(round(43.7942117, 2))

The terminal output will be:

ddn_ro@linux:~/Desktop$ python file.py
43.79
ddn_ro@linux:~/Desktop$

The sum() function

The sum() function – is used to find the total of a list.

Example:

print(sum([1, 2, 3, 4, 5]))

The terminal output will be:

ddn_ro@linux:~/Desktop$ python file.py
12
ddn_ro@linux:~/Desktop$

Leave a Reply