# Functions in Python

Learn: What are **Modules**, **Built-In** and **User Defined functions**? In this article, we are going to learn about Modules, Built-In, User Defined functions in Python with Examples.

Submitted by Abhishek Jain, on August 24, 2017

**A function is named sequence of statement(s) that performs a computation. It contains line of code(s) that are executed sequentially from top to bottom by Python interpreter.** They are the most important building block 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 .py extension. Functions can be categorized as belonging to:

**Modules****Built-In****User Defined**

### 1) Modules

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. There are two ways to import a module in our program, they are:

- import
- from

**Import:** 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

**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 *

**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

**The functions available in math module are:**

ceil(), floor(),fabs(),exp() ,log(), log10() ,pow(),sqrt(),cos(), sin(), tan() ,degrees(), radians() ,etc.

**Some functions from random module are:**

random() ,randint() ,uniform() ,randrange()

### 2) Built in Function

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.

abs(), max(), min(), bin(),divmod(),len(), range(), round(),bool(),chr(), float(),int(), long(), str(), type(), id( ),tuple( ).

### 3) 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.

def SI(P,R,T): return((P*R*T)/100)

Output

>>>SI(1000,2,10) 200.0

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions