0% found this document useful (0 votes)
51 views

Functions, Modules and Packages in Python: N.Venkatesh Asst - Prof of CSE

The document discusses functions, modules, and packages in Python. It defines functions as reusable blocks of code that take arguments and return values. Modules allow the organization of Python code into files while packages are collections of modules. The document covers topics like default arguments, keyword arguments, variable arguments, anonymous functions, importing modules, and user-defined modules.

Uploaded by

Ankur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Functions, Modules and Packages in Python: N.Venkatesh Asst - Prof of CSE

The document discusses functions, modules, and packages in Python. It defines functions as reusable blocks of code that take arguments and return values. Modules allow the organization of Python code into files while packages are collections of modules. The document covers topics like default arguments, keyword arguments, variable arguments, anonymous functions, importing modules, and user-defined modules.

Uploaded by

Ankur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

functions

Modules
Packages

Functions,Modules and Packages in Python

N.Venkatesh
Asst.Prof of CSE

JNTUK-University College of Engineering,Vizianagaram

July 1, 2017

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python


functions
Modules
Packages

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

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

Why functions

Similar or Identical Code in multiple places ? Issues: Writing


many times, maintainance
Functions can be used to reduce code duplication and make
programs more easily understood and maintained.
As the Algorithms you design get increasingly complex,
TopDown Approach: Divide into subproblems, and write
independent subprograms

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

functions

A function is like a subprogram, a small program inside of a


program
The basic idea we write a sequence of statements and then
give that sequence a name. We can then execute this
sequence at any time by referring to the name.
The part of the program that creates a function is called a
function definition.
When the function is used in a program, we say the definition
is called or invoked.
def greet():
print(’Happy BirtDay X’)
greet()

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

Arguments, Parameters, Return

function takes arguments and returns result


functions can be defined with paramater names,A paramater
is a variable that is initialized when the function is called
An argument is a value we pass into the function as its input
when we call the function
return is used ”to store the result of function computation
and use it later”
def printtwice(Person):
print(Person)
print(Person)
printtwice(”Santosh”)
printtwice(”Priya”)

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

Default Values for Arguments

You can provide default values for a functions arguments


These arguments are optional when the function is called
def myfun(b, c=3, d=’hello’):
return b + c
myfun(5,3,hai)
myfun(5,3)
myfun(5)
positional arguments so order is important

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

KeyWord Arguments

Some times, the order of parameters is hard to remember, you


can supply name of parameter
The arguments that are supplied with the names of
parameters are called keyword arguments
def myfun(x,y,z):
print(’x=’,x,’y=’,y,’z=’,z)
myfun(x=2,y=8,z=20)
myfun(x=2,z=4,y=9) myfun(45,z=4,y=9)
can call a function with some/all of its arguments out of order
as long as you specify their names

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

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

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

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

Functions without a name, created by using lambda


lamda argumentslist: expression
lamda x,y: x+y
lamda feature was added to python due to demand from LISP
programmers
Used in combination with functions map(),reduce(),filter()
supports functional programming

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python


functions Introduction
Modules import module
Packages User-Defined Modules

module

A module is a file that contains a collection of related


functions and classes(.py)
Examples: all mathematical related functions can be written
into one file, all operating system related functions can
written into one file, all graphics related functions can be
written into one file.
user-defined or library module

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python


functions Introduction
Modules import module
Packages User-Defined Modules

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

Everything in somefile.py gets imported.


To refer to something in the file, append the text ”somefile.”
to the front of its name:
somefile.func(25)
somefile.classname.function(’jntuv’)
math.sqrt(34) (import math)

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python


functions Introduction
Modules import module
Packages User-Defined Modules

import continued

from somefile import *

Everything in somefile.py gets imported.


To refer to anything in the module, just use its name.
Everything in the module is now in the current namespace.
Caution: Using this import command can easily overwrite the
definition of an existing function or variable
func(25)
classname.function(’jntuv’)
sqrt(34) (from math import *)

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python


functions Introduction
Modules import module
Packages User-Defined Modules

import continued

from somefile import ClassName/function

Only the item className/function in somefile.py gets


imported
After importing className, you can just use it without a
module prefix. Its brought into the current namespace.
Caution: Using this import command can easily overwrite the
definition of an existing function or variable
func(25)
classname.function(’jntuv’)
sqrt(34) (from math import sqrt)

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python


functions Introduction
Modules import module
Packages User-Defined Modules

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

Write one or more classes/functions into somefile.py file,now


somefile is a module
It has to stored in sys.path directory or append the file
directory to sys.path
Beauty is you can import and use where ever you wish

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python


functions
Modules Introduction
Packages

Thank You!

N.VenkateshAsst.Prof of CSE Functions,Modules and Packages in Python

You might also like