You can learn about Functions in Python Programs with Outputs helped you to understand the language better.
Python Programming – Types Of Functions
Python comes preloaded with many function definitions that you can use or create new functions as per your requirement. Functions are of three types. These are:
- Built-in Functions
- User-Defined Functions
A module is a file that contains a collection of related functions and other definitions. Python comes loaded with some pre-defined modules that you can use or you even create your own modules. The Python modules that come preloaded with Python are called standard library modules. Three such standard library modules are the math module, random module, and statistics module. You can make a module available to another program by importing it. For example, if you want a pre-defined function pi ( ), inside your module, you need to first import the module math which contains definitions of pi() in your program. Ferre’s how you import:
>>> import math
Here’s how you access the name pi that it defines:
Note that you can import modules with the import command. To use a module, you can import it. Usually, import statements occur at the beginning of the Python module. Importing modules is a fairly simple operation, but it requires a little explanation. Modules are discussed in detail in Chapter 12.
The math Module
This module contains a set of commonly-used mathematical functions, including number-theoretic functions (such as factorial); logarithmic and power functions; trigonometric (and hyperbolic) functions; angular conversion functions (degree/radians); and some special functions and constants.
The random Module
The random module provides tools for making random selections. Random numbers are used for games, simulations, testing, security, and privacy applications. The random module must be imported before it can be used.
The statistics Module
The statistics module provides functions to mathematical statistics of numeric data. The popular statistical functions are defined in this module are mean ( ), median ( ) and mode ( ).
Built-in functions are those which come along with Python installation as libraries and always available for use, without importing any module (file). Built-in functions in Python Programming Language are called a Library function. You don’t have to bother about the logic inside the Library functions. The Python interpreter has a number of built-in functions. They are loaded automatically as the interpreter starts and are always available. For example, print ( ) and input ( ) for I/O, number con-version functions int ( ), float( ), complex( ), data type conversions list(), tuple( ), set( ), etc.
Instead of depending on built-in functions, the Python programming language allows you to create your own functions called user-defined functions. For example, if you want to perform some mathematical calculations then you can place them in a separate function with the proper function name and then you can call that function multiple times.
Python function is the grouping of program statements into a single unit. Each Python function can be activated through the execution of a function call. Execution always begins at the first statement of the program. Statements are executed one at a time, in order, from top to bottom. The order in which statements are executed is called the flow of execution. So far you have only seen the functions which come with Python either in some file (module) or in the interpreter itself (built-in), but it is also possible for a programmer to write their own function(s). These functions can then be combined to form a module which can then be used in other programs by importing them.