2. A function is a way to turn a bunch of related
statements into a single "chunk"
Python Libraries
3. A function is a way to turn a bunch of related
statements into a single "chunk"
–– AAvvooiidd dduupplliiccaattiioonn
Python Libraries
4. A function is a way to turn a bunch of related
statements into a single "chunk"
–– AAvvooiidd dduupplliiccaattiioonn
– Make code easier to read
Python Libraries
5. A function is a way to turn a bunch of related
statements into a single "chunk"
–– AAvvooiidd dduupplliiccaattiioonn
– Make code easier to read
A library does the same thing for related functions
Python Libraries
6. A function is a way to turn a bunch of related
statements into a single "chunk"
–– AAvvooiidd dduupplliiccaattiioonn
– Make code easier to read
A library does the same thing for related functions
Hierarchical organization
Python Libraries
7. A function is a way to turn a bunch of related
statements into a single "chunk"
–– AAvvooiidd dduupplliiccaattiioonn
– Make code easier to read
A library does the same thing for related functions
Hierarchical organization
family library
genus function
species statement
Python Libraries
9. Every Python file can be used as a library
Use import to load it
Python Libraries
10. Every Python file can be used as a library
Use import to load it
# halman.py
ddddeeeeffff threshold(signal):
rrrreeeettttuuuurrrrnnnn 1.0 / sum(signal)
Python Libraries
11. Every Python file can be used as a library
Use import to load it
# halman.py
ddddeeeeffff threshold(signal):
rrrreeeettttuuuurrrrnnnn 1.0 / sum(signal)
# program.py
iiiimmmmppppoooorrrrtttt halman
readings = [0.1, 0.4, 0.2]
pppprrrriiiinnnntttt 'signal threshold is', halman.threshold(readings)
Python Libraries
12. Every Python file can be used as a library
Use import to load it
# halman.py
ddddeeeeffff threshold(signal):
rrrreeeettttuuuurrrrnnnn 1.0 / sum(signal)
# program.py
iiiimmmmppppoooorrrrtttt halman
readings = [0.1, 0.4, 0.2]
pppprrrriiiinnnntttt 'signal threshold is', halman.threshold(readings)
$ python program.py
signal threshold is 1.42857
Python Libraries
14. When a module is imported, Python:
1. Executes the statements it contains
Python Libraries
15. When a module is imported, Python:
1. Executes the statements it contains
2. Creates an object that stores rreeffeerreenncceess ttoo
the top-level items in that module
Python Libraries
16. When a module is imported, Python:
1. Executes the statements it contains
2. Creates an object that stores rreeffeerreenncceess ttoo
the top-level items in that module
# noisy.py
pppprrrriiiinnnntttt 'is this module being loaded?'
NOISE_LEVEL = 1./3.
Python Libraries
17. When a module is imported, Python:
1. Executes the statements it contains
2. Creates an object that stores rreeffeerreenncceess ttoo
the top-level items in that module
# noisy.py
pppprrrriiiinnnntttt 'is this module being loaded?'
NOISE_LEVEL = 1./3.
>>>>>>>>>>>> import noisy
is this module being loaded?
Python Libraries
18. When a module is imported, Python:
1. Executes the statements it contains
2. Creates an object that stores rreeffeerreenncceess ttoo
the top-level items in that module
# noisy.py
pppprrrriiiinnnntttt 'is this module being loaded?'
NOISE_LEVEL = 1./3.
>>>>>>>>>>>> import noisy
is this module being loaded?
>>>>>>>>>>>> print noisy.NOISE_LEVEL
0.33333333
Python Libraries
21. Each module is a namespace
mmoodduullee
function
Python Libraries
22. Each module is a namespace
global
mmoodduullee
function
Python Libraries
23. Each module is a namespace
global
module
# module.py
NAME = 'Transylvania'
ddddeeeeffff func(arg):
function
rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg
Python Libraries
24. Each module is a namespace
global
module
# module.py
NAME = 'Transylvania'
ddddeeeeffff func(arg):
function
>>>>>>>>>>>> NAME = 'Hamunaptra'
rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg
Python Libraries
25. Each module is a namespace
global
module
# module.py
NAME = 'Transylvania'
ddddeeeeffff func(arg):
function
>>>>>>>>>>>> NAME = 'Hamunaptra'
>>>>>>>>>>>> import module
rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg
Python Libraries
26. Each module is a namespace
global
module
# module.py
NAME = 'Transylvania'
ddddeeeeffff func(arg):
function
>>>>>>>>>>>> NAME = 'Hamunaptra'
>>>>>>>>>>>> import module
>>>>>>>>>>>> print module.func('!!!')
Transylvania !!!
rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg
Python Libraries
28. Python comes with many standard libraries
>>> import math
Python Libraries
29. Python comes with many standard libraries
>>> import math
>>>>>>>>>>>> pppprrrriiiinnnntttt math.sqrt(2)
1.4142135623730951
Python Libraries
30. Python comes with many standard libraries
>>> import math
>>>>>>>>>>>> pppprrrriiiinnnntttt math.sqrt(2)
1.4142135623730951
>>>>>>>>>>>> pppprrrriiiinnnntttt math.hypot(2, 3) # sqrt(x**2 + y**2)
3.6055512754639891
Python Libraries
31. Python comes with many standard libraries
>>> import math
>>>>>>>>>>>> pppprrrriiiinnnntttt math.sqrt(2)
1.4142135623730951
>>>>>>>>>>>> pppprrrriiiinnnntttt math.hypot(2, 3) # sqrt(x**2 + y**2)
3.6055512754639891
>>>>>>>>>>>> pppprrrriiiinnnntttt math.e, math.pi # as accurate as possible
2.7182818284590451 3.1415926535897931
Python Libraries
33. Python also provides a help function
>>>>>>>>>>>> iiiimmmmppppoooorrrrtttt math
>>>>>>>>>>>> help(math)
HHeellpp oonn mmoodduullee mmaatthh::
NAME
math
FILE
/usr/lib/python2.5/lib-dynload/math.so
MODULE DOCS
http://www.python.org/doc/current/lib/module-math.html
DESCRIPTION
This module is always available. It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos(...)
acos(x)
Return the arc cosine (measured in radians) of x.
⋮
Python Libraries
35. And some nicer ways to do imports
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt sqrt
>>>>>>>>>>>> sqrt(3)
1.7320508075688772
Python Libraries
36. And some nicer ways to do imports
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt sqrt
>>>>>>>>>>>> sqrt(3)
1.7320508075688772
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt hypot aaaassss euclid
>>>>>>>>>>>> euclid(3, 4)
5.0
Python Libraries
37. And some nicer ways to do imports
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt sqrt
>>>>>>>>>>>> sqrt(3)
1.7320508075688772
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt hypot aaaassss euclid
>>>>>>>>>>>> euclid(3, 4)
5.0
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt *
>>>>>>>>>>>> sin(pi)
1.2246063538223773e-16
>>>>>>>>>>>>
Python Libraries
38. And some nicer ways to do imports
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt sqrt
>>>>>>>>>>>> sqrt(3)
1.7320508075688772
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt hypot aaaassss euclid
>>>>>>>>>>>> euclid(3, 4)
5.0
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt *
>>>>>>>>>>>> sin(pi)
1.2246063538223773e-16
>>>>>>>>>>>>
Generally a bad idea
Python Libraries
39. And some nicer ways to do imports
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt sqrt
>>>>>>>>>>>> sqrt(3)
1.7320508075688772
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt hypot aaaassss euclid
>>>>>>>>>>>> euclid(3, 4)
5.0
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt *
>>>>>>>>>>>> sin(pi)
1.2246063538223773e-16
>>>>>>>>>>>>
Generally a bad idea
Someone could add to
the library after you
start using it
Python Libraries
52. sys.stdin is standard input (e.g., the keyboard)
sys.stdout is standard output (e.g., the screen)
Python Libraries
53. sys.stdin is standard input (e.g., the keyboard)
sys.stdout is standard output (e.g., the screen)
sys.stderr is standard error (usually aallssoo tthhee ssccrreeeenn))
Python Libraries
54. sys.stdin is standard input (e.g., the keyboard)
sys.stdout is standard output (e.g., the screen)
sys.stderr is standard error (usually aallssoo tthhee ssccrreeeenn))
See the Unix shell lecture for more information
Python Libraries
60. The more polite way
'''Count lines in files. If no filename arguments given,
read from standard input.'''
iiiimmmmppppoooorrrrtttt sys
ddddeeeeffff count_lines(reader):
'''Return number of lines in text read from reader.'''
rrrreeeettttuuuurrrrnnnn len(reader.readlines())
iiiiffff __name__ == '__main__':
...as before...
Python Libraries
61. The more polite way
'''Count lines in files. If no filename arguments given,
read from standard input.'''
iiiimmmmppppoooorrrrtttt sys
ddddeeeeffff count_lines(reader):
'''Return number of lines in text read from reader.'''
rrrreeeettttuuuurrrrnnnn len(reader.readlines())
iiiiffff __name__ == '__main__':
...as before...
Python Libraries
62. The more polite way
'''Count lines in files. If no filename arguments given,
read from standard input.'''
iiiimmmmppppoooorrrrtttt sys
ddddeeeeffff count_lines(reader):
'''Return number of lines in text read from reader.'''
rrrreeeettttuuuurrrrnnnn len(reader.readlines())
iiiiffff __name__ == '__main__':
...as before...
Python Libraries
63. If the first statement in a module or function is
a string, it is saved as a docstring
Python Libraries
64. If the first statement in a module or function is
a string, it is saved as a docstring
Used for online ((aanndd oofffflliinnee)) hheellpp
Python Libraries
65. If the first statement in a module or function is
a string, it is saved as a docstring
Used for online ((aanndd oofffflliinnee)) hheellpp
# adder.py
'''Addition utilities.'''
ddddeeeeffff add(a, b):
'''Add arguments.'''
rrrreeeettttuuuurrrrnnnn a+b
Python Libraries
66. If the first statement in a module or function is
a string, it is saved as a docstring
Used for online ((aanndd oofffflliinnee)) hheellpp
# adder.py
'''Addition utilities.'''
ddddeeeeffff add(a, b):
'''Add arguments.'''
rrrreeeettttuuuurrrrnnnn a+b
>>>>>>>>>>>> import adder
>>>>>>>>>>>> help(adder)
NAME
adder - Addition utilities.
FUNCTIONS
add(a, b)
Add arguments.
>>>>>>>>>>>>
Python Libraries
67. If the first statement in a module or function is
a string, it is saved as a docstring
Used for online ((aanndd oofffflliinnee)) hheellpp
# adder.py
'''Addition utilities.'''
ddddeeeeffff add(a, b):
'''Add arguments.'''
rrrreeeettttuuuurrrrnnnn a+b
>>>>>>>>>>>> import adder
>>>>>>>>>>>> help(adder)
NAME
adder - Addition utilities.
FUNCTIONS
add(a, b)
Add arguments.
>>>>>>>>>>>> help(adder.add)
add(a, b)
Add arguments.
>>>>>>>>>>>>
Python Libraries
68. When Python loads a module, it assigns a value
to the module-level variable __name__
Python Libraries
69. When Python loads a module, it assigns a value
to the module-level variable __name__
main program
'__main__'
Python Libraries
70. When Python loads a module, it assigns a value
to the module-level variable __name__
main program loaded as library
'__main__' module name
Python Libraries
71. When Python loads a module, it assigns a value
to the module-level variable __name__
main program loaded as library
'__main__' module name
...module definitions...
iiiiffff __name__ == '__main__':
...run as main program...
Python Libraries
72. When Python loads a module, it assigns a value
to the module-level variable __name__
main program loaded as library
'__main__' module name
...module definitions... Always executed
iiiiffff __name__ == '__main__':
...run as main program...
Python Libraries
73. When Python loads a module, it assigns a value
to the module-level variable __name__
main program loaded as library
'__main__' module name
...module definitions... Always executed
iiiiffff __name__ == '__main__':
...run as main program... Only executed when
file run directly
Python Libraries
74. # stats.py
'''Useful statistical tools.'''
ddddeeeeffff average(values):
'''Return average of values or None if no data.'''
iiiiffff values:
rrrreeeettttuuuurrrrnnnn sum(values) / len(values)
eeeellllsssseeee::::
rrrreeeettttuuuurrrrnnnn NNNNoooonnnneeee
iiiiffff __name__ == '__main__':
pppprrrriiiinnnntttt 'test 1 should be None:', average([])
pppprrrriiiinnnntttt 'test 2 should be 1:', average([1])
pppprrrriiiinnnntttt 'test 3 should be 2:', average([1, 2, 3])
Python Libraries
75. # test-stats.py
ffffrrrroooommmm stats iiiimmmmppppoooorrrrtttt average
pppprrrriiiinnnntttt 'test 4 should be None:', average(set())
pppprrrriiiinnnntttt 'test 5 should be -1:', average({0, -1, -2})
Python Libraries
76. # test-stats.py
ffffrrrroooommmm stats iiiimmmmppppoooorrrrtttt average
pppprrrriiiinnnntttt 'test 4 should be None:', average(set())
pppprrrriiiinnnntttt 'test 5 should be -1:', average({0, -1, -2})
$ python stats.py
test 1 should be None: None
test 2 should be 1: 1
test 3 should be 2: 2
$
Python Libraries
77. # test-stats.py
ffffrrrroooommmm stats iiiimmmmppppoooorrrrtttt average
pppprrrriiiinnnntttt 'test 4 should be None:', average(set())
pppprrrriiiinnnntttt 'test 5 should be -1:', average({0, -1, -2})
$ python stats.py
test 1 should be None: None
test 2 should be 1: 1
test 3 should be 2: 2
$ python test-stats.py
test 4 should be None: None
test 5 should be -1: -1
$
Python Libraries