Running and Quitting
Overview
Teaching: 10 min
Exercises: 0 minQuestions
How can I run Python programs?
Objectives
Launch the Jupyter Notebook, create new notebooks, and exit the Notebook.
Create Markdown cells in a notebook.
Create and run Python cells in a notebook.
Python programs are plain text files.
- They have the
.py
extension to let everyone (including the operating system) know it is a Python program.- This is convention, not a requirement.
- It’s common to write them using a text editor but we are going to use the Jupyter Notebook to present the tutorials.
- The bit of extra setup is well worth it because the Notebook provides code completion and other helpful features.
- Notebook files have the extension
.ipynb
to distinguish them from plain-text Python programs.- Can export as “pure Python” to run from the command line.
Use the Jupyter Notebook for editing and running Python.
- The Anaconda package manager is an automated way to install the Jupyter notebook.
- See the setup instructions for Anaconda installation instructions.
- It also installs all the extra libraries it needs to run.
-
Once you have installed Python and the Jupyter Notebook requirements, open a shell and type:
$ jupyter notebook
- This will start a Jupyter Notebook server and open your default web browser.
- The server runs locally on your machine only and does not use an internet connection.
- The server sends messages to your browser.
- The server does the work and the web browser renders the notebook.
- You can type code into the browser and see the result when the web page talks to the server.
- This has several advantages:
- You can easily type, edit, and copy and paste blocks of code.
- Tab complete allows you to easily access the names of things you are using and learn more about them.
- It allows you to annotate your code with links, different sized text, bullets, etc. to make it more accessible to you and your collaborators.
- It allows you to display figures next to the code that produces them to tell a complete story of the analysis.
Spyder
You can also use a Python Integrated Development Environment
PyCharm
You can also use a Python Integrated Development Environment
Jupyter notebook
Math with numbers
- Addition
2+3
5
- Multiplication
2*2
4
- More complex
9.81*10.2**2/2
510.3162
- Power
3 ** 2
9
Math with strings (?)
- Concatenation
"cat " + "dog"
'cat dog'
- repetition
"cat "*3 + "dog"
'cat cat cat dog'
- subtraction
"cat " - "dog"
...
TypeError: unsupported operand type(s) for -: 'str' and 'str'
Operators
- Addition
6 + 7
13
- Multiplication
6 * 7
42
- Division
6 / 7
0.8571428571428571
- Power
6 ** 7 # power
279936
- Modulo
66 % 7 # Modulo (remainder)
3
Logic
- Less than
6 < 7
True
- less than or equal to
6 <= 7
True
- equal to
6 == 7
False
- greater than or equal to
6 >= 7
False
- greater than
6 > 7
False
- logical and
True and False
False
- logical or
True or False
True
- logical not
not False
True
Key Points
Python programs are plain text files.
Use the Jupyter Notebook for editing and running Python.
The Notebook has Command and Edit modes.
Use the keyboard and mouse to select and edit cells.
The Notebook will turn Markdown into pretty-printed documentation.
Markdown does most of what HTML does.