< previous | Table of Contents | next > |
4.2 TYPES OF FUNCTIONS
BUILT-IN FUNCTIONS
As you already have learned, Python's built-in functions are a set of pre-built functions that are readily available for use. Some examples include the following.
print()
- outputs data to the consoleint()
,str()
,float()
- convert objects to desired data typesinput()
- take input from a user.
IMPORTED FUNCTIONS
Another group of functions can be defined as imported functions. These libraries are made available for us to use by the Python community or other third parties. Some examples would be the following.
random
- library that contains functions such asrandint()
that allows you to generate random numbers.math
- a collection of mathematical functions, such assqrt()
,factorial()
,log()
, etc.
USER-DEFINED FUNCTIONS
User-defined functions in Python are custom functions that you write to solve tasks. They can also incorporate built-in or imported libraries within them.
# Function that calculates the square of the provided number def square_of(number): return number ** 2 print(square_of(10)) # Outputs 100
KEY THINGS TO REMEMBER
- Built-in functions are integrated into the Python interpreter, so they are available in any Python environment without the need to import additional libraries.
- Imported functions are functions made available by third parties. To access them you need to explicitly
import
these libraries or specific functions as shown in previous chapters. - The programmer writes user-defined functions to perform specific tasks that extend beyond the scope of Python's built-in functions.
< previous | Table of Contents | next > |