Home »
Python
Python Functions: Module's, Built-In, and User-Defined
In this tutorial, we will learn about Python functions, categories of functions like module functions, built-in & user-defined functions, what they are, and their usages with the help of examples.
By Abhishek Jain Last updated : December 30, 2023
Python Function
A function is named a sequence of statement(s) that performs a computation. It contains lines of code(s) that are executed sequentially from top to bottom by a Python interpreter. They are the most important building blocks for any software in Python. For working in script mode, we need to write the Python code in functions and save it in the file having a .py extension.
Types of Function
Functions can be categorized as belonging to:
- Python Module Functions
- Python Built-In Functions
- Python User-Defined Functions
1. Python Module Functions
A module is a file containing Python definitions (i.e. functions) and statements. Standard library of Python is extended as module(s) to a Programmer. Definitions from the module can be used into code of Program. To use these modules in a program, programmer needs to import the module. Once we import a module, we can reference (use) to any of its functions or variables in our code.
Some of the functions of the math module are: ceil(), floor(), fabs(), exp(), log(), log10(), pow(), sqrt(), cos(), sin(), tan(), degrees(), radians(), etc.
There are two ways to import a module in our program, they are:
The important Statement
It is simplest and most common way to use modules in our code.
Syntax
import modulename1 [, module name 2, ---------]
Example
Input any number and to find square and square root.
import math
x = int(input("Enter any number:"))
y = math.sqrt(x)
a = math.pow(x, 2)
print("Square Root value=", y)
print("Square value=", a)
Output
Enter any number:25
Square Root value= 5.0
Square value= 625.0
The from Statement
It is used to get a specific function in the code instead of complete file. If we know beforehand which function(s), we will be needing, then we may use from. For modules having large number of functions, it is recommended to use from instead of import.
Syntax
from modulename import functionname [, functionname ...]
from modulename import *
It will import everything from the file.
Example
Input any number and to find square and square root.
from math import sqrt, pow
x = int(input("Enter any number:"))
y = sqrt(x) # without using math
a = pow(x, 2) # without using math
print("Square Root value =", y)
print("Square value =", a)
Output
Enter any number:100
Square Root value = 10.0
Square value = 10000.0
2. Python Built-In Functions
Built in functions are the function(s) that are built into Python and can be accessed by Programmer. These are always available and for using them, we don't have to import any module (file). Python has a small set of built-in functions as most of the functions have been partitioned to modules. This was done to keep core language precise.
Python's built-in functions are: abs(), all(), any(), bin(), bool(), complex(), hex(), oct(), len(), chr(), divmod(), dict(), dir(), ord(), list(), cmp(), pow(), id(), int(), float(), range(), map(), and filter()
Syntax
function_name([argument(s)])
Example
Example of the use of built-in function (abs()).
# python code to demonstrate an example
# of abs() function
# integer number
iNum1 = 10
iNum2 = -10
print("Absolute value of ", iNum1, " is = ", abs(iNum1))
print("Absolute value of ", iNum2, " is = ", abs(iNum2))
# float number
fNum1 = 10.23
fNum2 = -10.23
print("Absolute value of ", fNum1, " is = ", abs(fNum1))
print("Absolute value of ", fNum2, " is = ", abs(fNum2))
# complex number
cNum1 = 10 + 5j
cNum2 = 10 - 5j
print("Absolute value of ", cNum1, " is = ", abs(cNum1))
print("Absolute value of ", cNum2, " is = ", abs(cNum2))
Output
Absolute value of 10 is = 10
Absolute value of -10 is = 10
Absolute value of 10.23 is = 10.23
Absolute value of -10.23 is = 10.23
Absolute value of (10+5j) is = 11.180339887498949
Absolute value of (10-5j) is = 11.180339887498949
3. Python User-Defined Functions
In Python, it is also possible for programmer to write their own function(s). These functions can then be combined to form module which can be used in other programs by importing them. To define a function, keyword def is used. After the keyword, comes an identifier i.e. name of the function, followed by parenthesized list of parameters and the colon which ends up the line, followed by the block of statement(s) that are the part of function.
Syntax
def NAME ([PARAMETER1, PARAMETER2, …..])
#Square brackets include optional part of statement
Example
To find simple interest using function.
# defining user-defined function
def SI(P, R, T):
return (P * R * T) / 100
# Caling SI function
result = SI(1000, 2, 1)
print("The simple interest is :", result)
Output
The simple interest is : 20.0
3.1. Creating a Function
To create a user-defined function in Python, use the def keyword followed by the function_name and parameter_list and then write the function body block.
Syntax
def function_name(parameter_list):
function body
In the above example, this is the function body, which calculates and returns the simple interest.
def SI(P, R, T):
return (P * R * T) / 100
Python Tutorial