Intro to Interactive Python

When you code in Python, you end end up writing lines of code across lots of different files. It is very common for larger Python projects to have thousands of lines of code. But when you are first starting to code, it is ideal to write some lines of code and then see what they do instantly. This experience is interactive and is ideal for learning and playing around with Python.

There are couple of ways to start interactive sessions with Python. For example, if you have Python installed on your computer you can open up a terminal/shell and just type python or python3. Here is an example of what that looks like. python3_terminal

Each line starts with >>> and you can simply type some python code and then hit return on your keyboard to execute it. Here I typed 1 + 1 and then hit return. Then the code was executed instantly and the output was displayed on the screen, 2.

Try it out on your own! Open a terminal/shell and type python3. Then try adding two numbers together. You can type quit() and then return to exit the interactive python shell in the terminal.

Another way to work in python in a very rich interactive environment is by using Jupyter Notebooks. If you have Jupyter installed you can just open a terminal/shell on your computer and type jupyter notebook and hit return on the keyboard.

jupyter_notebook_terminal

You can copy and paste the link http://localhost:8889/?token=d844c610f304b048803cb24f809ece636c126c0dce446800 in your web browser. Or it might even open automatically. In the web browser you will see something like this: jupyter_notebook_home

You can click on the button new on the right and then click on Python 3 as shown here.

jupyter_note_book_python3

This will open up a blank Untitled Jupyter Notebook (also called Ipython Notebook).

blank_notebook

You can change the name of the notebook file by clicking on the title at the top and typing a new name.

jupyter_title

This will open up a window like this. Type the new name and then click Rename.

jupyter_title_rename

A notebook is made of individual cells in which you can type python code in. You can add more cells by clicking on the + sign on the left beside the save button. Here I clicked it 5 times which created 5 new cells.

five_new_cells

Let’s start to learn some python! Click on the first cell and type the following code in the first cell. After typing your code in a cell you can hit shift plus return on your keyboard to execute the code in that cell. Or you can click the button Run on the top tool bar but it’s better to use the keyboard shortcuts.

1 + 1
2

Trying creating some more cells and typing some mathematical expressions in them. Don’t forget to execute the code. In Python we can use the following symbols for addition, subtraction, multiplication, and division:

  • + for addition

  • - for subtraction

  • * for multiplication (shift + 8 on the keyboard)

  • / for division (the forward slash on the key with the ? symbol)

1 + 2 + 3 + 4 + 5
15
105 - 100
5
6 / 3
2.0
9 * 9
81
5 + 9 * 9
86
5 + 9 * 9 - 100
-14
3 * 3 + 6 - 9/3
12.0
4/5
0.8
5/4
1.25

Notice that Python obeys the standard mathematical order of operations. That is, multiplication and division come before addition and subtraction.

For example the proper way to do,

\[2 + 3 × 4\]

is to do

\[2 + (3 × 4)\]

which is

\[2 + 12\]

which is

\[14\]

The wrong order of operations would be to first calculate \(2+3\) which is \(5\) and then do \(5 × 4\) to get the incorrect answer \(20\). Python executes the correct order of operations.

2 + 3 * 4
14

When writing complicated mathematical expressions in Python code it’s really useful to make use of brackets. Just like you do when writing math on paper. For example you might work out this on paper as:

\[(10 + (5 × 5) - (50 / 5)) × 2\]
\[(10 + 25 - 10) × 2\]
\[(35 - 10) × 2\]
\[25 × 2\]
\[50\]

In Python, to work out this exact same math problem you must you brackets.

(10 + (5 * 5) - (50 / 5)) * 2
50.0