Home » Python

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:

  1. Modules
  2. Built-In
  3. 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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.