The Python for Everybody Specialization provided by UNIVERSITY OF MICHIGAN introduces fundamental programming concepts including data structures, networked application program interfaces, and databases, using the Python programming language. Python for Everybody is a completely open-source course, you can find all the notes and textbooks on its official website, so this note will only contain my solution to all post-lesson exercises in this course.
Chapter 1
Question 1
Write a program that uses a print statement to say ‘hello world’ as shown in ‘Desired Output’.
Desired Output
1 | hello world |
Solution
1 | print("hello world") |
Chapter 2
Question 1
Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data.
Desired Output
1 | Pay: 96.25 |
Solution
1 | hrs = input("Enter Hours: ") |
Chapter 3
Question 1
Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input - assume the user types numbers properly.
Desired Output
1 | 498.75 |
Solution
1 | hrs = input("Enter Hours:") |
Question 2
Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.
Desired Output
1 | B |
Solution
1 | score = input("Enter Score: ") |
Chapter 4
Question 1
Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function.
Desired Output
1 | Pay 498.75 |
Solution
1 | def computepay(h,r): |
Chapter 5
Question 1
Write a program that repeatedly prompts a user for integer numbers until the user enters ‘done’. Once ‘done’ is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below.
Desired Output
1 | Invalid input |
Solution
1 | largest = None |
Chapter 6
Question 1
Write code using find() and string slicing (see section 6.10) to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.
Desired Output
1 | 0.8475 |
Solution
1 | text = "X-DSPAM-Confidence: 0.8475"; |
Chapter 7
Question 1
Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:
1 | X-DSPAM-Confidence: 0.8475 |
Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution.
You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are testing below enter mbox-short.txt as the file name.
Desired Output
1 | Average spam confidence: 0.750718518519 |
Solution
1 | # Use the file name mbox-short.txt as the file name |
Chapter 8
Question 1
Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order.
You can download the sample data at http://www.py4e.com/code3/romeo.txt
Desired Output
1 | ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder'] |
Solution
1 | fname = input("Enter file name: ") |
Question 2
Open the file mbox-short.txt and read it line by line. When you find a line that starts with ‘From ‘ like the following line:
1 | From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 |
You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.
Hint: make sure not to include the lines that start with ‘From:’.
You can download the sample data at http://www.py4e.com/code3/mbox-short.txt
Desired Output
1 | stephen.marquard@uct.ac.za |
Solution
1 | fname = input("Enter file name: ") |
Chapter 9
Question 1
Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for ‘From ‘ lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender’s mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.
Desired Output
1 | cwen@iupui.edu 5 |
Solution
1 | fname = input("Enter file name: ") |
Chapter 10
Question 1
Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the ‘From ‘ line by finding the time and then splitting the string a second time using a colon.
1 | From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 |
Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.
Desired Output
1 | 04 3 |
Solution
1 | fname = input("Enter file name: ") |
Chapter 11
Question 1
Handling The Data
The basic outline of this problem is to read the file, look for integers using the re.findall(), looking for a regular expression of ‘[0-9]+’ and then converting the extracted strings to integers and summing up the integers.
Solution
1 | import re |
Chapter 12
Question 1
Exploring the HyperText Transport Protocol
You are to retrieve the following document using the HTTP protocol in a way that you can examine the HTTP Response headers.
- http://data.pr4e.org/intro-short.txt
There are three ways that you might retrieve this web page and look at the response headers: - Preferred: Modify the socket1.py program to retrieve the above URL and print out the headers and data. Make sure to change the code to retrieve the above URL - the values are different for each URL.
- Open the URL in a web browser with a developer console or FireBug and manually examine the headers that are returned.
- Use the telnet program as shown in lecture to retrieve the headers and content.
Desired Output
1 | HTTP/1.1 200 OK |
Solution
1 | import socket |
Question 2
Scraping Numbers from HTML using BeautifulSoup In this assignment you will write a Python program similar to http://www.py4e.com/code3/urllink2.py. The program will use urllib to read the HTML from the data files below, and parse the data, extracting numbers and compute the sum of the numbers in the file.
We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
- Sample data: http://py4e-data.dr-chuck.net/comments_42.html (Sum=2553)
- Actual data: http://py4e-data.dr-chuck.net/comments_501453.html (Sum ends with 35)
You do not need to save these files to your folder since your program will read the data directly from the URL. Note: Each student will have a distinct data url for the assignment - so only use your own data url for analysis.
Solution
1 | from urllib.request import urlopen |
Question 3
Following Links in Python
In this assignment you will write a Python program that expands on http://www.py4e.com/code3/urllinks.py. The program will use urllib to read the HTML from the data files below, extract the href= vaues from the anchor tags, scan for a tag that is in a particular position relative to the first name in the list, follow that link and repeat the process a number of times and report the last name you find.
We provide two files for this assignment. One is a sample file where we give you the name for your testing and the other is the actual data you need to process for the assignment
- Sample problem: Start at http://py4e-data.dr-chuck.net/known_by_Fikret.html
Find the link at position 3 (the first name is 1). Follow that link. Repeat this process 4 times. The answer is the last name that you retrieve.
Sequence of names: Fikret Montgomery Mhairade Butchi Anayah
Last name in sequence: Anayah - Actual problem: Start at: http://py4e-data.dr-chuck.net/known_by_Malaeka.html
Find the link at position 18 (the first name is 1). Follow that link. Repeat this process 7 times. The answer is the last name that you retrieve.
Hint: The first character of the name of the last page that you will load is: K
Solution
1 | import urllib.request |
Chapter 13
Question 1
Extracting Data from XML
In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/geoxml.py. The program will prompt for a URL, read the XML data from that URL using urllib and then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
- Sample data: http://py4e-data.dr-chuck.net/comments_42.xml (Sum=2553)
- Actual data: http://py4e-data.dr-chuck.net/comments_501455.xml (Sum ends with 78)
You do not need to save these files to your folder since your program will read the data directly from the URL. Note: Each student will have a distinct data url for the assignment - so only use your own data url for analysis.
Solution
1 | import urllib.request |
Question 2
Extracting Data from JSON
In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/json2.py. The program will prompt for a URL, read the JSON data from that URL using urllib and then parse and extract the comment counts from the JSON data, compute the sum of the numbers in the file and enter the sum below:
We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
- Sample data: http://py4e-data.dr-chuck.net/comments_42.json (Sum=2553)
- Actual data: http://py4e-data.dr-chuck.net/comments_501456.json (Sum ends with 42)
You do not need to save these files to your folder since your program will read the data directly from the URL. Note: Each student will have a distinct data url for the assignment - so only use your own data url for analysis.
Solution
1 | import urllib.request |
Question 3
Calling a JSON API
In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/geojson.py. The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
API End Points
To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:
1 | http://py4e-data.dr-chuck.net/json? |
This API uses the same parameter (address) as the Google API. This API also has no rate limit so you can test as often as you like. If you visit the URL with no parameters, you get “No address…” response.
To call the API, you need to include a key= parameter and provide the address that you are requesting as the address= parameter that is properly URL encoded using the urllib.parse.urlencode() function as shown in http://www.py4e.com/code3/geojson.py
Make sure to check that your code is using the API endpoint is as shown above. You will get different results from the geojson and json endpoints so make sure you are using the same end point as this autograder is using.
Solution
1 | import urllib.request |
Chapter 15
Question 1
create a SQLITE database or use an existing database and create a table in the database called “Ages”:
1 | CREATE TABLE Ages ( |
Then make sure the table is empty by deleting any rows that you previously inserted, and insert these rows and only these rows with the following commands:
1 | DELETE FROM Ages; |
Once the inserts are done, run the following SQL command:
1 | SELECT hex(name || age) AS X FROM Ages ORDER BY X |
Question 2
Counting Organizations
This application will read the mailbox data (mbox.txt) and count the number of email messages per organization (i.e. domain name of the email address) using a database with the following schema to maintain the counts.
1 | CREATE TABLE Counts (org TEXT, count INTEGER) |
When you have run the program on mbox.txt upload the resulting database file above for grading.
If you run the program multiple times in testing or with dfferent files, make sure to empty out the data before each run.
You can use this code as a starting point for your application: http://www.py4e.com/code3/emaildb.py.
The data file for this application is the same as in previous assignments: http://www.py4e.com/code3/mbox.txt.
Solution
1 | import sqlite3 |
Question 3
Musical Track Database
This application will read an iTunes export file in XML and produce a properly normalized database with this structure:
1 | CREATE TABLE Artist ( |
If you run the program multiple times in testing or with different files, make sure to empty out the data before each run.
You can use this code as a starting point for your application: http://www.py4e.com/code3/tracks.zip. The ZIP file contains the Library.xml file to be used for this assignment. You can export your own tracks from iTunes and create a database, but for the database that you turn in for this assignment, only use the Library.xml data that is provided.
To grade this assignment, the program will run a query like this on your uploaded database and look for the data it expects to see:
1 | SELECT Track.title, Artist.name, Album.title, Genre.name |
The expected result of the modified query on your database is: (shown here as a simple HTML table with titles)
Solution
1 | import xml.etree.ElementTree as ET |
Question 4
This application will read roster data in JSON format, parse the file, and then produce an SQLite database that contains a User, Course, and Member table and populate the tables from the data file.
You can base your solution on this code: http://www.py4e.com/code3/roster/roster.py - this code is incomplete as you need to modify the program to store the role column in the Member table to complete the assignment.
Each student gets their own file for the assignment. Download this file and save it as roster_data.json
. Move the downloaded file into the same folder as your roster.py
program.
Once you have made the necessary changes to the program and it has been run successfully reading the above JSON data, run the following SQL command:
1 | SELECT hex(User.name || Course.title || Member.role ) AS X FROM |
Find the first row in the resulting record set and enter the long string that looks like 53656C696E613333.
Solution
1 | import json |