Functions, Modules and Packages in Python: N.Venkatesh Asst - Prof of CSE
Functions, Modules and Packages in Python: N.Venkatesh Asst - Prof of CSE
Modules
Packages
N.Venkatesh
Asst.Prof of CSE
July 1, 2017
Table of contents
1 functions
Introduction
Default Values for Arguments
KeyWord Arguments
Variable-Number arguments
Anonymous Functions
2 Modules
Introduction
import module
User-Defined Modules
3 Packages
Introduction
Why functions
functions
KeyWord Arguments
Variable-Number Arguments
In Python, functions can defined such that they can work with
variable-number of arguments
A parameter with name begins with * gathers any number of
arguments supplied by the user of function.It actually gathers
into tuple
def printall(*args):
print(args)
Try to write definition of sumall that takes any number of
values and returns their sum
Variable-Number Arguments
In Python, functions can defined such that they can work with
variable-number of arguments
A parameter with name begins with * gathers any number of
arguments supplied by the user of function.It actually gathers
into tuple
def printall(*args):
print(args)
Try to write definition of sumall that takes any number of
values and returns their sum
def printall(*args):
sum=0
for i in args:
sum=sum+i
print(sum)
N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python
Introduction
functions Default Values for Arguments
Modules KeyWord Arguments
Packages Variable-Number arguments
Anonymous Functions
Anonymous Functions
module
import statement
import is used to use the functions & classes defined in
another file/module ex: import math
To access one of the functions, specify the name of the
module and the name of the function, separated by a dot.
math.sqrt(34)
Different ways of importing
import somefile
from somefile import *
from somefile import className/function
from somemodule import somefunction, anotherfunction,
yetanotherfunction
import somefile as newname
from somefile import function as newname
The difference? What gets imported from the file and
What name refers to it after importing
N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python
functions Introduction
Modules import module
Packages User-Defined Modules
import continued
import somefile
import continued
import continued
import continued
As clause:
What if you have two modules, each containing a function
called open.
you can add an as clause to the end and supply the name you
want to use, either for the entire module or for the given
function:
from module1 import open as open1
from module2 import open as open2
Directories for module files:
The list of directories where Python will look for the files to
be imported is sys.path This is just a variable named path
stored inside the sys module
import sys sys.path
N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python
functions Introduction
Modules import module
Packages User-Defined Modules
Module
Thank You!