Python WS19PB02 Homework

  1. Wednesday, April 24, 2019: up to the end of Standard output in the in-class examples. Install Python 3 and IDLE on your Mac or PC. Try out everything we did in class and see if it actually works as advertised. (Do this each day.) Don’t worry too much about 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.

    1. In the upper right corner of your GitHub page, click your profile photo, then click Settings.
    2. In your Personal Settings sidebar on the left, click on Organizations.
    3. In the upper right corner, press the “New organization” button.
    4. Name the organization 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.
    5. Press the green Continue button. Do not be alarmed if GitHub changed the name of your organization to all lowercase. If you want, you can then take their survey on the Organization details page. For example, tell them that your organization is for educational purposes. Or you can click “skip this step” at the bottom of the page. Then you can sign out from GitHub and admire the other students’ accounts.

    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):

    #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
    
  2. Thursday, April 25, 2019: up to the next-to-last section in Standard Input. See EAFP vs. LBYL. Write a Python program that does something interesting. Create a repository for it in your GitHub organization, and store the python program in the repository. (See the instructions in the next-to-last section in Standard Input.) Then admire the other students’s work.
  3. Monday, April 29, 2019: up to the leap year example in 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.
  4. Wednesday, May 1, 2019: up to exercise 3 in Pi. Install NumPy and matplotlib on your computer at home. Then run
    pip3 list
    or
    pip3 list --verbose
    to see if you have any other modules installed. Hand in the graph paper exercise in For, or something even more interesting with loops (while and/or for) and if statements. It’s okay if you want to produce output in HTML format instead of plain text.
  5. Thursday, May 2, 2019: up to Pig Latin, ¶ 6. Write a program that imports 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)
    
  6. Monday, May 6, 2019: up to 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 lists 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.
  7. Wednesday, May 8, 2019: up to the zip example in Sort. Continue to play with lists.
  8. Thursday, May 9, 2019: up to CVS. Be prepared to tell the class if you found any interesting databases at NYC OpenData. Join Slack and then go to BMCC Python Bootcamp so we can exchange Python programs in class.

    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, dates, lists, list comprehensions, etc. See the two list comprehensions I retrofitted into heat_equation.py in the homework answers for May 9.

  9. Monday, May 13, 2019: up to Roman.html. Write (and, as usual, upload to your GitHub organization) a Python program that reads one of the NYC OpenData databases and does something interesting (i.e., prints all or part of the information in the database). Write a program that examines a tic-tac-toe board and tells if someone has already won.
  10. Wednesday, May 15, 2019: up to Street trees. Study how the output of this Python program was display in a Google map, and then do something similar.
  11. Thursday, May 16, 2019: up to two parallel columns. Write (and, as usual, upload to your GitHib organization) a Python program with a tkinter GUI such as the one in the Manhattan address algorithm.
  12. Monday, May 20, 2019: up to Download JSON. Write (and upload to your GitHub organization) a Python program that downloads the date, times of sunrise and sunset, current temperature, humidity, icon, etc. from OpenWeatherMap, and displays these pieces of information in a tkinter GUI.
  13. Wednesday, May 22, 2019: up to Intersect.
  14. Thursday, May 23, 2019: up to Variable keyword. Work on all your overdue homework. No class on Monday, May 27 (Memorial Day).
  15. Wednesday, May 29, 2019: up to Class. Do all the exercises in Class. Then hand in a class like class Date, but name it class Time and have it contain the attributes hour, minute, second.
  16. Thursday, May 30, 2019: up to unit tests for class Date.
  17. Monday, June 3, 2019: up to Generators. Create an iterator like the ones in the last three exercises in Iterator.
  18. Wednesday, June 5, 2019: up to Getitem. Create a generator function. Create a generator expression.
  19. Thursday, June 6, 2019: up to LeapDate. Write a base class and a derived class (i.e., a superclass and a subclass). Create objects of these two classes to demonstrate that the derived class object can do everything that the base class object can do, plus more.
  20. Monday, June 10, 2019: up to pdfminer. Can you find more examples of inheritance in the Python Standard Library? Can you get IDLE to automatically run mypy when you run a Python program? Play with annotations.
  21. Wednesday, June 12, 2019: finished Twitter and Life.
  22. Thursday, June 13, 2019: up to the June, 2019 in Regular Expressions. Find a phone number that has no 0 or 1. Does the phone number spell any word or pair of words?
  23. Monday, June 17, 2019: up to Groups.
  24. Wednesday, June 19, 2019: up to Beautiful Soup. Hand in the phone number homework in Substitute. Does compiling a regular expression make it run faster?
  25. Thursday, June 20, 2019: up to Maze. I fixed the bug in Quicksort (“Sort the numbers in increasing order of how far they are from 1900”).
  26. Monday, June 24, 2019