try
and
except
:
we can go over them again next week.
Read the course’s
grading policy.
Bookmark the Python
documentation
in your browser.
Look at the documentation for the built-in functions
we mentioned in class tonight:
round
,
print
,
input
,
len
,
int
,
etc.
Create
a GitHub account if you don’t already have one.
Please email me (at
mark.meretzky@gmail.com
)
your name and the name of your GitHub account.
Log into your GitHub account.
To add a photo of yourself,
pull down the triangle in the upper right corner,
select Settings,
and upload a picture.
Then create a GitHub organization named
WS19PB02-yourname
(with a dash),
where
yourname
is your GitHub loginname.
We will use a GitHub
organization
as a container to contain our
repositories.
Each repository will contain one Python program.
WS19PB02-yourname
(with a dash),
where
yourname
is your GitHub loginname.
The billing email address should be your email address.
Under “Choose your plan” select “$0 per month”.
At the bottom of the page,
press the Create Organization button.
Here is the instructor’s
GitHub account
and his
WS19PB02-MarkMeretzky
organization.
The organization contains a repository named
flag
,
and the repository contains a file named
flag.py
.
This file is a Python script and we’ll talk about it
here.
Three differences between Python 2 and Python 3 we mentioned tonight (see Common Stumbling Blocks):
/
operator gave you a truncated quotient.
In Python 3,
the
/
operator
gives you a quotient that is not truncated.
print
was a statement and needed no parentheses.
In
Python 3,
print
is a function and therefore needs parentheses.
input
function was named
raw_input
.
(To make matters worse, Python 2 also had another function named
input
.)
#List the directories searched by the import statement. import sys print(type(sys.path)) print() #Skip a line. for dir in sys.path: #sys.path is a list print(dir) sys.exit(0)
<class 'list'> /Users/mark/python /Users/mark/Documents /Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7 /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
If you want to launch IDLE on macOS with an extra directory added to its
sys.path
,
export PYTHONPATH=$PYTHONPATH:/extra/directory python3 -m idlelib
if
statements.
Learn the rules for telling whether a year is leap or not.
Then satisfy yourself that
leap.py
embodies these rules correctly.
Write (and upload to your GitHub organization) an interesting Python program
with one or more
while
loops.
matplotlib
on your computer at home.
Then run
while
and/or
for
)
and
if
statements.
It’s okay if you want to produce output in HTML format instead of plain
text.
tkinter
and displays a flag of some country, real or imagined,
or some other interesting picture.
Another way to take a
list
of three numbers
and create a new
list
of the
sin
of the three numbers
is with a
list
comprehension:
import math oldList = [0, math.pi / 2, math.pi * 3 / 2] newList = [math.sin(n) for n in oldList] print(newList)
[0.0, 1.0, -1.0]
""" Print graph paper to user specifications. """ import sys brows = int(input("How many rows of boxes? ")) bcols = int(input("How many columns of boxes? ")) srows = int(input("How many rows of spaces in each box? ")) scols = int(input("How many columns of spaces in each box? ")) for brow in range(brows): for bcol in range(bcols): print("+", end = "") for srow in range(scols): print("-", end = "") print("+") for srow in range(srows): for bcol in range(bcols): print("|", end = "") for srow in range(scols): print(" ", end = "") print("|") for bcol in range(bcols): print("+", end = "") for srow in range(scols): print("-", end = "") print("+") sys.exit(0)
""" Print graph paper to user specifications using string multiplication. """ import sys brows = int(input("How many rows of boxes? ")) bcols = int(input("How many columns of boxes? ")) srows = int(input("How many rows of spaces in each box? ")) scols = int(input("How many columns of spaces in each box? ")) dashes = "+" + scols * "-" spaces = "|" + scols * " " horizontalLine = bcols * dashes + "+\n" horizontalJail = bcols * spaces + "|\n" rowOfBoxes = horizontalLine + srows * horizontalJail graphPaper = brows * rowOfBoxes + horizontalLine print(graphPaper, end = "") sys.exit(0)
monkey2.py
in
Alternative.
Study
monkey1.py
and
monkey2.py
side by side.
Read the online documentation for the functions
open
and
urllib.request.urlopen
.
Write (and upload to your GitHub organization)
an interesting program that creates one or more
list
s
and loops through them.
An example would be the
list
in
lines
16–20
of
itflag.py
.
I corrected the instructions in
Binary
for displaying an image in jpg format,
including the code that converts the image from color to black and white.
Maybe you could do something interesting with the nested
for
loops in that code.
zip
example in
Sort.
Continue to play with
list
s.
The following code outputs two different lines on macOS and Linux. Does it output two different lines on Microsoft Windows?
import datetime d = datetime.date(2019, 12, 9) print(d.strftime("%A, %B %-d, %Y")) #on macOS: Monday, December 9, 2019 print(d.strftime("%A, %B %d, %Y")) #on macOS: Monday, December 09, 2019
Monday, December 9, 2019 Monday, December 09, 2019
Write (and upload to GitHub) an interesting Python program
with files,
date
s,
list
s,
list
comprehensions,
etc.
See the two
list
comprehensions
I retrofitted into
heat_equation.py
in the
homework answers
for May 9.
tkinter
GUI such as the one in the
Manhattan
address algorithm.
tkinter
GUI.
Date
,
but name it class
Time
and have it contain the attributes
hour
,
minute
,
second
.
Date
.
mypy
when you run a Python program?
Play with annotations.