forked from SocialComplexityLab/socialgraphs2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fc19d5
commit 1ced4fc
Showing
1 changed file
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,308 @@ | ||
{ | ||
"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://towardsdatascience.com/jypyter-notebook-shortcuts-bf0101a98330), or using the menu 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 https://learnpythonthehardway.org/python3/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](https://learnpythonthehardway.org/python3/). " | ||
] | ||
}, | ||
{ | ||
"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", | ||
"types_of_people = 10\n", | ||
"x = f\"There are {types_of_people} types of people.\"\n", | ||
"\n", | ||
"binary = \"binary\"\n", | ||
"do_not = \"don't\"\n", | ||
"y = f\"Those who know {binary} and those who {do_not}.\"\n", | ||
"\n", | ||
"print(x)\n", | ||
"print(y)\n", | ||
"\n", | ||
"print(f\"I said: {x}\")\n", | ||
"print(f\"I also said: '{y}'\")\n", | ||
"\n", | ||
"hilarious = False\n", | ||
"joke_evaluation = \"Isn't that joke so funny?! {}\"\n", | ||
"\n", | ||
"print(joke_evaluation.format(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/3/library/json.html). \n", | ||
"\n", | ||
"First use `urllib` (https://docs.python.org/3/library/urllib.html), or another Python library, to download **[this file](https://raw.githubusercontent.com/SocialComplexityLab/socialgraphs2021/main/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 years 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": [] | ||
} | ||
], | ||
"metadata": { | ||
"anaconda-cloud": {}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |