SlideShare a Scribd company logo
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.

More Related Content

What's hot (20)

ODP
Programming Under Linux In Python
PDF
Python for Linux System Administration
PDF
Python- strings
PPT
Euro python2011 High Performance Python
PDF
An introduction to Python for absolute beginners
PDF
Python于Web 2.0网站的应用 - QCon Beijing 2010
PDF
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
PPTX
Basic Python Programming: Part 01 and Part 02
PPTX
Python 101: Python for Absolute Beginners (PyTexas 2014)
PDF
Argparse: Python command line parser
ODP
Python 3000
PDF
ZCA: A component architecture for Python
PDF
AmI 2017 - Python basics
PDF
Python 101 1
PDF
Geeks Anonymes - Le langage Go
PPTX
2015 bioinformatics python_strings_wim_vancriekinge
ODP
What Shazam doesn't want you to know
PDF
Python 101
PPTX
Sequences: Strings, Lists, and Files
PDF
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RR
Programming Under Linux In Python
Python for Linux System Administration
Python- strings
Euro python2011 High Performance Python
An introduction to Python for absolute beginners
Python于Web 2.0网站的应用 - QCon Beijing 2010
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
Basic Python Programming: Part 01 and Part 02
Python 101: Python for Absolute Beginners (PyTexas 2014)
Argparse: Python command line parser
Python 3000
ZCA: A component architecture for Python
AmI 2017 - Python basics
Python 101 1
Geeks Anonymes - Le langage Go
2015 bioinformatics python_strings_wim_vancriekinge
What Shazam doesn't want you to know
Python 101
Sequences: Strings, Lists, and Files
Module Workshop NSC "Raspberry pi3 with Python" - Sanusi & Sasmitoh RR
Ad

Similar to Libraries (20)

PDF
W-334535VBE242 Using Python Libraries.pdf
PPTX
Using python libraries.pptx , easy ppt to study class 12
PDF
Python. libraries. modules. and. all.pdf
PDF
ch 2. Python module
PPTX
Class 12 CBSE Chapter: python libraries.pptx
PDF
Using Python Libraries.pdf
PPTX
Using-Python-Libraries.9485146.powerpoint.pptx
PDF
Functions and modules in python
PDF
Python libraries
PPTX
Python Modules and Libraries
PDF
Python Modules, Packages and Libraries
ODP
Pythonpresent
PPTX
Python introduction
PDF
class 12 Libraries in dfbdsbsdbdfbdfdfdf.pdf
PDF
Functions_in_Python.pdf text CBSE class 12
PPTX
CLASS-11 & 12 ICT PPT Functions in Python.pptx
PPTX
Functions_in_Python.pptx
PPT
python language programming presentation
PPTX
Python short notes on modules and applications
PPTX
this includes basics about python modules and packages introduction
W-334535VBE242 Using Python Libraries.pdf
Using python libraries.pptx , easy ppt to study class 12
Python. libraries. modules. and. all.pdf
ch 2. Python module
Class 12 CBSE Chapter: python libraries.pptx
Using Python Libraries.pdf
Using-Python-Libraries.9485146.powerpoint.pptx
Functions and modules in python
Python libraries
Python Modules and Libraries
Python Modules, Packages and Libraries
Pythonpresent
Python introduction
class 12 Libraries in dfbdsbsdbdfbdfdfdf.pdf
Functions_in_Python.pdf text CBSE class 12
CLASS-11 & 12 ICT PPT Functions in Python.pptx
Functions_in_Python.pptx
python language programming presentation
Python short notes on modules and applications
this includes basics about python modules and packages introduction
Ad

Recently uploaded (20)

PDF
Modernizing your data center with Dell and AMD
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
KodekX | Application Modernization Development
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
cuic standard and advanced reporting.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Modernizing your data center with Dell and AMD
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Monthly Chronicles - July 2025
20250228 LYD VKU AI Blended-Learning.pptx
KodekX | Application Modernization Development
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
cuic standard and advanced reporting.pdf
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
CIFDAQ's Market Insight: SEC Turns Pro Crypto
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

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 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
  • 8. Every Python file can be used as a library 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
  • 13. When a module is imported, Python: 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
  • 19. Each module is a namespace Python Libraries
  • 20. Each module is a namespace function 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
  • 27. Python comes with many standard libraries 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
  • 32. Python also provides a help function 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
  • 34. And some nicer ways to do imports 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
  • 40. Almost every program uses the sys library Python Libraries
  • 41. Almost every program uses the sys library >>>>>>>>>>>> iiiimmmmppppoooorrrrtttt sys Python Libraries
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. sys.argv holds command-line arguments Python Libraries
  • 47. sys.argv holds command-line arguments Script name is sys.argv[0] Python Libraries
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. sys.stdin is standard input (e.g., the keyboard) 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
  • 55. # 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
  • 56. # 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
  • 57. # 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
  • 58. # 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
  • 59. # 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
  • 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
  • 78. 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.