Python
http://markmeretzky.com/python/
mark.meretzky@gmail.com
Sections
- DHSS-GA.1120 possibly at NYU, 2022.
- SF19PB1
Python Bootcamp, Summer/Fall 2019.
- WS19PB02
Python Bootcamp, Spring 2019.
- INFO1-CE9990
Introduction to Python Programming, Summer 2017 Section 2.
For the current Python 3.10.6
(and the forthcoming
3.11)
- Python Tutorial.
- Python Language Reference.
Includes control structure keywords such as
while
,
for
,
and
if
.
- Python Standard Library.
Includes
- built-in functions
such as
print
,
input
,
and
round
-
modules
such as
sys
,
math
,
and
random
- data types
such as
- Numeric
types:
int
,
float
- Sequence
types:
str
,
bytes
,
list
,
tuple
,
range
- Mapping
types:
dict
- Set
types:
set
- Python Glossary,
for example
EAFP
vs.
LBYL.
-
the
HOWTOs
-
IDLE,
the
Integrated
Development Environment.
-
Learning Python, 5th ed.,
by Mark Lutz.
You don’t have to buy this book.
The course
-
Prologue.
A
computer
is a machine that follows a list of instructions.
The list of instructions is called a
program.
In the future,
it will be possible to write programs in English.
But that day hasn’t come yet,
so for the time being we write programs in simpler languages.
One of these simpler languages is
Python.
This will be a course in writing programs
in Python.
A short, informal program is often called a
script.
Test for students who already have Python experience.
Teaser.
-
Install
Python 3.8.0 and IDLE on
macOS,
Microsoft Windows,
and
Amazon AWS Linux.
IDLE is the
IDE
(Integerated Develpment Environment)
that comes with Python.
-
Definition
of IDLE in the Python
Glossary.
-
Wikipedia page
about IDLE.
-
Documentation
about IDLE.
-
Eric
IDLE.
-
Expressions
in the language Python.
Play with them using IDLE.
- Operators, operands, and variables.
The
dot operator.
- Operator
precedence
and associativity;
overriding them with parentheses.
- Function calls,
arguments, and return value.
- Data types:
int
,
float
,
string
,
bool
.
Conversion between the data types.
-
GitHub
- Create
a GitHub account.
- Create
a GitHub
organization
in your GitHub account.
- Create
a GitHub
repository
in your GitHub organization.
-
Standard i/o and variables.
- Standard output
with the
print
function and its
keyword
arguments
sep
and
end
.
(In Python 2,
print
was a statement, not a function.)
Docstrings,
pydoc3
.
-
Exit status:
an invisible number returned by the
sys.exit
in your Python script to the operating system.
-
Variables
and
assignment
statements.
We are about to need a variable to hold the standard input.
- Standard input
with the
input
function
(was
raw_input
in Python 2).
Raise and catch an
exception
when something goes wrong with input or type conversion.
Indentation.
EAFP
vs.
LBYL.
- Execute a Python script.
- on
macOS:
- in IDLE: pull down
Run
→ Run Module
-
in the macOS Finder:
drag the Python script onto
Applications/Python 3.7/Python Launcher.app
-
in the macOS Finder:
set the script’s Open With.
-
in the Terminal window: execute
python3
from the command line and feed it the script.
python3 myscript.py
-
in the Terminal window: make the script executable
using the
PATH
environment variable,
#!/usr/bin/env
,
and
chmod
,
- on
Microsoft Windows:
- in IDLE: pull down
Run
→ Run Module
-
in Windows Explorer:
take advantage of the
“file association”
between
python.exe
and any file whose name ends with
.py
-
in the Command Prompt window:
add one directory to the
PATH
environment variable
- Redirect the script’s standard i/o with
>
,
2>
,
<
,
and
|
(pipe).
Standard output
and
standard error output
can be directed to two different destinations.
- Control structure.
By default,
each instruction of a script is executed exactly once,
and they are executed
in the order in which they are written.
The
control structure
statements can cause the statements to be executed in some other order.
while
loops,
the
break
statement,
infinite loops,
nested loops
if
/elif
/else
statements
(conditionals)
for
loops and the
range
function as a more localized alternative to
while
loops.
The return value of
range
is an
iterable.
- Loops containing
if
statements:
-
Validate:
keep looping until the user provides valid input.
-
Guess:
a simple guessing game.
-
Teaser.
-
Monte Carlo
method for computing the value of
π.
-
Converge
until we’re close to the value of
π.
tkinter
graphics,
implemented with the
Tk
from
Tcl/Tk.
turtle
graphics.
-
Strings
of characters.
The
str
data type
is an example of a
sequence type.
See
sequence.
-
Indexing
(subscripting),
slicing,
and the
len
built-in function.
Immutability.
-
Characters:
loop through the characters of a
str
ing
with a
for
loop and the
enumerate
built-in function.
A
str
ing
and the return value of
enumerate
are
iterable.
- Search
a
str
ing
with
in
,
not
in
,
index
,
and a
regular expression.
- Split
a
str
ing.
-
Pig Latin.
Translate English to Pig Latin.
The operators
in
,
and
,
or
,
not
.
The string methods
isupper
,
isalpha
,
lower
,
capitalize
.
-
One big string.
Read a text file into one big string.
-
One
big sequence of bytes.
Read a text file from the Internet into one big sequence of bytes.
Then convert the sequence of bytes into one big string.
-
Other things to loop through with a
for
loop.
-
Lines:
loop through the lines of an input file or pipe with a
for
loop.
rstrip
the newline character and other whitespace.
-
URL:
loop through the lines of a file downloaded from the web.
decode
each incoming line from a
sequence
of bytes
into a more familiar
string of characters.
-
Binary file:
we can input a binary file,
but it has no lines to loop through
because it does not consist of lines of text.
We can, however,
loop through the rows and columns of pixels
in a binary file that is an image file.
-
Lists.
The
list
data type, like the
str
data type, is an example of a
sequence type.
- Basics.
-
List of numbers.
Mutability.
-
List of strings.
Access the individual characters of each string.
- Loop
through the items of a
list
with
while
,
for
,
range
,
and
enumerate
.
A
list
is
iterable.
-
zip
function for looping through two
list
s
in parallel
-
Alternative:
a list as an alternative for a long chain of
if/elif/else
statements.
Search a list with
index
.
-
Barchart: a
tkinter
graphic of the data in a list.
-
Sort
the items of a list with the functions
sort
and
sorted
.
Custom sort orders by specifying a
key function.
Pass a
lambda
(i.e., nameless)
function to another function.
Make a
shallow
copy
of a list.
Search a list with
index
.
-
Max:
one more example of a
lambda
function.
-
Create
a list by
split
ting
a string into a list of strings.
Loop through the words on a line.
-
Create
a list by reading the lines of a text file into a list of strings.
We can split the file into lines as we input it,
or input the file into one huge string and split it up later.
-
List
comprehension:
create a copy of an existing
list,
usually with modifications.
See
List
Comprehensions.
-
Date:
an object of class
datetime.date
holds a
year,
month,
and
day.
-
CSV:
comma-separated values.
-
CSV database:
a database of comma-separated values.
Search the New York City
Restaurant
Inspection
results.
append
to a
list.
-
Street trees.
Search a database and display what you find on a Google map.
-
Pandas:
data analysis and scientific computing.
See
pandas
and
NumPy.
pip3 install numpy
pip3 list
pip3 show numpy
-
Summary
of the ways to create a list.
-
Insert,
remove,
and
del
ete
list items.
See
del
in
tutorial.
Might be simpler to create a new
list
instead of performing surgery on an old one.
-
Higher
order functions
(i.e., functions whose arguments are functions)
for looping through a
list
:
map
and
itertools.starmap
filter
functtools.reduce
and the
operator
functions
operator.add
,
operator.mul
,
operator.concat
A two-dimensional
list
is implemented in Python as a
list
of
list
s.
-
Heterogeneous:
a list that contains a list,
i.e., a list one of whose items is a list.
Followed by a list all of whose items are lists.
- Tic-tac-toe
board.
- Plot generator
has a list of lists.
The
random.choice
function.
- Roman:
convert an integer to and from Roman numerals.
-
Manhattan
address algorithm has a list of lists.
append
to a
list.
Add a
tkinter
GUI with
buttons,
drop-down
menus,
etc.
-
JSON:
deserialize a
JSON
string into a Python
list.
Tuples.
A tuple is like an immutable
list
.
-
Immutable.
Dictionaries.
A
dictionary
is like a list with two parallel columns.
The left column contains
keys.
The right column contains
values.
We look up a key in order to find the corresponding value.
Each key can appear in the dictionary only once.
- Two parallel columns,
the keys and the values.
-
Numeric keys:
never create a
dict
ionary
whose keys are consecutive
int
egers.
It’s simpler to create a
list
instead.
-
Sparse:
use a dictionary instead of a sparse
list.
-
Dictionary comprehension.
-
Defaultdict
is a dictionary whose initial values are created for you by default.
-
Counter
counts how many times an item appears.
-
Environment variables
such as
PATH
,
etc.
-
Attributes:
a dictionary of attributes of different data types.
-
JSON:
deserialize a
JSON
string into a Python
dictionary.
-
Download JSON:
download and parse JSON from
OpenWeathermap,
Google
Maps,
and
GitHub.
-
Download XML:
download and parse XML from
OpenWeathermap.
- Facebook:
traverse the Facebook graph.
Sets.
A set is like a
dict
ionary
with keys but with no corresponding values.
Each key can appear in the set only once.
-
Test for membership
with the
in
operator.
-
No duplicates:
a
set
can contain no more than one copy of each item.
-
Intersect:
compute the
intersection,
union,
and
difference
of two sets.
-
Limitations
on the types of objects that can be contained by a
set
.
- Miscellaneous compound data types:
collections.namedtuple
and
range
.
Functions
defined by the user (i.e., created by you).
Package a piece of code as a function and give it a convenient name.
-
Define
a function.
-
Pass
an argument
to a function.
-
Pass list:
pass a
list
or
tuple
to a function.
-
Keyword argument:
pass an optional
keyword
argument
to a function
(like the
sep
and
end
arguments of the
print
function).
-
Variable number:
a function that will accept a variable number of
positional
arguments.
-
Variable keyword:
a function that will accept a variable number of
positional
arguments,
followed by a variable number of
keyword
arguments.
-
Count calls:
a function that can count how many times it’s been called,
by incrementing a
global
variable.
-
Return tuple:
a function that
returns
a
tuple
of items.
Modules for natural (i.e., human) language processing.
- Speech to text.
- Text to speech.
- Translation.
Static type checking.
-
mypy
-
Annotate
a variable (including a function argument)
and a function return value.
Classes
and
objects.
An
object
is a value that contains
smaller values
inside of it,
and that has
functions
that belong to it.
The objects we create will include
unit tests
and
iterables.
- Attributes:
attach an
attribute
to an
object.
Call the
hasattr
,
setattr
,
and
getattr
functions.
-
Setattr:
the
setattr
and
getattr
functions.
-
__init__:
provide a class with an
__init__
method to initialize each instance of the class.
-
Pass
an object to a function.
-
Class Date:
create a class
containing instance attributes and class attributes,
instance methods and class methods.
The
class
definition,
the
self
argument,
the
__init__
and
__str__
methods.
-
Module:
define a class in a separate
module,
and then
import
the module into your Python script.
if __name__ == "__main__":
-
Dir:
list the
attributes
of an object,
and use
callable
to find out which attributes are also
methods.
-
with
statement and
context
managers.
-
decimal
module as an example of a context manager.
-
Unit
tests
for the
graph paper
function.
-
Unit tests
for
class
Date
.
-
Life:
J. H. Conway’s Game of Life.
-
Tweepy:
the Python interface to the Twitter API.
Get the most recent tweets that mention Trump or DeBlasio.
Install a Python package (for Twitter, Facebook, etc.) with
pip3
.
Iterators
and
generators
- Iterators:
loop through a range of
int
s,
a range of
float
s,
and a range of
date
s.
- Generator:
a
generator
function
that
returns a
generator
iterator.
The generator function
yield
s
a range of
floats
.
-
Generator
expressions
look like list comprehensions,
but with
(
parentheses)
instead of
[
square
brackets]
.
-
Getitem:
apply an index to an object.
Inheritance:
create a new class with a head start.
- Subclass:
create a subclass of an existing class.
-
Leapdate:
create a smarter class
Date
that knows about leap years
so it can pass the unit tests.
-
Abstract base class.
- Multiple inheritance.
Multiple
Inheritance
in the Python Tutorial.
SQLite:
the light version of the Structured Query Language.
-
Play with SQLite.
Put tables into a database, put rows into a table.
CRUD, join.
- Inspection:
create, update, and search
an SQLite database on your Mac or PC of restaurant inspection results.
Regular expressions
for searching a string.
This is how you win at Scrabble.
-
Elementary
regular expressions.
The
re.search
function.
-
Groups
in a regular expression.
-
Match
objects tell you what you found.
-
Substitute
different text for what you found.
The
re.sub
function.
- Compile
a regular expression with the
re.compile
function.
Pandas
Recursion.
A recursive function is one that calls itself.
A recursive data structure is one that contains smaller data structures
of the same shape (i.e., a
list
that contains smaller lists,
that in turn contain even smaller lists).
-
Recursion:
a recursive function that prints the integers from 1 to 10.
-
Quicksort:
a recursive function that sorts a list.
-
Subsets:
print all the subsets of a set.
-
Tree:
print and search a simple recursive tree.
-
Tree of processes:
print the tree of currently running processes on macOS.
-
Maze:
find a path through a maze using recursion.
Miscellaneous.
-
io.StringIO:
hold a string in memory rather than in a file.
Used by pdfminer.
-
Selenium
browser automation.
-
CGI:
the Common Gateway Interface.
Connect an HTML form to a Python cgi script.
Test it with Selenium.
- pdfminer:
search a PDF file.
-
Beautiful Soup
HTML parser.
-
dscl:
directory service command line,
plistlib
,
binascii
.
Summary
of the course,
and of the distinguishing features of Python.
Next time I teach this course:
-
Catch exception in function that raised it, vs. catch exception
in the caller’s code.
- Multithreading.
Multi-threading,
Concurrent
execution,
threading
and
queue
modules,
global
interpreter lock.
-
Specify width for
tkinter.OptionMenu
as in OpenWeatherMap.
-
Move
~
interpretation (introduced in
Binary)
up to first program
(for
loop)?
that reads from a file in home directory
(Lines).