Skip to content

Commit

Permalink
Intro lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rivera committed Sep 23, 2024
1 parent 26c59e4 commit 5fda456
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_build
node_modules
.ipynb_checkpoints/
4 changes: 4 additions & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
format: jb-book
root: intro
parts:
- caption: Python Lessons
chapters:
- file: lessons/write_run_python/write_run_python.md
- file: lessons/notebook/notebook.md
- caption: Teaching Python in Chemistry Meeting Blogs
chapters:
- file: state-of-teaching
Expand Down
Binary file added lessons/notebook/code_cell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions lessons/notebook/example_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0c977540",
"metadata": {},
"source": [
"# Example Notebook\n",
"This is as a Jupyter Notebook. Notebooks are separated into cells.\n",
"\n",
"This is a Markdown cell, which means I can write formatted.\n",
"\n",
"You write *italics like this*, **bold like this**, and `monospace like this`,\n",
"which is usually done to display code.\n",
"- Lists\n",
"- Like\n",
"- This\n",
"\n",
"1. Or\n",
"2. With\n",
"3. Numbers\n",
"\n",
"[Hyperlinks are like this](https://www.wikipedia.org/) and maths\n",
"like this $\\hat{H}\\Psi = E\\Psi$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a388f47",
"metadata": {},
"outputs": [],
"source": [
"# This is a Code cell, so you can write Python code\n",
"print(753+247)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a385bd8-ca7c-49f7-b218-6301fe335a1a",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added lessons/notebook/example_notebook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/notebook/jupyterlab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/notebook/markdown_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/notebook/navigator_jupyterlab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions lessons/notebook/notebook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Opening and Using a Jupyter Notebook
## Prerequisites
None.
## What is a Jupyter Notebook?
Some Python code is accompanied by text and images. This kind of code is stored in a file format called a _Jupyter Notebook_. You can recognise a Jupyter Notebook because the file name ends in `.ipynb`.
## How to open a Jupyter Notebook
If you don't already have a Jupyter Notebook file, you can download {Download}`this one<`example_notebook`.ipynb>`.
## Instructions
### Install Anaconda
Anaconda is a bundle of programs which can run and edit Python code. Check whether your computer has the program _Anaconda Navigator_. If not, **download Anaconda [here](https://www.anaconda.com/download/success).** If you are not sure which installer to download, choose the one from your operating system (Windows, Mac, or Linux), and select "Graphical Installer" if possible.

Once it is downloaded, **open the installer and follow the instructions**.

### Open JupyterLab
**Search for the program _Anaconda Navigator_ and open it**. After a few seconds, you should see a window like this appear:

![Window showing six logos for different programs. A red arrow points to the "Launch" button below the top right logo.](navigator_jupyterlab.png)

**Press the "Launch" under JupyterLab.** This will open a tab on your default web browser which should look like this:

![Window showing a folder navigation menu on the left and a Python 3 logo on the right.](jupyterlab.png)

Look at the address bar at the top of your browser. It should read `localhost` followed by some numbers. This means that you are not actually accessing the internet, despite being on a web browser. The page is generated locally by your computer, so you don't need an internet connection to work on your Jupyter Notebook.

**In the sidebar on the left, search through your folders for your `.ipynb` file and double-click on it.** If you just downloaded this file, it might be in your _Downloads_ folder. Your screen should now show this:

![Screenshot of a Jupyter Notebook.](example_notebook.png)

You have just opened the Jupyter Notebook.

## How to use a Jupyter Notebook
Jupyter Notebooks are split into horizontal segments called _cells_. You can see which cell is selected because it has a vertical blue bar on the left of it, as you can see in the screenshot above. If you click on another part of the notebook, you will select another cell.

There are two important kinds of cells: _Markdown_ and _Code_.

You can tell what kind a cell is by looking at the top ribbon:

![Top bar of the Jupyter Notebook with the word "Markdown" circled in red.](top_bar.png)

### Markdown cells
Markdown cells can contain formatted text and images. To change this content, **double-click on the cell**. This will show you the unformatted text, which has been written using a language called "Markdown":

![A box of raw Markdown text.](raw_markdown.png)

To make the text appear formatted again, make sure that the cell is selected, and **click on the "Run" button in the top ribbon** (Shortcut: Shift + Enter):

![Top bar of the Jupyter Notebook with the "run" triangle circled in red.](run_button.png)

### Code cells
Code cells contain programming code; in this case—Python. **Select the code cell, and run it, just like you ran the Markdown cell.** The result should look like this:

![Code cell showing "print(753+247)", and below, the result: 1000.](code_cell.png)

Observe how the output of the Python code (the number 1000) is printed below the cell. Additionally, a new empty code cell was created below, to let you continue writing code.

## Advice
Jupyter Notebooks are very good if you want someone else to use your code and your text in the same place. They also have a lot of options for being hosted online. Good uses for a Jupyter Notebook could be:
- A tutorial where you want to mix written instructions with runnable code.
- A data science report, where you generate figures along with the explanation for the origin of the data.
- A programme where you want the user to modify the source code, but they can't run Python on their own computer.

You should avoid using a notebook in these situations:
- You care about reproducibility: Notebook cells can be run in any order, and also deleted after running. All of this is information hidden from the user which could affect the results of the program.
- You are conducting a long-term project: Notebooks are not written in plain text (i.e. they don't look readable when opened in a notepad). Therefore, if you have multiple versions of one project, it's very hard to find exactly what the differences between versions are. In contrast, for plain text files, there are automatic tools for this.
- Your user doesn't need to see the source code: Notebooks always show you the original Python code which they are running. Usually, the user would prefer not to read this text before running a program because it is slow and pointless.

If you find yourself in any of these situations, you should prefer to write your code in a Python file, which is a plain text file ending in `.py`.
Binary file added lessons/notebook/raw_markdown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/notebook/run_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/notebook/top_bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/notebook/water.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/write_run_python/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/write_run_python/navigator_spyder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/write_run_python/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/write_run_python/run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/write_run_python/save.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/write_run_python/spyder_clean.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions lessons/write_run_python/write_run_python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Writing and Running a Python Program
## Prerequisites
None.
## What is a Python Program?
Python programs are sets of instructions that a computer can follow, written in a programming language called _Python_.

The Python programming language can be written on many platforms. The most common one is called a _Python program file_. This is a single plain text file ending with `.py`. You may also see them referred to as _scripts_ or _source code_. Once a Python program file is written, the user can ask the computer to follow the instructions within. This is called _running_ or _executing_ the program.

You can write such a file in any plain text editor (e.g. Notepad or TextEdit), but it is helpful to use an editor that can write and run your code in one same place. These are called _code editors_, or if they have more features, they are called an _Integrated Development Environment_ (IDE). For this lesson, we will use an IDE called _Spyder_.
# Instructions
## Install Anaconda
Anaconda is a bundle of programs which can run and edit Python code. Check whether your computer has the program _Anaconda Navigator_. If not, **download Anaconda [here](https://www.anaconda.com/download/success).** If you are not sure which installer to download, choose the one from your operating system (Windows, Mac, or Linux), and select "Graphical Installer" if possible.

Once it is downloaded, **open the installer and follow the instructions**.

## Open Spyder
**Search for the program _Anaconda Navigator_ and open it**. After a few seconds, you should see a window like this appear:

![Window showing six logos for different programs. A red arrow points to the "Launch" button below the Spyder logo.](navigator_spyder.png)

Scroll until you find the right button and **press the "Launch" under Spyder.** This will open window that should look this:

![Window titled "Spyder" separated into three panes and with a top ribbon with many options. There are three labels: A, B, and C, which respectively point to the large pane on the left, the play button on the top ribbon, and the bottom-right pane.](spyder_clean.png)

You may first see a pop-up suggesting to update Spyder. If so, click OK.

## Write a Python program
The left-hand pane is where you can write your program. **On line 8, paste the following text**:
```print("Hello, world!")```

It should look like this:

![Block of code with the line print("Hello, world!") written at the bottom.](hello_world.png)

This one-line program tells the computer to repeat the words "Hello, world!" to us in writing.

Next, **save the program by pressing the "Save" button in the top ribbon** (Shortcut: Ctrl + S):

![Top ribbon with the save button circled in red.](save.png)
## Run a Python program
To run the program, **press the "Play" button in the top ribbon** (Shortcut: F5):

![Top ribbon with the play button circled in red.](run.png)

Anything that the computer writes as a result of the program is called an _output_. Read your output in the bottom-right pane:

![Pane of text showing the words "Hello, world!" circled in red.](output.png)

# Advice
You should now be in a position to write Python programs, or open pre-existing ones using File-> Open (Shortcut: Ctrl+O) on Spyder. There are many other Python code editors that work very similarly, such as _Visual Studio Code_ or _PyCharm_.

Some Python code does not come in Python program files. The most popular alternative is the Jupyter Notebook, which Spyder is unable to open.

Some complex programming projects might involve multiple `.py` files which make reference to each other. In such cases, you may need those several files saved in the same folder for the program to run.

It is also possible to run Python programs without opening their source code, but the way to do this depends heavily on the kind of program it is and the computer it is running on.

0 comments on commit 5fda456

Please sign in to comment.