Skip to main content

Built in Functions, Modules and Packages

A function is a 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 blocks for any software development in Python.

Built in functions are the function(s) that are built into Python and can be accessed directly.
Functions usually have arguments and return a value.

Some built in functions are given below with examples. Refer python documentation for more details.

abs(x) returns the absolute value of x . abs(-45) will return 45
max(x,y,z) returns the maximum of x,y,z . max(10,20,30) will return 30
min(x,y,z) returns the minimum of x,y,z . min(10,20,30) will return 10
divmod(x,y) returns both the quotient and remainder . divmod(14,5) will return (2,4)
cmp(x,y) returns 0 if x==y , 1 if x>y and -1 if x<y
round(x,n) round x to n digits. round(3.14567,2) will return 3.15
range(start,stop,step) will return a list from start to stop-1 with an increment of step.
range(10) will return [0,1,2,…9] range(1,10,2) will return [1,3,5,7,9]
type(x) will return the type of the variable object x.
dir(x) will display the details of the object x.
len(x) will return the length of the object.
int(),float(),str(),bool(),chr(),long() these functions can be used for type conversions.
bin(x), oct(x), hex(x) these functions will convert the decimal number x into corresponding base.

Composition of functions

Just as with mathematical functions, Python functions can be composed, meaning that you can use an expression or the result of another function as an argument to a function.
>>> x = math.cos(math.pi/2)

>>> x = math.exp(math.log(10.0))


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 within the code of a program. To use these modules in the program, a programmer needs to import the module. Once you import a module, you can reference (use), any of its functions or variables in your code. There are two ways to import a module in your program using the following statement
i. import
ii. from

It is simplest and most common way to use modules in our code. Its syntax is:

import modulename1 [,modulename2, ---------]

>>> import math
On execution of this statement, definitions of the module will become part of the code in which the module was imported.
To use/ access/invoke a function, you will specify the module name and name of the
function- separated by dot (.).

>>> value= math.sqrt (25) # dot notation
The example uses sqrt( ) function of module math to calculate square root of the value
provided in parenthesis, and returns the result which is inserted in the value. The
expression (variable) written in parenthesis is known as argument (actual argument). It
is common to say that the function takes arguments and return the result.

You can also use alias name while importing a module and then use the alias name for invoking a function.
>>>import math as m

from statement

It is used to get a specific function in the code instead of the complete module file. If we
know beforehand which function(s), we will be needing, then we may use from. For modules having large no of functions, it is recommended to use from instead of import.
Its syntax is

>>> from modulename import functionname [, functionname…..]

>>> from math import sqrt
>>>value = sqrt (25)
Here, we are importing sqrt function only, instead of the complete math module. Now
sqrt( ) function will be directly referenced to. These two statements are equivalent to
previous example.
>>>from modulename import *
will import everything from the module file.

Note: You normally put all import statement(s) at the beginning of the Python file but
technically they can be anywhere in program.

To know more about the functions in a module you can use the help facility of python. After importing a module call the help, it will list all functions.

You can also get the help of a function in a module.

To know the list of available modules use can type


Kinds of Modules
There are different kind of modules:
Those written in Python
   They have the suffix: .py
Dynamically linked C modules
    Suffixes are: .dll, .pyd, .so, .sl, ...
C-Modules linked with the Interpreter:
   It's possible to get a complete list of these modules:
import sys
print   sys.builtin_module_names

Content of a module

With the built-in function dir() and the name of the module as an argument, you can list all valid attributes and methods for that module.
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin','atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees',
'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot','ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']

Click the following link to know more about python  modules


It's possible to put several modules into a Package. A directory of Python code is said to be a package. A package is imported like a "normal" module.
Each directory named within the path of a package must also contain a file named, or else your package import will fail. 

Programs to try  using built in functions

a)Read a number in decimal and convert it into different bases.
b)Read a binary number (Eg: 0b101010) and convert it into different bases.
c)Read a hexa decimal number (Eg: 0xa3) and convert it into different bases.
d)Read an octal number (Eg:012) and convert it into different base.
e)Find the biggest and smallest of three numbers.
f)Read a number and find its factorial.
g)Find the number of digits in the factorial of a given number.
h)Generate the following sequences using range function
  Odd numbers less than 100.
  Even numbers less than 100.
  Even numbers less than 100 in reverse order.
i) Read a Year and print the calendar of that year
(Hint: import calendar ;print calendar.calendar(2017) will display 2017 calendar.
j)Read a Year and month and print the calendar of the specified month.
(Hint: print calendar.month(2017,8) will display 2017 August month calendar)


  1. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    Best Python training Institute in chennai

  2. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.


Post a Comment

Popular posts from this blog

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc). When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name. By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. To open a fil…

User Defined Functions in Python

So far we have only seen the functions which come with Python either in some file (module) or in interpreter itself (built in), but it is also possible for programmer to write their own function(s) and are called User Defined Functions. These functions can then be combined to form a module which can then be used in other programs by importing them.
In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition.
To define a function keyword def is used. After the keyword comes an identifier of the function, followed by parenthesized list of parameters and the colon which ends up the line. Next follows the block of statement(s) that are the part of function. A block is one or more lines of code, grouped together so that they are treated as one big sequence of statements while executing. In Python, statements in a block are written with indentation. Usually, a block begins when a li…

Dictionary In Python

Dictionaries are similar to other compound types except that they can use any immutable type as an index. We can refer to a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps a value. The association of a key and a value is called a key-value pair. A dictionary is an extremely useful data storage construct for storing and retrieving all key value pairs, where each element is accessed (or indexed) by a unique key. However, dictionary keys are not in sequences.
            Dictionaries are mutable.             Dictionaries are unordered. Items in dictionaries are accessed via keys and not via their position. A dictionary is an associative array (also known as hashes). Any key of the dictionary is associated (or mapped) to a value. The values of a dictionary can be any Python data type. So dictionaries are unordered key-value-pairs.  Dictionaries don't support the sequence operation of the sequence data types like strings, tuples and…