Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lessons/Part1/00_workshop_setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Before the workshop, complete the following steps to make sure that you are ready to start with the material for Part 1!\n",
"#### Before the workshop, read and complete the following steps to make sure that you are ready to start with the material for Part 1!\n",
"\n",
"# How do I run Python on my machine?\n",
"\n",
"There are a few options for running Python: \n",
"\n",
"1) **Jupyter Notebook:** All files in this workshop are Jupyter notebooks with the extension `.ipynb`. The key feature of a Jupyter notebook is its organization around *executable cells*. Jupyter notebooks are the most common format you'll encounter with Python programming revolving around data processing and analysis. They can be initialized with Anaconda Navigator or via the command line.\n",
"**Recommended:**\n",
"- **Jupyter Notebook/JupyterLab:** All files in this workshop are Jupyter notebooks with the extension `.ipynb`. The key feature of a Jupyter notebook is its organization around *executable cells*. Jupyter notebooks are the most common format you'll encounter with Python programming revolving around data processing and analysis. Like a word document, Jupyter notebooks need to be opened in an appropriate app. Two most common options (Jupyter Notebook and JupyterLab) come installed with Anaconda (which you will install in the steps below). They can be opened with Anaconda Navigator or via the command line.\n",
"\n",
"2) **Google Colab:** Similar in layout to a Jupyter notebook. The key difference is that Colab is hosted on Google servers rather than being run locally on your machine. Colab notebooks have more support for parallel processing (useful for large datasets and complex models), and don't require a Python distribution on your machine. They're similarly in an `.ipynb` format.\n",
"**Next best options:**\n",
"- **DataHub:** A cloud-based service at UC Berkeley for interacting with Jupyter Notebooks. If you haven't been able to install Anaconda on your machine, or it runs too slowly, you can use DataHub to host your materials on the cloud. To do so, click on the Launch DataHub button in the Readme of the repository.\n",
"\n",
"3) **Spyder (or another [IDE](https://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments#Python)):** An integrated development environment (IDE) is a software application that provides a host of tools for debugging and development. They are usually used for running Python script files with extension `.py`, though newer IDEs are adding functionality for Jupyter notebooks. Spyder comes installed with the Anaconda distribution.\n",
"- **Binder:** Another cloud-based service for interacting with Jupyter Notebooks. This is like DataHub but a little slower. Use this alternative if you weren't able to complete the steps below, but don't have a UC Berkeley log-in.\n",
"\n",
"4) **Command Line:** It is also possible to run `.py` scripts directly in your terminal or command line (and even write the script in a text editor). This is not typically used for active development because it lacks the editor and tools of the above methods, but is sometimes used for executing scripts quickly.\n",
"\n",
"For this workshop, we will use the Anaconda distribution of Python and the included Jupyter Notebook application. Before the first workshop, complete the following steps to get the workshop materials ready.\n",
"**Other alternatives:**\n",
"\n",
"- **Google Colab:** Another interface for working with Jupyter notebooks. The key difference is that Colab is hosted on Google servers rather than being run locally on your machine. Colab notebooks have more support for parallel processing (useful for large datasets and complex models), and don't require a Python distribution on your machine. \n",
"\n",
"- **Spyder (or another [IDE](https://en.wikipedia.org/wiki/Comparison_of_integrated_development_environments#Python)):** An integrated development environment (IDE) is a software application that provides a host of tools for debugging and development. They are usually used for running Python script files with extension `.py`, though newer IDEs are adding functionality for Jupyter notebooks. Spyder comes installed with the Anaconda distribution.\n",
"\n",
"- **Command Line:** It is also possible to run `.py` scripts directly in your terminal or command line (and even write the script in a text editor). This is not typically used for active development because it lacks the editor and tools of the above methods, but is sometimes used for executing scripts quickly.\n",
"\n",
"For this workshop, we will use the Anaconda distribution of Python and the included Jupyter Notebook or JupyterLab application. Before the first workshop, complete the following steps to get the workshop materials ready.\n",
"\n",
"\n",
"## Step 1: Install Anaconda\n",
Expand Down Expand Up @@ -68,7 +77,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
24 changes: 18 additions & 6 deletions lessons/Part1/01_introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"\n",
"Most programmers can program in more than one language. That's because they know *how to program* generally, as opposed to \"knowing\" Python, R, Matlab, or any other language.\n",
"\n",
"In other words, programming is an extendible skill. Basic programming concepts - conditionals, for loops, functions - can be found in almost any programming language with small variations. Thus \"knowing how to program\" is not an exercise in memorization. \n",
"In other words, programming is an extendible skill. Basic programming concepts can be found in almost any language with small variations. Thus \"knowing how to program\" is not an exercise in memorization. \n",
"\n",
"Even within Python, there are too many packages and functions to memorize. Rather, a programmer knows 1) general structures and programming logic, 2) how to find and use new functions, and 3) how to work through problems that arise. \n",
"\n",
Expand All @@ -38,14 +38,19 @@
"1. State the goals of your code as clearly as possible.\n",
"2. Plan out the general logic of steps needed to achieve the goal.\n",
"3. Translate the steps into code:\n",
" a. Build up steps piece by piece.\n",
" b. Test frequently to make sure code is working as expected and handle bugs as quickly as possible.\n",
" 1. Build up steps piece by piece.\n",
" 2. Test frequently to make sure code is working as expected and handle bugs as quickly as possible.\n",
"4. Check the output.\n",
"\n",
"For each option, it is useful to predict what you think the output should look like, then compare that to the output of your code. These steps can help you code more effectively and make it easier to deal with issues that do come up, but there is still no way to entirely avoid bugs.\n",
"These steps can help you code more effectively and make it easier to deal with issues that do come up, but there is still no way to entirely avoid bugs. However, debugging is a learnable skill just like any other part of coding!\n",
"\n",
"## Debugging\n",
"\n",
"Here's a useful mental workflow to keep in mind when you want to try and debug an error:\n",
"There are two kinds of bugs in coding:\n",
"1. Those that give an error message. We will practice reading lots of error messages in this workshop but remember -- error messages are your friend!\n",
"2. Those that don't give an error message, but give an incorrect or nonsensical output. These are harder to find, but not impossible! \n",
"\n",
"It all comes down to understanding **where** the error occurs, and **why**, then you can make a plan for **how** to work around it. Here's a useful mental workflow to keep in mind when you want to try and debug an error:\n",
"\n",
"1. Read the errors!\n",
"2. Read the documentation.\n",
Expand All @@ -70,6 +75,13 @@
"\n",
"Don't reinvent the wheel - learning how to find the answer to the issues you run into is a critical part of becoming a capable programmer!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -88,7 +100,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.9.12"
}
},
"nbformat": 4,
Expand Down
88 changes: 62 additions & 26 deletions lessons/Part1/02_jupyter_notebooks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"\n",
"\n",
"**Learning Objectives**:\n",
"- Launch the Jupyter Notebook, create new notebooks, and exit the Notebook.\n",
"- Create and run Python cells in a notebook.\n",
"- Learn key Python formatting principles\n",
"- Create, open, and exit notebooks.\n",
"- Edit and run Python cells in a notebook.\n",
"- Learn key Python formatting principles.\n",
"* * * * *"
]
},
Expand All @@ -20,16 +20,26 @@
"source": [
"## Navigating Jupyter Notebooks\n",
"\n",
"In Jupyter Notebooks, code is typed into cells. Cells are individual units in which code can be separately run. In contrast to many IDEs, running code is done with a keyword combination: `Shift+Enter`. Running `Shift+Enter` on a selected cell will run the code in the cell and then automatically move your cursor to the following cell.\n",
"In Jupyter Notebooks, code is divided into cells which can each be run separately. This is the main distinction between Jupyter Notebook `.ipynb` format and Python script `.py` format. Running a cell is done with a key combination: `Shift+Enter`. `Shift+Enter` will run the code in the selecte cell and then automatically move to the following cell.\n",
"\n",
"Try to run the following code using `Shift+Enter` now."
"Try to run the following code using `Shift+Enter` now. \n",
"\n",
"**Question:** What was the output of the code?"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World!\n"
]
}
],
"source": [
"print(\"Hello World!\")"
]
Expand All @@ -38,16 +48,26 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If you hit **Enter** only, Jupyter Notebook gives you another line in the current cell.\n",
"If you hit `Enter` only, Jupyter Notebook gives you another line in the current cell.\n",
"\n",
"This allows you to compose multi-line commands and submit them to Python all at once."
"This allows you to compose multi-line commands and submit them to Python all at once.\n",
"\n",
"**Question:** What does the following cell do? What is the output?"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"a = 1 + 2\n",
"print(a)"
Expand All @@ -66,7 +86,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Try using `Control+Enter` to run this cell a few times. What happens?"
"**Question**: Try using `Control+Enter` to run this cell three times. What is the output? Run the cell one more time. Is the output the same?"
]
},
{
Expand All @@ -83,7 +103,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to create new empty cells, you can use Insert -> Insert Cell Below or use the Insert Cell Below button at the top of the notebook. Try entering a new cell below this one."
"If you want to create new empty cells, you can use Insert ==> Insert Cell Below or use the Insert Cell Below button at the top of the notebook. Try entering a new cell below this one."
]
},
{
Expand All @@ -92,11 +112,11 @@
"source": [
"## Markdown\n",
"\n",
"Jupyter notebooks allow you type in Markdown as well as code. In fact, this very cell is written in Markdown! We use markdown to narrate the workshop and provide context. Markdown is also used for documentation of code in Python notebooks more generally.\n",
"Jupyter notebooks allow you combine text and code using a system called markdown. In fact, this very cell is written in markdown! We use this formatting language to narrate the workshop and provide context. (Imagine reading this notebook with no markdown!) Markdown is also used for documentation of code in Python notebooks more generally.\n",
"\n",
"Markdown has its own syntax, but it's easy to learn. Here's a [cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) that can help.\n",
"Markdown has its own syntax, but it's fairly straighforward. Here's a [cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) that can help. You can also double-click on any of the markdown cells to see how they are made.\n",
"\n",
"Double click the cell below to see the markdown code rendering the output."
"Double click the cell below to see the markdown code rendering the output. Then do `Shift+Enter` to go back to the formatted text."
]
},
{
Expand All @@ -105,16 +125,24 @@
"source": [
"## Clearing Jupyter\n",
"\n",
"Jupyter remembers everything it executed, **even if it's not currently displayed in the notebook**.\n",
"Jupyter remembers line of code it executed, **even if it's not currently displayed in the notebook**. This means that deleting a line of code does not delete it from the notebook's memory if it has already been run. Instead, to clear everything from Jupyter use Kernel -> Restart in the menu. The kernel is basically the program actually running the code, so if you reset the kernel, it's as if you just opened up the notebook for the first time. All of the outputs are forgotten, and the variables are reset.\n",
"\n",
"To clear everything from Jupyter use Kernel -> Restart in the menu."
"Let's see how this actually works. First, run the cell below. What is the output?"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"And three shall be the count.\n"
]
}
],
"source": [
"mystring = \"And three shall be the count.\" \n",
"\n",
Expand All @@ -125,14 +153,22 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now use Kernel -> Restart in the menu! You can also press the \"Reset\" button in the icon bar."
"Now use Kernel -> Restart in the menu! You can also press the \"Reset\" button in the icon bar. Then run the code below. What happens?"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"And three shall be the count.\n"
]
}
],
"source": [
"print(mystring)"
]
Expand Down Expand Up @@ -185,11 +221,11 @@
"source": [
"### Commenting\n",
"\n",
"We will discuss how and why to comment code later in this series, but it's also useful when you temporarily don't want to run a section of code.\n",
"We will discuss how and why to comment code later in this series, but we will introduce it now because it's useful when you temporarily don't want to run a section of code.\n",
"\n",
"Simply place a pound sign `#` at the beginning of the line, and that line won't run. Any uncommented lines will be treated as code.\n",
"\n",
"Try running the cell below, then comment out `bad_thing`, and run it again.\n"
"Try running the cell below, then comment out `bad_thing`, and run it again. What changes?\n"
]
},
{
Expand Down Expand Up @@ -257,7 +293,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.9.12"
},
"toc": {
"base_numbering": 1,
Expand Down
Loading