{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Goals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some goals for this exercise:\n", "\n", "* to reacquaint ourselves with Python\n", "* to start learning how to use a particular IPython notebook environment, one which is easy to jump right into, namely [Wakari](https://www.wakari.io/)\n", "* to learn a bit about the international context before diving into the US Census.\n", "* to get ourselves into looking at the Wikipedia as a data source.\n", "\n", "Thinking about populations of various geographic entities is a good place to start with open data. We can to work with numbers without necessarily involving complicated mathematics. Just addition if we're lucky. We can also think about geographical locations. We can build out from our initial explorations in a systematic manner.\n" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Things to Think About" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Off the top of your head:\n", " \n", " * What do you think is the current world population?\n", " * How many countries are there?\n", " * How many people are there in the USA? Canada? Mexico? Your favorite country?\n", " * What is the minimum number of countries to add up to 50% of the world's population? How about 90%?\n", " \n", "Now go answer these questions looking on the web. Find some a source or two or three." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Data Source for Populations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two open sources we'll consider:\n", "\n", "* [CIA World Factbook: Country Comparison Population](https://www.cia.gov/library/publications/the-world-factbook/rankorder/2119rank.html) (see also [The World Factbook: ABOUT :: COPYRIGHT AND CONTRIBUTORS](https://www.cia.gov/library/publications/the-world-factbook/docs/contributor_copyright.html))\n", "* [List of countries by population (United Nations) - Wikipedia, the free encyclopedia](https://en.wikipedia.org/w/index.php?title=List_of_countries_by_population_(United_Nations)) (see also [Wikipedia:Reusing Wikipedia content - Wikipedia, the free encyclopedia](https://en.wikipedia.org/wiki/Wikipedia:Reusing_Wikipedia_content)) -- btw, not the same as [List of countries by population - Wikipedia, the free encyclopedia](https://en.wikipedia.org/wiki/List_of_countries_by_population).\n", "\n", "We will study how to parse these data sources in a later exercise, but for this exercise, the data sets have been parsed into [JSON format](https://en.wikipedia.org/wiki/JSON), which is easily loadable in many languages, including Python using the [json Python standard library](http://docs.python.org/2/library/json.html). We'll also use [requests](http://docs.python-requests.org/en/latest/).\n", "\n", "Let's look first at the Wikipedia source." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# https://gist.github.com/rdhyee/8511607/raw/f16257434352916574473e63612fcea55a0c1b1c/population_of_countries.json\n", "# scraping of https://en.wikipedia.org/w/index.php?title=List_of_countries_by_population_(United_Nations)&oldid=590438477\n", "\n", "# read population in\n", "import json\n", "import requests\n", "\n", "pop_json_url = \"https://gist.github.com/rdhyee/8511607/raw/f16257434352916574473e63612fcea55a0c1b1c/population_of_countries.json\"\n", "pop_list= requests.get(pop_json_url).json()\n", "pop_list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "EXERCISES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show how to calculate the total population according to the list in the Wikipedia. (Answer: 7,162,119,434)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the total population of 196 entities that have a numeric rank. (Answer: 7,145,999,288) BTW, are those entities actually countries?" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the total population according to [The World Factbook: Country Comparison Population](https://www.cia.gov/library/publications/the-world-factbook/rankorder/2119rank.html) (See https://gist.github.com/rdhyee/8530164)." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "CHALLENGE EXERCISE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now for something more interesting. I'd like for us to get a feel of what it'd be like to pick a person completely at random from the world's population. Say, if you were picking 5 people -- where might these people be from? Of course, you won't be surprised to pick someone from China or India since those countries are so populous. But how likely will it be for someone from the USA to show up?\n", "\n", "To the end of answering this question, start thinking about writing a Python generator that will return the name of a country in which the probability of that country being returned is the proportion of the world's population represented by that country. \n", "\n", "Work with your neighbors -- we'll come back to this problem in detail in class on Thursday." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] } ], "metadata": {} } ] }