PythonFunctions and
Modules
Functions
●
A functions is a reusable named set of
instructions. Below a function is defined and
then used (invoked, called) two different ways:
with literal as arguments and with variables as
arguments.
def mult(x, y): >>> mult(4, 5) >>> a = 5
return x * y 20 >>> b = 6
>>> mult(a, b)
30
2
Functions
● A function does not have to return a value
# This function does not return a value
def showInfo(name):
print("Hello", name)
print("Today is Monday")
print("The month is December")
print("The year is 2020")
>>> showInfo("Audrey")
Hello Audrey
Today is Monday
The month is December
The year is 2020
3
Default Arguments
●
You can specify default arguments in the
definition of a function. Then when calling the
function, you have the choice to use the
defaults or replace them with new arguments.
# Default arguments >>> foo(6)
def foo(a, b=2, c=3): 5
return a + b - c >>> foo(6, 7)
10
>>> foo(6, 7, 8)
5
4
Keyword Arguments
●
When calling a function, you can use the
parameter names to specify keyword-value
pairs. The advantage is that arguments do not
have to follow parameter positioning.
>>> minus(5, 4)
def minus(a, b):
1
return a - b
>>> minus(b=5, a=4)
-1
>>>
5
Variable Arguments
●
It is possible to define a Python function that
can accept any number of arguments.
def getsum(*numbers): >>> getsum(1, 2, 3)
total = 0 6
for number in numbers: >>> getsum(1, 2, 3, 4, 5, 6)
total = total + number 21
return total
6
Modules
A Python module is a file that contains
code to be reused in other programs.
Suppose you have written some
functions to do some basic arithmetic, like
these:
7
Modules
If you need these functions in several
applications, it is tedious to rewrite them
every time.
It is also tedious to look for them and copy
and paste them into a new program every
time.
But even worse, if you need tomodifyo
them, you have to hunt down every file in
which they appear. And there is no
guarantee that you’ll change them
consistently.
8
Modules
The solution is to put all these functions
in a module, let’s call it arithmetic.py.
Any changes you need to make, you
make them in one place.
Then, whenever you need to use any of
the functions contained therein, you just
import the module or just the function you
need.
9
Importing and Using a Module
1
0
Importing and Using a Module
●
To reduce typing, you can abbreviate a long
module name using the as keyword like this:
1
1
Importing and using a Module
You can import just the functions you need
instead of the entire module.
1
2
Testing your Module
It is a good idea to test the functions in your
module to make sure they are working correctly.
1
3
Testing your Module
However, you don’t want this test code to be executed when
the module is imported into another program.
You can prevent this by checking the name of a variable called
name
When a module is loaded through direct execution, that module’s
name variable is set to
“ main ”.
When it is loaded by being imported, it name
variable is set to the name of the module.
1
4
Conditional Execution of Test
Code
1
5
Tutorials
● Recommended Python Tutorials:
https://python.swaroopch.com/
http://anh.cs.luc.edu/python/hands-on/3.1/Hands
-onPythonTutorial.pdf
1
6