{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "
\n", "\n", "Created by [Nathan Kelber](http://nkelber.com) and Ted Lawless for [JSTOR Labs](https://labs.jstor.org/) under [Creative Commons CC BY License](https://creativecommons.org/licenses/by/4.0/)
\n", "For questions/comments/improvements, email nathan.kelber@ithaka.org.
\n", "___" ] }, { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Getting Started with Jupyter Notebooks\n", "\n", "\n", "**Description:** This lesson introduces [Jupyter notebooks](https://docs.constellate.org/key-terms/#jupyter-notebook) and [Python](https://docs.constellate.org/key-terms/#python) for absolute beginners. If you are completely new to text analysis, this is the place to start.\n", "\n", "**Use Case:** For Learners (Additional explanation, not ideal for researchers)\n", "\n", "**Difficulty:** Beginner\n", "\n", "**Completion time:** 15 minutes\n", "\n", "**Knowledge Required:** None\n", "\n", "**Knowledge Recommended:** None\n", "\n", "**Data Format:** None\n", "\n", "**Libraries Used:** `time` to demonstrate code cell execution\n", "\n", "**Research Pipeline:** None\n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![Getting Started with Jupyter Notebooks](https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/video/getting-started.png)](https://youtu.be/3jZYC9rGrNg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "Welcome to your first [Jupyter notebook](https://ithaka.github.io/tdm-notebooks/book/parts/reference/glossary.html#jupyter-notebook). [Jupyter notebooks](https://docs.constellate.org/key-terms/#jupyter-notebook) are documents that contain both computer code (like [Python](https://docs.constellate.org/key-terms/#python)) alongside explanatory images, figures, videos, and links. Most importantly, the code in a [Jupyter notebook](https://docs.constellate.org/key-terms/#jupyter-notebook) can be executed, modified, and deleted. As you explore this notebook, please feel free to modify the text, the code, and to generally play around with the environment. You can always [launch another instance of this notebook](https://docs.constellate.org/intro-to-jupyter-notebooks/) that will restore its original configuration. Later, you may learn how to create, modify, and save your own notebooks to share with others." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cells\n", "\n", "Similar to the way an essay is composed of paragraphs, Jupyter notebooks are composed of [cells](https://docs.constellate.org/key-terms/#cell). A [cell](https://docs.constellate.org/key-terms/#cell) is like a container for a particular kind of content. There are essentially two kinds of content in Jupyter notebooks:\n", "\n", "1. [Markdown Cells](https://docs.constellate.org/key-terms/#markdown-cell)- These can contain text, images, video, and other kinds of explanatory content you might find on a regular website. This cell is a markdown cell.\n", "2. [Code Cells](https://docs.constellate.org/key-terms/#code-cell)- These can contain code written in a variety of languages.\n", "\n", "A [code cell](https://docs.constellate.org/key-terms/#code-cell) can be distinguished from a [markdown cell](https://docs.constellate.org/key-terms/#markdown-cell) by the fact that it contains a pair of brackets with a colon to its left, like so ``[ ]:``" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This is a code cell " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A [markdown cell](https://docs.constellate.org/key-terms/#markdown-cell) provides information, but a [code cell](https://docs.constellate.org/key-terms/#code-cell) can be executed to perform an action. The [code cell](https://docs.constellate.org/key-terms/#code-cell) above does not contain any executable content, only a text comment. We can tell the text in the [code cell](https://docs.constellate.org/key-terms/#code-cell) is a comment because it is prefixed by a ``#``. In Python, any time a line is prefaced by a ``#`` that line is a comment and will not be executed if the code is run. In a [code cell](https://docs.constellate.org/key-terms/#code-cell), comments are also blueish-green in color." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hello World: Your First Code\n", "\n", "It is traditional in programming education to begin with a program that prints ``Hello World``. In Python, this is a simple task. We will use the ``print()`` function. This function simply prints out whatever is inside the parentheses (). We will pass the quotation \"Hello World\" to the print function like so:\n", "\n", "```print(\"Hello World\")```\n", "\n", "Write this code into the following [code cell](https://docs.constellate.org/key-terms/#code-cell) below. To execute our code, we have a couple options:\n", "\n", "#### Option One\n", "\n", "![Image of play button](https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/play_button.png) Click the code cell you wish to run and then push the \"Run\" button above. \n", "#### Option Two\n", "\n", "Click the [code cell](https://docs.constellate.org/key-terms/#code-cell) you wish to run and press Ctrl + Enter (Windows) or shift + return (OS X) on your keyboard.\n", "\n", "Type ```print(\"Hello World\")``` into the box below and then run the cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Try this!\n", "* Does it matter if you use single or double quotes?\n", "* Can you also insert a comment into the code cell?\n", "* Can you write code and a comment on a single line? Which must come first?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After your code runs, you'll receive any output and a number will appear in the pair of brackets `[ ]:` to the left of the code cell to show the order the cell was run. If your code is complicated or takes some time to execute, an asterisk * will be displayed in the pair of brackets `[*]:` while the code executes. \n", "\n", "Execute the code cell below which:\n", "\n", "1. Prints \"Waiting 5 seconds...\"\n", "2. Waits 5 seconds\n", "3. Prints \"Done\"\n", "\n", "As the program is running, watch the pair of brackets and you will see the code is running `[*]:`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Waiting 5 seconds...')\n", "import time\n", "time.sleep(5)\n", "print('Done')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you missed the asterisk, you can run the code cell as many times as you like. Notice that each time you run a [code cell](https://docs.constellate.org/key-terms/#code-cell) the number increases in the pair of brackets `[ ]:`. This keeps track of the order cells were run. While we will always run code in order from top to bottom, keep in mind that [code cells](https://docs.constellate.org/key-terms/#code-cell) can be run in any order. If you run a [code cell](https://docs.constellate.org/key-terms/#code-cell) at the bottom of a notebook that depends on the output of a [code cell](https://docs.constellate.org/key-terms/#code-cell) at the top, you will probably get an error. When you get an error, it's a good idea to check if you missed a [code cell](https://docs.constellate.org/key-terms/#code-cell) earlier that needed to be run first." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating a Cell\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![The + symbol to create a new cell](https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/new_cell.png)To create a new [cell](https://docs.constellate.org/key-terms/#cell), click the + at the top of the menu. A new [cell](https://docs.constellate.org/key-terms/#cell) will be created immediately underneath the currently selected [cell](https://docs.constellate.org/key-terms/#cell)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, a [code cell](https://docs.constellate.org/key-terms/#code-cell) is created. To change the cell type, click on the dropdown menu.\n", "![Change cell type menu](https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/change_code_cell.gif)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deleting a Cell\n", "\n", "![right clicking to delete cell](https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/delete_cell.gif)\n", "\n", "\n", "To delete a [cell](https://docs.constellate.org/key-terms/#cell), select the [cell](https://docs.constellate.org/key-terms/#cell) (or set of cells) and select \"Delete Cells\" from the \"Edit\" menu. (Alternatively, press the key \"d\" twice.)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modifying a Cell\n", "\n", "The text in [code cells](https://docs.constellate.org/key-terms/#code-cell) can be quickly changed like a regular textbox. In order to change the content of a [markdown cell](https://docs.constellate.org/key-terms/#markdown-cell), you need to expose the markdown content underneath by double-clicking the [cell](https://docs.constellate.org/key-terms/#cell). This will reveal the plain text of the markdown that creates various elements like headings, links, images, etc. When you want the cell to render again, you can simply run it again by pushing the play button or pressing Ctrl + Enter (Windows) or shift + return (OS X) on your keyboard." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is Markdown?\n", "\n", "If you are familiar with HTML, markdown is a simplified way to write HTML elements. Basically it allows you to mark out where headings, italics, bold, and other kinds of basic formatting go. In terms of styling, markdown is very minimalist. If you would like to include an element that is not included in markdown in your notebook, you can also use HTML and CSS in your [markdown cells](https://docs.constellate.org/key-terms/#markdown-cell)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How do I write my own Markdown?\n", "Here are some basic examples to get you started. Double-click on this cell to see how each was made. There are many markdown cheatsheets available on the web. It can be useful to [print one out](https://guides.github.com/pdfs/markdown-cheatsheet-online.pdf) and keep it handy.\n", "\n", "#### Headers\n", "\n", "|Markdown Syntax|Rendered Result|\n", "|---|---|\n", "|`# header title`| Largest header size |\n", "|`## header title`| Second largest header |\n", "|`### header title`| Third largest header |\n", "\n", "#### Emphasis\n", "\n", "*Use asterisks around texts to add emphasis, also known as italics*\n", "_You can also use underscores_\n", "~~A strike-thru effect is created with two tildes~~ ~~\n", "\n", "#### Lists\n", "\n", "A list of ordered items:\n", "1. List item 1\n", "2. List item 2\n", "\n", "Unordered items:\n", "* List item\n", "* Also a list item\n", "\n", "+ A list item\n", "+ Another list item\n", "\n", "- Also an item\n", "- The last item\n", "\n", "#### Links\n", "\n", "This is a link to [JSTOR](http://jstor.org). \n", "\n", "#### Images\n", "\n", "![Description of the image for accessibility(a jstor logo)](https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/logoJSTOR.png)\n", "\n", "#### Horizontal Rule\n", "\n", "Create a horizontal rule with three hyphens, asterisks, or underscores.\n", "____" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lesson Complete\n", "\n", "Congratulations! You have completed \"Getting Started with Jupyter Notebooks.\" If you have never programmed in [Python](https://docs.constellate.org/key-terms/#python) before, we recommend you complete:\n", "* *Python Basics* I\n", "* *Python Basics* II\n", "* *Python Basics* III\n", "\n", "### Start Next Lesson: [Python Basics I](./python-basics-1.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.10.12" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 4 }