iap python - lecture 3 - mit student information ... · andrew farrell (mit sipb) iap python -...
TRANSCRIPT
![Page 1: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/1.jpg)
IAP Python - Lecture 3
Andrew Farrell
MIT SIPB
January 11, 2011
![Page 2: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/2.jpg)
Essential Python Modules subsection
Documentation
Documentation for these modules can be found a couple different ways.For the math module:
>>> import math
>>> help(math)
or visit http://docs.python.org/library/math.htmlor google for ”python math”the same will be true of other standard libraries
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 2 / 35
![Page 3: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/3.jpg)
Essential Python Modules subsection
Math - mathematical functions.
I Won’t discuss in detail, but includes
constants π and e
error function
trigonometric functions and their inverses
log, exponentiation
absolute value,factorial
You can write many of these yourself, but why bother.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 3 / 35
![Page 4: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/4.jpg)
Essential Python Modules subsection
operator
Contains functions for the operators that python defines. for example
>>> [1,2,3] contains 3
True
>>> 3+4
7
>>> import operator
>>> operator.contains([1,2,3],3)
True
>>> operator.add(3,4)
Why is this useful? So you can pass these functions around rather thancreating lambdas.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 4 / 35
![Page 5: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/5.jpg)
Essential Python Modules subsection
Datetime: time is actually kinda complicated
sure, you can try to represent time with some kind of tuples, or make yourown classes, but thats just silly.Python comes with a module for managing the Gregorian calendar.
time : tracks time within a day, which always has 24*3600 seconds init
date : tracks days of the calendar
datetime : both
timedelta : what you get when you subtract two of the first threeobjects. You can add it to other time objects
For each of these, you can stringify it with object.strftime(format string).You can also generate a datetime from a string and a format stringprovided the two match.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 5 / 35
![Page 6: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/6.jpg)
Essential Python Modules subsection
Datetime: time is actually kinda complicated
>>> import datetime
>>> today = datetime.datetime.today()
>>> today.strftime("%A,%b %d %H:%M")
’Tuesday,Jan 11 18:25’
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 6 / 35
![Page 7: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/7.jpg)
Essential Python Modules subsection
Random: Randomness
Your basic psuedo-random Number Generator. It is seeded with systemtime.
>>> import random
>>> random.random()
0.13255772573731972
>>> random.random()
0.92458247942405747
>>> random.random()
0.33678168180710721
It supports a wide variety of types of random variables, from uniform toWeibull. It also has functions to draw elements from a collection such as aset or a list.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 7 / 35
![Page 8: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/8.jpg)
Essential Python Modules subsection
Random: Randomness
Your basic psuedo-random Number Generator. It supports a wide varietyof types of random variables, from uniform to Weibull.It is seeded with system time.
>>> import random
>>> random.random()
0.13255772573731972
>>> random.random()
0.92458247942405747
>>> random.random()
0.33678168180710721
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 8 / 35
![Page 9: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/9.jpg)
Essential Python Modules subsection
open() and file objects
Not actually a separate library.
open(filename,mode)
The filename is the name of the file to be opened or created. A file objecthas a mode: what operations can you perform on it?
read
write: will clear the file beforehand
append: does not.
The mode is a string which determines what methods will actually work onit. it should be a single character, ’r’,’w’,or ’w’
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 9 / 35
![Page 10: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/10.jpg)
Essential Python Modules subsection
open() and file objects
If it is in read mode, a file is also an iterator over its lines.
>>> a = open("test.txt","r")
>>> [i for i in a]
[’fjksl\n’, ’jfkds\n’, ’sjfkl\n’]
otherwise, you can call read(), optionally passing it a number of characters.To write to it, you pass it a string. no need to pass in the number ofcharacters. Note that when you are done with a file, you should close it.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 10 / 35
![Page 11: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/11.jpg)
Essential Python Modules subsection
with statements
If it is in read mode, a file is also an iterator over its lines.
>>> with open("test.txt","r") as a:
... for line in a:
... print a
’fjksl
jfkds
sjfkl
You can automatically do this using a with statement, which will clean upthings once it is executed.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 11 / 35
![Page 12: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/12.jpg)
Essential Python Modules subsection
os and sys: operating system interface
sys contains data and objects pertaining to the state of the pythoninterpereter. For instance, if you started this on the command line,sys.flags will show the status of various options that you can pass intopython. Some useful things:
sys.argv: arguments passed on the command line to a script Notethat there are more sophisticated says of dealing with these, such asargparse
sys.modules: what modules can you import?
sys.version: what version of python are you using?
sys.sterr, sys.stdin, sys.stdout: file objects corresponding to stdin,stdout, and stderr.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 12 / 35
![Page 13: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/13.jpg)
Essential Python Modules subsection
os and sys: operating system interface
the os module is really two modules, as it also includes os.path.os.path lets you manipulate path names conveniently, joining, extractingdirectory names, etc.os lets you examine processes, rename, list, create and remove files, andopen files with some special flags.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 13 / 35
![Page 14: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/14.jpg)
Essential Python Modules subsection
Demonstration
and now I’m going to demonstrate an example of a small application Iwrote for generating Random numbers.
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 14 / 35
![Page 15: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/15.jpg)
Essential Python Modules subsection
Installing nonstandard libraries
There are two ways to get nonstandard libraries:
Your operating system distributes them or has a package thatprovides them
PyPI : http://pypi.python.org/pypi
To install packages off of PyPI, you want a tool called EasyInstall, foundat http://pypi.python.org/pypi/setuptools then you can simply
$ sudo easy_install grail
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 15 / 35
![Page 16: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/16.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 16 / 35
![Page 17: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/17.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 17 / 35
![Page 18: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/18.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 18 / 35
![Page 19: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/19.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 19 / 35
![Page 20: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/20.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 20 / 35
![Page 21: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/21.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 21 / 35
![Page 22: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/22.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 22 / 35
![Page 23: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/23.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 23 / 35
![Page 24: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/24.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 24 / 35
![Page 25: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/25.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 25 / 35
![Page 26: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/26.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 26 / 35
![Page 27: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/27.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 27 / 35
![Page 28: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/28.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 28 / 35
![Page 29: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/29.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 29 / 35
![Page 30: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/30.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 30 / 35
![Page 31: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/31.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 31 / 35
![Page 32: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/32.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 32 / 35
![Page 33: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/33.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 33 / 35
![Page 34: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/34.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 34 / 35
![Page 35: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/35.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 35 / 35
![Page 36: IAP Python - Lecture 3 - MIT Student Information ... · Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, ... 7 >>>importoperator ... IAP Python - Lecture 3 January 11,](https://reader031.vdocuments.site/reader031/viewer/2022022515/5afe09157f8b9a68498dc0f2/html5/thumbnails/36.jpg)
Essential Python Modules subsection
Andrew Farrell (MIT SIPB) IAP Python - Lecture 3 January 11, 2011 36 / 35