Here are some exercises for you to try on your own. Solutions are available at http://pragprog.com/titles/gwpy3/practical-programming.
In this exercise, you will create a table to store the population and land area of the Canadian provinces and territories according to the 2001 census. Our data is taken from http://www12.statcan.ca/english/census01/home/index.cfm.
Province/Territory |
Population |
Land Area |
---|---|---|
Newfoundland and Labrador |
512930 |
370501.69 |
Prince Edward Island |
135294 |
5684.39 |
Nova Scotia |
908007 |
52917.43 |
New Brunswick |
729498 |
71355.67 |
Quebec |
7237479 |
1357743.08 |
Ontario |
11410046 |
907655.59 |
Manitoba |
1119583 |
551937.87 |
Saskatchewan |
978933 |
586561.35 |
Alberta |
2974807 |
639987.12 |
British Columbia |
3907738 |
926492.48 |
Yukon Territory |
28674 |
474706.97 |
Northwest Territories |
37360 |
1141108.37 |
Nunavut |
26745 |
1925460.18 |
Write Python code that does the following:
Creates a new database called census.db
Makes a database table called Density that will hold the name of the province or territory (TEXT), the population (INTEGER), and the land area (REAL)
Inserts the data from Table 34, 2001 Canadian Census Data
Retrieves the contents of the table
Retrieves the populations
Retrieves the provinces that have populations of less than one million
Retrieves the provinces that have populations of less than one million or greater than five million
Retrieves the provinces that do not have populations of less than one million or greater than five million
Retrieves the populations of provinces that have a land area greater than 200,000 square kilometers
Retrieves the provinces along with their population densities (population divided by land area)
For this exercise, add a new table called Capitals to the database. Capitals has three columns—province/territory (TEXT), capital (TEXT), and population (INTEGER)—and it holds the data shown here:
Province/Territory |
Capital |
Population |
---|---|---|
Newfoundland and Labrador |
St. John’s |
172918 |
Prince Edward Island |
Charlottetown |
58358 |
Nova Scotia |
Halifax |
359183 |
New Brunswick |
Fredericton |
81346 |
Quebec |
Quebec City |
682757 |
Ontario |
Toronto |
4682897 |
Manitoba |
Winnipeg |
671274 |
Saskatchewan |
Regina |
192800 |
Alberta |
Edmonton |
937845 |
British Columbia |
Victoria |
311902 |
Yukon Territory |
Whitehorse |
21405 |
Northwest Territories |
Yellowknife |
16541 |
Nunavut |
Iqaluit |
5236 |
Write SQL queries that do the following:
Retrieve the contents of the table
Retrieve the populations of the provinces and capitals (in a list of tuples of the form [province population, capital population])
Retrieve the land area of the provinces whose capitals have populations greater than 100,000
Retrieve the provinces with land densities less than two people per square kilometer and capital city populations more than 500,000
Retrieve the total land area of Canada
Retrieve the average capital city population
Retrieve the lowest capital city population
Retrieve the highest province/territory population
Retrieve the provinces that have land densities within 0.5 persons per square kilometer of on another—have each pair of provinces reported only once
Write a Python program that creates a new database and executes the following SQL statements. How do the results of the SELECT statements differ from what you would expect Python itself to do? Why?
| CREATE TABLE Numbers(Val INTEGER) |
| INSERT INTO Numbers Values(1) |
| INSERT INTO Numbers Values(2) |
| SELECT * FROM Numbers WHERE 1/0 |
| SELECT * FROM Numbers WHERE 1/0 AND Val > 0 |
| SELECT * FROM Numbers WHERE Val > 0 AND 1/0 |