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

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
>>>s1="Python Programming"
>>>s2='Python Programs'
>>>s3="""Python is a powerful
Programming language"""

This will create a string object s1, s2 and s3 using different forms.
Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
We can also slice a string. Segment of a string is called a slice.
If the first index is not mentioned, the slicing will start from 0.
If the last index is not mentioned, the …

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. The class provides basic…

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…