Functions
Functions are like machines


Learning to write your own functions will greatly increase the complexity of the programs that you can write
A function is a black box-it takes some input,does something with it, and spits out some output
Functions hide details away, allowing you to solve problems at a higher level without getting bogged down
Examples
A typical function looks like this:
def function_name(function_arguments)
"""optional string decribing the function"""
statements ...
return result
A function example: sum
In [1]: numbers=[1,2,3,4,5]
In [2]: sum(numbers)
Out[2]: 15
Anatomy of sum function
1.initialize the sum to zero
2.loop over each number while adding the number to the sum variable
3.return the value of sum
def sum(xs):
"""Given a sequence of numbers, return the sum."""
s=0
for x in xs:
s=s+x
return s
Writing your own function
Open a text editor, type the following and save it as MyMathFunctions.py:
def mysum(numbers):
"""Given a sequence of numbers, return the sum"""
s=0
for x in range(numbers):
s=s+x
return s
def myproduct(numbers):
"""Given a sequence of numbers, return the product"""
p=1
for x in range(numbers):
p=p*x
return p
Importing a function from a file (module)
Once we import
MyMathFunctionsmodule we just wrote, we can use themysumfunction andmyproductfunction just like the built-in functionThe way to call a function is to give the function name followed by parenthesis with values for the number of arguments expected
In [1]: import MyMathFunctions
In [2]: numbers=[1,2,3,4]
In [3]: MyMathFunctions.mysum(numbers)
Out[3]: 10
In [4]: MyMathFunctions.myproduct(numbers)
Out[4]: 24
Too many typing strokes? Try the following:
In [5]: import MyMathFunctions as f
In [6]: f.mysum(numbers)
Out[6]: 10
Still too many typing strokes? Try the following:
In [7]: import MyMathFunctions
In [8]: a=MyMathFunctions.mysum
In [9]: a(numbers)
Out[9]: 10
Function arguments
We can define functions with more than one arguments
Example: restrcition.py
def finder(DNA,enzyme):
db={}
name=['ECOR1','BAMH1','HINDIII']
site=['GAATTC','GGATCC','AAGCTT']
db=dict(zip(name,site))
DNA=DNA.upper()
enzyme=enzyme.upper()
index=DNA.find(db[enzyme])
if (index>-1):
print ("The restriction site starts at base pair %d\n" %index)
else:
print ("No such restriction site\n")
In [1]: import restriction
In [2]: restriction.finder('ATGGAATTCCGT','EcoR1')
The restriction site starts at base pair 3
In [3]: restriction.finder('ATGGAATTCCGT','BamH1')
No such restriction site
Arguments with default values do not need to be supplied when calling a function. But if provided, will overwrite the default values
Example: restrcition_BamH1_default.py
def finder(DNA,enzyme='BamH1'):
db={}
name=['ECOR1','BAMH1','HINDIII']
site=['GAATTC','GGATCC','AAGCTT']
db=dict(zip(name,site))
DNA=DNA.upper()
enzyme=enzyme.upper()
index=DNA.find(db[enzyme])
if (index>-1):
print ("The restriction site starts at base pair %d" %index)
else:
print ("No such restriction site\n")
In [1]: import restriction_BamH1_default
In [2]: restriction_BamH1_default.finder('ATGGAATTCCGT')
No such restriction site
In [3]: restriction_BamH1_default.finder('ATGGAATTCCGT','Ecor1')
The restriction site starts at base pair 3Some existing Python modules
The
osmodule provides a platform independent way to work with the operating system, make or remove files and directoriesThe
csvmodule provides readers and writers for comma separated value dataThe
sysmodule contains many objects and functions for dealing with how python was complied or called when executedThe
globmodule proves theglobfunction to perform file globbing similar to what the unix shell providesThe
mathmodule provides common algebra and trigonometric function along with several math constantsThe
remodule provides access to powerful regular expressionThe
datetimemodule provides time and datetime objects. allowing easy comparison of times and datesThe
timemodule provides simple estimates for how long a command takesThe
picklemodule provides a way to save python objects to a file that you can unpickle later in a different programThe
pypimodule helps package installationThe
numpymodule is the de facto standard for numerical computingThe
pandasmodule is useful for tabular data managingThe
matplotlibmodule is the most frequently used plotting package in PythonThe
seabornmodule is a module based onmatplotlib. It provides a high-level interface for drawing attractive graphics.
Last updated
Was this helpful?
