What is a Function in Python
A function is a block of reusable code, that performs a specific task. This is helpful when you have code, complex or not that you will be using often within your program.
We will cover built-in functions in Python specifically. We can also create custom functions in Python.
Numeric Functions
Min()
Returns the smallest numeric value in a list.
- Example:
1
2
3
4
sales = [120.64, 120.5, 99.65, 72.35]
# Find the lowest sale
print(min(sales))
- Output:
1
72.35
Max()
Max returns the largest numeric value in a string.
- Using the previous example:
1
print(max(sales))
- Output:
1
120.64
Sum()
Sum adds all the values in a list.
- Using sales list in previous example:
1
print(sum(sales))
- Output:
1
413.14
Round()
Round will round the value to the nearest decimal specified.
1
print(round(sum(sales), 1))
- Output:
1
413.1
If no number is specified then python will round to the nearest whole number.
General Purpose
Len()
The Len() function returns the number of items in an object. It can be applied to various data structures.
- Using our previous sales list:
1
print(len(sales))
- Output:
1
4
Sorted()
The sorted() function returns a new sorted list from the elements of any iterable without modifying the original iterable.
1
print(sorted(sales))
- Output:
1
[72.35, 99.65, 120.5, 120.64]
Help()
The help() function provides information on how to use a function.
1
print(help(sorted))
- Output: ```python Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False) Return a new list containing all items from the iterable in ascending order.
1
2
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order. ```