Python 
Libraries 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.
A function is a way to turn a bunch of related 
statements into a single "chunk" 
Python Libraries
A function is a way to turn a bunch of related 
statements into a single "chunk" 
–– AAvvooiidd dduupplliiccaattiioonn 
Python Libraries
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
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
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
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
Every Python file can be used as a library 
Python Libraries
Every Python file can be used as a library 
Use import to load it 
Python Libraries
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
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
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
When a module is imported, Python: 
Python Libraries
When a module is imported, Python: 
1. Executes the statements it contains 
Python Libraries
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
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
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
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
Each module is a namespace 
Python Libraries
Each module is a namespace 
function 
Python Libraries
Each module is a namespace 
mmoodduullee 
function 
Python Libraries
Each module is a namespace 
global 
mmoodduullee 
function 
Python Libraries
Each module is a namespace 
global 
module 
# module.py 
NAME = 'Transylvania' 
ddddeeeeffff func(arg): 
function 
rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg 
Python Libraries
Each module is a namespace 
global 
module 
# module.py 
NAME = 'Transylvania' 
ddddeeeeffff func(arg): 
function 
>>>>>>>>>>>> NAME = 'Hamunaptra' 
rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg 
Python Libraries
Each module is a namespace 
global 
module 
# module.py 
NAME = 'Transylvania' 
ddddeeeeffff func(arg): 
function 
>>>>>>>>>>>> NAME = 'Hamunaptra' 
>>>>>>>>>>>> import module 
rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg 
Python Libraries
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
Python comes with many standard libraries 
Python Libraries
Python comes with many standard libraries 
>>> import math 
Python Libraries
Python comes with many standard libraries 
>>> import math 
>>>>>>>>>>>> pppprrrriiiinnnntttt math.sqrt(2) 
1.4142135623730951 
Python Libraries
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
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
Python also provides a help function 
Python Libraries
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
And some nicer ways to do imports 
Python Libraries
And some nicer ways to do imports 
>>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt sqrt 
>>>>>>>>>>>> sqrt(3) 
1.7320508075688772 
Python Libraries
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
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
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
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
Almost every program uses the sys library 
Python Libraries
Almost every program uses the sys library 
>>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys 
Python Libraries
Almost every program uses the sys library 
>>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 
2.7 (r27:82525, Jul 4 2010, 09:01:59) 
[MSC v.1500 32 bit (Intel)] 
Python Libraries
Almost every program uses the sys library 
>>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 
2.7 (r27:82525, Jul 4 2010, 09:01:59) 
[MSC v.1500 32 bit (Intel)] 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.platform 
win32 
Python Libraries
Almost every program uses the sys library 
>>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 
2.7 (r27:82525, Jul 4 2010, 09:01:59) 
[MSC v.1500 32 bit (Intel)] 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.platform 
win32 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.maxint 
2147483647 
Python Libraries
Almost every program uses the sys library 
>>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 
2.7 (r27:82525, Jul 4 2010, 09:01:59) 
[MSC v.1500 32 bit (Intel)] 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.platform 
win32 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.maxint 
2147483647 
>>>>>>>>>>>> pppprrrriiiinnnntttt sys.path 
['', 
'C:WINDOWSsystem32python27.zip', 
'C:Python27DLLs', 'C:Python27lib', 
'C:Python27libplat-win', 
'C:Python27', 'C:Python27libsite-packages'] 
Python Libraries
sys.argv holds command-line arguments 
Python Libraries
sys.argv holds command-line arguments 
Script name is sys.argv[0] 
Python Libraries
sys.argv holds command-line arguments 
Script name is sys.argv[0] 
# echo.py 
iiiimmmmppppoooorrrrtttt sys 
ffffoooorrrr i iiiinnnn range(len(sys.argv)): 
pppprrrriiiinnnntttt i, '"' + sys.argv[i] + '"' 
Python Libraries
sys.argv holds command-line arguments 
Script name is sys.argv[0] 
# echo.py 
iiiimmmmppppoooorrrrtttt sys 
ffffoooorrrr i iiiinnnn range(len(sys.argv)): 
pppprrrriiiinnnntttt i, '"' + sys.argv[i] + '"' 
$$$$ python echo.py 
0 echo.py 
$ 
Python Libraries
sys.argv holds command-line arguments 
Script name is sys.argv[0] 
# echo.py 
iiiimmmmppppoooorrrrtttt sys 
ffffoooorrrr i iiiinnnn range(len(sys.argv)): 
pppprrrriiiinnnntttt i, '"' + sys.argv[i] + '"' 
$$$$ python echo.py 
0 echo.py 
$ python echo.py first second 
0 echo.py 
1 first 
2 second 
$ 
Python Libraries
sys.stdin is standard input (e.g., the keyboard) 
Python Libraries
sys.stdin is standard input (e.g., the keyboard) 
sys.stdout is standard output (e.g., the screen) 
Python Libraries
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
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
# count.py 
iiiimmmmppppoooorrrrtttt sys 
iiiiffff len(sys.argv) == 1: 
count_lines(sys.stdin) 
eeeeeeeellllllllsssssssseeeeeeee:::::::: 
rd = open(sys.argv[1], 'r') 
count_lines(rd) 
rd.close() 
Python Libraries
# count.py 
iiiimmmmppppoooorrrrtttt sys 
iiiiffff len(sys.argv) == 1: 
count_lines(sys.stdin) 
eeeeeeeellllllllsssssssseeeeeeee:::::::: 
rd = open(sys.argv[1], 'r') 
count_lines(rd) 
rd.close() 
Python Libraries
# count.py 
iiiimmmmppppoooorrrrtttt sys 
iiiiffff len(sys.argv) == 1: 
count_lines(sys.stdin) 
eeeeeeeellllllllsssssssseeeeeeee:::::::: 
rd = open(sys.argv[1], 'r') 
count_lines(rd) 
rd.close() 
Python Libraries
# count.py 
iiiimmmmppppoooorrrrtttt sys 
iiiiffff len(sys.argv) == 1: 
count_lines(sys.stdin) 
eeeeeeeellllllllsssssssseeeeeeee:::::::: 
rd = open(sys.argv[1], 'r') 
count_lines(rd) 
rd.close() 
$ python count.py < a.txt 
48 
$ 
Python Libraries
# count.py 
iiiimmmmppppoooorrrrtttt sys 
iiiiffff len(sys.argv) == 1: 
count_lines(sys.stdin) 
eeeeeeeellllllllsssssssseeeeeeee:::::::: 
rd = open(sys.argv[1], 'r') 
count_lines(rd) 
rd.close() 
$ python count.py < a.txt 
48 
$ python count.py b.txt 
227 
$ 
Python Libraries
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
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
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
If the first statement in a module or function is 
a string, it is saved as a docstring 
Python Libraries
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
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
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
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
When Python loads a module, it assigns a value 
to the module-level variable __name__ 
Python Libraries
When Python loads a module, it assigns a value 
to the module-level variable __name__ 
main program 
'__main__' 
Python Libraries
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
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
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
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
# 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
# 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
# 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
# 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
created by 
Greg Wilson 
October 2010 
Copyright © Software Carpentry 2010 
This work is licensed under the Creative Commons Attribution License 
See http://software-carpentry.org/license.html for more information.

Libraries

  • 1.
    Python Libraries Copyright© Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.
  • 2.
    A function isa way to turn a bunch of related statements into a single "chunk" Python Libraries
  • 3.
    A function isa way to turn a bunch of related statements into a single "chunk" –– AAvvooiidd dduupplliiccaattiioonn Python Libraries
  • 4.
    A function isa way to turn a bunch of related statements into a single "chunk" –– AAvvooiidd dduupplliiccaattiioonn – Make code easier to read Python Libraries
  • 5.
    A function isa 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 isa 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 isa 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
  • 8.
    Every Python filecan be used as a library Python Libraries
  • 9.
    Every Python filecan be used as a library Use import to load it Python Libraries
  • 10.
    Every Python filecan 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 filecan 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 filecan 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
  • 13.
    When a moduleis imported, Python: Python Libraries
  • 14.
    When a moduleis imported, Python: 1. Executes the statements it contains Python Libraries
  • 15.
    When a moduleis 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 moduleis 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 moduleis 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 moduleis 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
  • 19.
    Each module isa namespace Python Libraries
  • 20.
    Each module isa namespace function Python Libraries
  • 21.
    Each module isa namespace mmoodduullee function Python Libraries
  • 22.
    Each module isa namespace global mmoodduullee function Python Libraries
  • 23.
    Each module isa namespace global module # module.py NAME = 'Transylvania' ddddeeeeffff func(arg): function rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg Python Libraries
  • 24.
    Each module isa namespace global module # module.py NAME = 'Transylvania' ddddeeeeffff func(arg): function >>>>>>>>>>>> NAME = 'Hamunaptra' rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg Python Libraries
  • 25.
    Each module isa namespace global module # module.py NAME = 'Transylvania' ddddeeeeffff func(arg): function >>>>>>>>>>>> NAME = 'Hamunaptra' >>>>>>>>>>>> import module rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg Python Libraries
  • 26.
    Each module isa namespace global module # module.py NAME = 'Transylvania' ddddeeeeffff func(arg): function >>>>>>>>>>>> NAME = 'Hamunaptra' >>>>>>>>>>>> import module >>>>>>>>>>>> print module.func('!!!') Transylvania !!! rrrreeeettttuuuurrrrnnnn NAME + ' ' + arg Python Libraries
  • 27.
    Python comes withmany standard libraries Python Libraries
  • 28.
    Python comes withmany standard libraries >>> import math Python Libraries
  • 29.
    Python comes withmany standard libraries >>> import math >>>>>>>>>>>> pppprrrriiiinnnntttt math.sqrt(2) 1.4142135623730951 Python Libraries
  • 30.
    Python comes withmany 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 withmany 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
  • 32.
    Python also providesa help function Python Libraries
  • 33.
    Python also providesa 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
  • 34.
    And some nicerways to do imports Python Libraries
  • 35.
    And some nicerways to do imports >>>>>>>>>>>> ffffrrrroooommmm math iiiimmmmppppoooorrrrtttt sqrt >>>>>>>>>>>> sqrt(3) 1.7320508075688772 Python Libraries
  • 36.
    And some nicerways 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 nicerways 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 nicerways 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 nicerways 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
  • 40.
    Almost every programuses the sys library Python Libraries
  • 41.
    Almost every programuses the sys library >>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys Python Libraries
  • 42.
    Almost every programuses the sys library >>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys >>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] Python Libraries
  • 43.
    Almost every programuses the sys library >>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys >>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] >>>>>>>>>>>> pppprrrriiiinnnntttt sys.platform win32 Python Libraries
  • 44.
    Almost every programuses the sys library >>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys >>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] >>>>>>>>>>>> pppprrrriiiinnnntttt sys.platform win32 >>>>>>>>>>>> pppprrrriiiinnnntttt sys.maxint 2147483647 Python Libraries
  • 45.
    Almost every programuses the sys library >>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys >>>>>>>>>>>> pppprrrriiiinnnntttt sys.version 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] >>>>>>>>>>>> pppprrrriiiinnnntttt sys.platform win32 >>>>>>>>>>>> pppprrrriiiinnnntttt sys.maxint 2147483647 >>>>>>>>>>>> pppprrrriiiinnnntttt sys.path ['', 'C:WINDOWSsystem32python27.zip', 'C:Python27DLLs', 'C:Python27lib', 'C:Python27libplat-win', 'C:Python27', 'C:Python27libsite-packages'] Python Libraries
  • 46.
    sys.argv holds command-linearguments Python Libraries
  • 47.
    sys.argv holds command-linearguments Script name is sys.argv[0] Python Libraries
  • 48.
    sys.argv holds command-linearguments Script name is sys.argv[0] # echo.py iiiimmmmppppoooorrrrtttt sys ffffoooorrrr i iiiinnnn range(len(sys.argv)): pppprrrriiiinnnntttt i, '"' + sys.argv[i] + '"' Python Libraries
  • 49.
    sys.argv holds command-linearguments Script name is sys.argv[0] # echo.py iiiimmmmppppoooorrrrtttt sys ffffoooorrrr i iiiinnnn range(len(sys.argv)): pppprrrriiiinnnntttt i, '"' + sys.argv[i] + '"' $$$$ python echo.py 0 echo.py $ Python Libraries
  • 50.
    sys.argv holds command-linearguments Script name is sys.argv[0] # echo.py iiiimmmmppppoooorrrrtttt sys ffffoooorrrr i iiiinnnn range(len(sys.argv)): pppprrrriiiinnnntttt i, '"' + sys.argv[i] + '"' $$$$ python echo.py 0 echo.py $ python echo.py first second 0 echo.py 1 first 2 second $ Python Libraries
  • 51.
    sys.stdin is standardinput (e.g., the keyboard) Python Libraries
  • 52.
    sys.stdin is standardinput (e.g., the keyboard) sys.stdout is standard output (e.g., the screen) Python Libraries
  • 53.
    sys.stdin is standardinput (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 standardinput (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
  • 55.
    # count.py iiiimmmmppppoooorrrrttttsys iiiiffff len(sys.argv) == 1: count_lines(sys.stdin) eeeeeeeellllllllsssssssseeeeeeee:::::::: rd = open(sys.argv[1], 'r') count_lines(rd) rd.close() Python Libraries
  • 56.
    # count.py iiiimmmmppppoooorrrrttttsys iiiiffff len(sys.argv) == 1: count_lines(sys.stdin) eeeeeeeellllllllsssssssseeeeeeee:::::::: rd = open(sys.argv[1], 'r') count_lines(rd) rd.close() Python Libraries
  • 57.
    # count.py iiiimmmmppppoooorrrrttttsys iiiiffff len(sys.argv) == 1: count_lines(sys.stdin) eeeeeeeellllllllsssssssseeeeeeee:::::::: rd = open(sys.argv[1], 'r') count_lines(rd) rd.close() Python Libraries
  • 58.
    # count.py iiiimmmmppppoooorrrrttttsys iiiiffff len(sys.argv) == 1: count_lines(sys.stdin) eeeeeeeellllllllsssssssseeeeeeee:::::::: rd = open(sys.argv[1], 'r') count_lines(rd) rd.close() $ python count.py < a.txt 48 $ Python Libraries
  • 59.
    # count.py iiiimmmmppppoooorrrrttttsys iiiiffff len(sys.argv) == 1: count_lines(sys.stdin) eeeeeeeellllllllsssssssseeeeeeee:::::::: rd = open(sys.argv[1], 'r') count_lines(rd) rd.close() $ python count.py < a.txt 48 $ python count.py b.txt 227 $ Python Libraries
  • 60.
    The more politeway '''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 politeway '''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 politeway '''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 firststatement in a module or function is a string, it is saved as a docstring Python Libraries
  • 64.
    If the firststatement 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 firststatement 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 firststatement 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 firststatement 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 loadsa module, it assigns a value to the module-level variable __name__ Python Libraries
  • 69.
    When Python loadsa module, it assigns a value to the module-level variable __name__ main program '__main__' Python Libraries
  • 70.
    When Python loadsa module, it assigns a value to the module-level variable __name__ main program loaded as library '__main__' module name Python Libraries
  • 71.
    When Python loadsa 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 loadsa 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 loadsa 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 '''Usefulstatistical 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 ffffrrrroooommmmstats 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 ffffrrrroooommmmstats 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 ffffrrrroooommmmstats 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
  • 78.
    created by GregWilson October 2010 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.