{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework 1 - Welcome to the class tools" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*by [Todd M. Gureckis](http://gureckislab.org/~gureckis)* and [Brenden M. Lake](https://cims.nyu.edu/~brenden/). Code shared under the [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) license. Credit given where inspiration was obtained from others!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The notebooks for Homework 1 are structured as follows:\n", "\n", "- This notebook gives an overview of the Jupyter Notebook. You will be using the Jupyter notebook environment to complete your homeworks and final projects. It includes an overview of writing formatted text including equations in the notebook. This is how you will help to organize/comment on your notebooks as well as complete the short answer questions on the homeworks.\n", "\n", "In the lab session you should have received a quick tutorial on getting an operating Jupyter notebook environment (either from the cloud based solution provided for the class or on your home computer/laptop). If you are having trouble getting started please get in touch with the TA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you go through these notebooks you will sometimes encounter colored cells. These provide helpful notes:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Assignment Cells
\n", " Green cells denote that you are being asked to perform some action or answer a question for the homework. We will be grading your responses the follow these types of cells so before turning in your homework make sure you have read and answered any thing that appears in a green cell.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Information cells
\n", "Blue cells denote helpful tips that we want to call attention to.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Warning!
\n", " Yellow cells denote warning to make sure you don't make a mistake or fail to load a necessary library.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Strong Warning!
\n", " Red cells provide even more of a warning. Make sure you read and understand these!!\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction to the Jupyter Notebook (part 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "This introduction to Jupyter Notebook is based on tutorials developed by Jessica Hamrick.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The notebook consists of a series of *cells*. For example, this text is in what is called a \"Markdown cell\". The following cell is a \"code cell\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# this is a code cell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can tell what the type of a cell is by selecting the cell, and looking at the toolbar at the top of the page. For example, try clicking on this cell. You should see the cell type menu displaying \"Markdown\", like this:\n", "\n", "![](images/cell-type-menu.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Command mode and edit mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the notebook, there are two modes: *edit mode* and *command mode*. By default the notebook begins in *command mode*. In order to edit a cell, you need to be in *edit mode*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "When you are in command mode, you can press enter to switch to edit mode. The outline of the cell you currently have selected will turn green, and a cursor will appear.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "When you are in edit mode, you can press escape to switch to command mode. The outline of the cell you currently have selected will turn gray, and the cursor will disappear.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Markdown cells\n", "\n", "For example, a markdown cell might look like this in **command mode** (Note: the following few cells are not actually cells -- they are images and just look like cells! This is for demonstration purposes only.)\n", "\n", "![](images/command-mode-markdown-rendered.png)\n", "\n", "Then, when you press enter, it will change to **edit mode**:\n", "\n", "![](images/edit-mode-markdown.png)\n", "\n", "Now, when we press escape, it will change back to **command mode**:\n", "\n", "![](images/command-mode-markdown-unrendered.png)\n", "\n", "However, you'll notice that the cell no longer looks like it did originally. This is because IPython will only *render* the markdown when you tell it to. To do this, we need to \"run\" the cell by pressing **`Ctrl-Enter`**, and then it will go back to looking like it did originally:\n", "\n", "![](images/command-mode-markdown-rendered.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "You will frequently use Markdown cells to give answers to homework questions so it is useful to learn a little bit of the Markdown syntax.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Code cells\n", "\n", "For code cells, it is pretty much the same thing. This is what a code cell looks like in command mode (again, the next few cells LOOK like cells, but are just images):\n", "\n", "![](images/command-mode-outline.png)\n", "\n", "If we press enter, it will change to **edit mode**:\n", "\n", "![](images/edit-mode-outline.png)\n", "\n", "And pressing escape will also go back to **command mode**:\n", "\n", "![](images/command-mode-outline.png)\n", "\n", "If we were to press **`Ctrl-Enter`** like we did for the markdown cell, this would actually *run* the code in the code cell:\n", "\n", "![](images/code-cell-run.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "At times the homeworks will require you to write or modify code using Python. This is a link to helpful tutorial for learning the basics of Python: http://openbookproject.net/thinkcs/python/english2e/ .\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Executing cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Code cells can contain any valid Python code in them. When you run the cell, the code is executed and any output is displayed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "You can execute cells with Ctrl-Enter (which will keep the cell selected), or Shift-Enter (which will select the next cell).\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try running the following cell and see what it prints out:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Printing cumulative sum from 1-10:\")\n", "total = 0\n", "for i in range(1, 11):\n", " total += i\n", " print(\"Sum of 1 to \" + str(i) + \" is: \" + str(total))\n", "print(\"Done printing numbers.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You'll notice that the output beneath the cell corresponds to the `print` statements in the code. Here is another example, which only prints out the final sum:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total = 0\n", "for i in range(1, 11):\n", " total += i\n", "print(total)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another way to print something out is to have that thing be the last line in the cell. For example, we could rewrite our example above to be:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total = 0\n", "for i in range(1, 11):\n", " total += i\n", "total" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, this *will not work* unless the thing to be displayed is on the last line. For example, if we wanted to print the total sum and then a message after that, this will not do what we want (it will only print \"Done computing total.\", and not the total sum itself)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total = 0\n", "for i in range(1, 11):\n", " total += i\n", "total\n", "print(\"Done computing total.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are accustomed to Python 2, note that the parentheses are obligatory for the `print` function in Python 3." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The IPython kernel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you first start a notebook, you are also starting what is called a *kernel*. This is a special program that runs in the background and executes code (by default, this is Python, but it could be other languages too, like R!). Whenever you run a code cell, you are telling the kernel to execute the code that is in the cell, and to print the output (if any).\n", "\n", "Just like if you were typing code at the Python interpreter, you need to make sure your variables are declared before you can use them. What will happen when you run the following cell? Try it and see:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The issue is that the variable `a` does not exist. Modify the cell above so that `a` is declared first (for example, you could set the value of `a` to 1 -- or pick whatever value you want). Once you have modified the above cell, you should be able to run the following cell (if you haven't modified the above cell, you'll get the same error!):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"The value of 'a' is: \" + str(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running the above cell should work, because `a` has now been declared. To see what variables have been declared, you can use the `%whos` command:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%whos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you ran the summing examples from the previous section, you'll notice that `total` and `i` are listed under the `%whos` command. That is because when we ran the code for those examples, they also modified the kernel state." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Note that commands beginning with a percent (%) or double percent (%%) are special IPython commands called *magics*. They will **only** work in IPython.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Restarting the kernel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is generally a good idea to periodically restart the kernel and start fresh, because you may be using some variables that you declared at some point, but at a later point deleted that declaration. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Your code should always be able to work if you run every cell in the notebook, in order, starting from a new kernel.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To test that your code can do this, first restart the kernel by clicking the restart button:\n", "\n", "![](images/restart-kernel-button.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, run all cells in the notebook in order by choosing **Cell$\\rightarrow$Run All** from the menu above." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "There are many keyboard shortcuts for the notebook. To see a full list of these, go to Help$\\rightarrow$Keyboard Shortcuts.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
To learn a little more about what things are what in the IPython Notebook, check out the user interface tour, which you can access by going to Help$\\rightarrow$User Interface Tour.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise: Getting your feet wet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 1 (5 points)

\n", "\n", "Implement the function hello and make sure the test cells runs without any errors. You will need to delete the line with `raise NotImplementedError`, write your own solution, and then re-run the cell before running the test cell. Each time you change code in a cell, you will need to re-run that cell before running any other cells that depend on it!\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def hello(name):\n", " \"\"\"Returns a message containing \"Hello, !\", \n", " where is passed in as an argument.\n", " \n", " Parameters\n", " ----------\n", " name : string\n", " The name of the person to say hello to\n", " \n", " Returns\n", " -------\n", " the message containing \"Hello, !\"\n", " \n", " \"\"\"\n", " raise NotImplementedError" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To test your function, you'll want to create a new cell to call it with a test input.\n", "To create a new cell, click on Insert$\\rightarrow$Insert Cell Below in the menubar above.\n", "Then, in your new cell, add the following code (or whatever code you want, really):\n", "\n", "```python\n", "hello(\"Lab in C&P students\")\n", "```\n", "\n", "Verify that when you run your new code cell, it prints out *Hello, Lab in C&P students!*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Markdown and LaTeX Cheatsheet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As mentioned above, Markdown is a special way of writing text in order to specify formatting, like whether text should be bold, italicized, etc.\n", "\n", "For the following few exercises, try to format the specified text using Markdown. You can use the following website as a reference: https://help.github.com/articles/markdown-basics\n", "\n", "- Hint #1: after editing the Markdown, you will need to run the cell so that the formatting appears.\n", "- Hint #2: try selecting *this* cell so you can see what the Markdown looks like when you're editing it. Remember to run the cell again to see what it looks like when it is formatted.\n", "\n", "Note that in this notebook, the formatted text and equations are actually images, so you cannot look at the cell in edit mode to see the answer. However, in all the other notebooks, I will write all the instructions and equations in Markdown, so in the future you will always be able to look at the cell in edit mode to see how the formatting is done." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Italics and Boldface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 2 (1 point)

\n", "Italicize the word \"Banana\", like this:\n", "
\n", "\n", "![](images/banana-italics.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 3 (1 point)

\n", "Bold the word \"Orange\", like this:\n", "
\n", "\n", "![](images/orange-bold.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 4 (1 point)

\n", "Make a bulleted list consisting of the words \"Apple\", \"Banana\", and \"Orange\", like this:\n", "
\n", "\n", "![](images/bulleted-list.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 5 (1 point)

\n", "Make a numbered list consisting of the words \"Strawberry\", \"Blueberry\", and \"Raspberry\", like this:\n", "
\n", "\n", "![](images/numbered-list.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 6 (1 point)

\n", "Make the following list of lists:\n", "
\n", "\n", "![](images/list-of-lists.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## LaTeX Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the advantages of using Markdown is that it allows us to easily write equations using LaTeX.\n", "\n", "The next few exercises will go over the basics of LaTeX equation formatting. You should be able to find all of the symbols you need on [this](https://observablehq.com/@s-haensch/latex-notation) page. Alternatively, if you forget the mathematical operation that a particular symbol corresponds to, check the [Wikipedia page](https://en.wikipedia.org/wiki/List_of_mathematical_symbols_by_subject) on mathematical notation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic equation formatting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 7 (1 point)

\n", "To format an equation using LaTeX, you must wrap the text in dollar signs, $like this$. Format \"y=ax+b\" to produce the following:\n", "
\n", "\n", "![](images/latex-basic-equation.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Subscripts and superscripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many equations require the use of exponents or underscores. To include a superscript, use the `^` symbol, and for subscripts, use the `_` symbol.\n", "
\n", "

Problem 8 (1 point)

\n", "Format the following equation:\n", "
\n", "\n", "![](images/latex-subscripts-and-superscripts.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes, we need to use exponents or underscores that require more than one character. Unfortunately, by default, the `^` or `_` symbols only apply to the *next* character. To tell them to apply to multiple characters, the exponent or subscript needs to be wrapped in curly braces.\n", "\n", "
\n", "

Problem 9 (1 point)

\n", "Format the following equation:\n", "
\n", "\n", "![](images/latex-curly-braces.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Greek letters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We may ask you to use Greek variable names or letters in some of the assignments for this course. In general, greek letters can be formatted in LaTeX with `\\letter`, where \"letter\" is the name of the greek letter. For the lowercase version, use `\\letter`, and for the uppercase version, use `\\Letter`. For example, to format the greek letter \"delta\", you would do `\\delta` to produce $\\delta$ and `\\Delta` to produce $\\Delta$.\n", "\n", "
\n", "

Problem 10 (1 point)

\n", "Format the following equation, which uses several common greek variables (note: the only letters that are *not* greek are the numbers and the \"m\" and \"n\" subscripts).\n", "
\n", "\n", "![](images/latex-greek-variables.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For reference, the greek letters used in this equation are called:\n", "![](images/latex-greek-variable-names.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sums and products" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sums and products across variables can be displayed using `\\sum` and `\\prod`. To get the variables above and below the sum or product symbols, you can use superscripts and subscripts.\n", "\n", "As a reminder, the large sum and product symbols mean the sum or product of a series of numbers or variables. Usually, the index variable along with its starting value is defined underneath the sum/product symbol (e.g. $i=1$), and its final value is given above the sum/product. For example, the following is a sum of all $x_i$ where $1\\leq i\\leq N$:\n", "\n", "![](images/latex-example-sum.png)\n", "\n", "and similarly, the following is a product of all $y_j$ where $1\\leq j\\leq M$:\n", "![](images/latex-example-product.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 11 (1 point)

\n", "Format the following equation using double dollar signs (instead of single dollar signs like above). This will always make the equation display on a new line, and will make symbols like sums and products look nicer.\n", "
\n", "\n", "![](images/latex-sum-product.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Putting it all together" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Problem 12 (1 point)

\n", "Format the following equation (using double dollar signs):\n", "
\n", "\n", "![](images/latex-challenge.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Turning in homeworks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you are finished with this notebook. Save your work in order to turn it in. To do this select *File*->*Download As...*->*PDF*.\n", "\n", "\n", "\n", "You can turn in your assignments in Gradescope (will be described in class)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernel_info": { "name": "python3" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9" }, "nteract": { "version": "0.15.0" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }