A function is a piece of code designed to carry out a specific task. It may be only one line long or several lines long. Functions allow you to re-use code. You don’t have to continually write code over and over again. The Python keyword for defining a user defined function is def. There are different types of functions in Python, and here are the three types.
- Built in Functions
- User Defined Function (UDF)
- Anonymous Functions (Lambda Functions)
You have probably already used several built in functions such as print(), input(), len(), set(),
The documentation for Python built in functions can easily be found by Googling python built in functions. The documentation is at https://docs.python.org/3/library/functions.html. Let’s just quickly look at the len() function as an example.
Len()
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
st = 'hello and welcome to functions' nums = (1,2,3) lst = [2,4,6,8]
print(len(st)) print(len(nums)) print(len(lst))
30 3 4