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.

Eg:

>>> x = math.cos(math.pi/2)

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

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

**import**

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

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

**Example**

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

5.0

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.

Example

>>>import math as m

>>>m.pow(2,3)

8.0

8.0

**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…..]**

**Example**

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

>>>help(math)

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

>>>help(math.sqrt)

To know the list of available modules use can type

>>>help(‘modules’)

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

Suffixes are: .dll, .pyd, .so, .sl, ...

C-Modules
linked with the Interpreter:

It's possible to get a complete list of these modules:

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

https://docs.python.org/2/py-modindex.html

**Packages**

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 __init__.py, or else your package import will fail.

Each directory named within the path of a package must also contain a file named __init__.py, 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
1,2,3,….,100

5,10,15,20,…,50

50,47,44,….,8,5,2

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)

50,47,44,….,8,5,2

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)

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.

ReplyDeleteBest Python training Institute in chennai

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

ReplyDeletedocker-training-in-chennai

very interesting. python training in Chennai

ReplyDeletevery interesting python training in Chennai

ReplyDelete