{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The python exercises\n", "\n", "**Ex 0**: The Jupyter Notebooks are organized as a list of cells. There are two central kinds of cells, they are called **Code** and **Markdown**. The cell type can be set [using a keyboard shortcut](https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/), or using the menus above.\n", "\n", "The **Code** cells simply contain the Python code, no big deal.\n", "\n", "The **Markdown** cells contain text (explanations, sections, etc). The text is written in Markdown. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML). You can read about it here:\n", "\n", "http://daringfireball.net/projects/markdown/\n", "\n", "In the cell below, write a short text that demonstrates that you can\n", "* Create a section\n", "* Write words in bold and italics\n", "* Create lists\n", "* Establish a [hyperlink](https://en.wikipedia.org/wiki/Hyperlink)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*[Write the answer to **Ex 0** here]*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 1**: Create a list `a` that contains the numbers from $1$ to $1110$, incremented by one, using the `range` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 2**: Show that you understand [slicing](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) in Python by extracting a list `b` with the numbers from $543$ to $779$ from the list created above." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 3**: Using `def`, define a function that takes as input a number $x$ and outputs the number multiplied by itself plus three $f(x) = x(x+3)$. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 4**: Apply this function to every element of the list `b` using a `for` loop. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 5**: Do the same thing using a list comprehension." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 6**: Write the output of your function to a text file with one number per line." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 7**: Show that you know about strings using this example from http://learnpythonthehardway.org/book/ex6.html (code reproduced below).\n", "\n", "1. Go through the code below and write a code comment above each line explaining it.\n", "1. Find all the places where a string is put inside a string. There are four places.\n", "1. Are you sure there are only four places? How do you know? Maybe I like lying.\n", "1. Explain why adding the two strings w and e with + makes a longer string.\n", "\n", "\n", "[**Hint**: If you feel this is too complex, try completing the prior learningthehardway exercises first. Start [here](http://learnpythonthehardway.org/book/ex1.html). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 10 types of people.\n", "Those who know binary and those who don't.\n", "I said: 'There are 10 types of people.'.\n", "I also said: 'Those who know binary and those who don't.'.\n", "Isn't that joke so funny?! False\n", "This is the left side of...a string with a right side.\n" ] } ], "source": [ "# Note from Sune: In Python, code comments follow the \"#\" character\n", "\n", "x = \"There are %d types of people.\" % 10\n", "binary = \"binary\"\n", "do_not = \"don't\"\n", "y = \"Those who know %s and those who %s.\" % (binary, do_not)\n", "\n", "print x\n", "print y\n", "\n", "print \"I said: %r.\" % x\n", "print \"I also said: '%s'.\" % y\n", "\n", "hilarious = False\n", "joke_evaluation = \"Isn't that joke so funny?! %r\"\n", "\n", "print joke_evaluation % hilarious\n", "\n", "w = \"This is the left side of...\"\n", "e = \"a string with a right side.\"\n", "\n", "print w + e" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*[Write the answer to **Ex 7**, 2-4 here]*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 8**: First, learn about JSON by reading the **[wikipedia page](https://en.wikipedia.org/wiki/JSON)**. Then answer the following two questions in the cell below. \n", "\n", "* What is `json`? What do the letters stand for?\n", "* Why is `json` superior to `xml`? (... or why not?)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*[Write your answer to **Ex 8** here]*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 9a**: Use the `json` module (instructions on usage here: https://docs.python.org/2.7/library/json.html). \n", "\n", "First use `urllib2` (https://docs.python.org/2.7/howto/urllib2.html), or another Python library, to download **[this file](https://raw.githubusercontent.com/suneman/socialgraphs2016/master/files/test.json)**. \n", "\n", "The downloaded file is a string when you first download it, but you can use the `json` library to \"load\" the string and decode it to a Python object, using `json.loads()`. (The decoded string is a python object, a list with a single element, a dictionary (with nested dictionaries inside it))." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 9b**: Now, let's take a look at the file you downloaded. First, just write the name of the variable that contains the decoded file content and hit enter to take a look at it. It's the list of Twitter Trending topics, a few days ago." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 9c**: The thing you've just decoded is now a list of length 1. What are the names of the keys organizing the dictionary at position 0 in the list? (Just write the code to produce the answer.) \n", "\n", "**Hint** use the `.keys()` method to easily get the keys of any dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 9d**: Two small questions \n", "* What time did I create the list of Trending Topics?\n", "* Print the names of the trending topics (bonus for using a list comprehension)\n", "\n", "(Just write the code to produce the answer.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 9e**: Two more small questions\n", "* Go on-line and figure out why there's a `u` in front of every string. Write the answer in your own words. (Hint it has to do with strings in Python).\n", "* What's going on with all of the `%22` and `%23`? Go on-line and figure it out. Write the answer in your own words. (Hint: It has to do with HTML URL encoding)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*[Write your answer to **Ex 9e** here]*" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 1 }