A function it's a code block, which receives zero or more arguments and performs an operation to return a value and/or performs a specific task. The functions must have a assigned name to be able to “call” them during the execution of the program and they are completely reusable.
Although you can create all kinds of functions, today we are going to focus on those that come by default integrated into Python. If you want to develop your career in Python, we recommend you visit our master in Advanced Programming in Python for Big Data, Hacking and Machine Learning and train with the best experts.
To start we can use the function dir() to list all predefined functions. We can get help from different functions with help(), for example the function max():
Most notable predefined functions
- list() Create a list from an element.
- tuple() Create or convert to a tuple.
- type() Returns the type of the element passed as a parameter.
To work with text:
- print() Prints on the screen the argument that we pass to it
- len() It returns the length in characters of the string that we pass as a parameter
- replace() Replaces a string with the one it includes as an argument.
- upper() Converts the string passed as an argument to uppercase
- lower() Converts the string passed as an argument to lowercase
And the most used numerical functions:
- sum() Sums the list of numbers passed as an argument.
- min() Determines the minimum value of a list of numbers passed as an argument.
- max() Determines the maximum value of a list of numbers passed as an argument.
- range() Creates an arithmetic list of numbers based on the value passed as an argument.
- str() Converts a numeric value to text.
There are many more predefined functions in Python, you can learn to use them and even create functions yourself.