Python and Jupyter Basics

Notebook "cells"

The notebook is divided into cells of different types.

  • Code cells.
  • Markdown cells for formatting text and writing descriptions.
  • Raw cells

You can change the cell type using the first drop down menu above. There are also keyboard shortcuts

Browse the buttons and menus on the left. See especially the "command" pallet.

  • You can insert cells above or below a selected cell.
  • You can execute a cell using the "play" button above.
    • Or, you can press shift-enter, which is faster
  • You can execute a cell and then insert a cell below by pressing alt-enter, which is also handy.
  • Cells can be dragged to different locations.
  • Code in cells are "aware" of the results of previous cells that have been run, as if the whole document were one big Python script.
    • A given cell is aware of cells that are above or below if that above or below cell has already been run.
    • Sometimes you may have to re-execute things if you change cells above.
    • If in doubt, just go to the "Run" menu and select --> "Run All Cells"
  • You can restart with the "kernel" menu

Code cells

Code cells are where we write python code.

The code is the same syntax whether we are using the Jupyter Notebook, or running python using a text file and a Python interpreter (using Spyder, say, or Idle, or the command line).


  • Comments in Python start with a # sign.
  • Anything after the # is ignored.
  • Comments are good for explaining what you are doing, and showing units.
  • You should ALWAYS include descriptive comments in your code so that others will know what you are doing. Also, you will know what you did when you come back and try to read your own code days or weeks later.


  • Variables don't have to be declared (unlike many languages), just define them and go.
  • Variables start with letters
    • But then can include letters, numbers, or underscores.
    • Don't include crazy symbols like $ or &, etc.
  • Variables are case sensitive
  • Any variable that you declare with units MUST have a comment stating the units.
  • Line things up for clarity.
  • Add blank lines to break up sections.


In [14]:
T       = 298.15          # Temperature in (K)
v_2     = 15.0            # velocity at position 2 (m/s)
Pres    = 101325.0        # Pressure in (Pa)
mdot_in = 35.0            # kg/s
mDot_in = 15.0            # kg/s (this is not the same as mdot_in,)
                          #      (if you have variables that differ only)
                          #      (in their case, it will be confusing).

Math operators

  • Use the standard operators: + - / *
  • Use a double star to take a power: 2**4
  • Use parentheses to group expressions.


Indentation matters in Python. Blocks of code have to have the same indentation level.

  • This usually shows up for
    • conditional blocks (like if statements)
    • loops
    • functions
    • classes
  • This is good. Consistent indentation makes code more readable.


In [15]:
T = 5
    v_3 = 6         # this won't work, indentation is wrong
  File "<ipython-input-15-a2aca9f205f3>", line 2
    v_3 = 6         # this won't work, indentation is wrong
IndentationError: unexpected indent

Notice how Python gives you an error, and tells you what is wrong.

In [16]:
if v_2 > 2.2 :
    print("v_2 is > 2.2")        # this works, indented
    print("Another line")
print("This line is not indented, so is not part of the if statement")
v_2 is > 2.2
Another line
This line is not indented, so is not part of the if statement


  • Single and Double quotes are basically the same thing
    • Single quotes 'some string'
    • Double quotes "some string"
  • Triple strings are useful for a multi-line string: preserves new-lines and white space.
    • Use for documenting a function
    • Triple quotes """some string""" or '''some string'''

Printing results

  • The last statement entered in a cell gets output to the screen.
    • (unless you end with a semicolon)
  • To output other values, use the print statement
In [17]:
pi = 3.14
Rg = 8314.46
me = "David"
print("Hi there,", me)              # separate fields by commas
print("Rg =", Rg, "and pi =", pi)
Hi there, David
Rg = 8314.46 and pi = 3.14

Getting help

  • Type print? in a cell (note the "?"). When you run the cell, function documentation appears at the bottom of the screen.
  • You can also type help(print)


  • You can put more than one command on a line if you separate them with a semicolon ";"
  • A command can be spread over several lines using a backslash \ at the end of the continuation line.
    • Or you can use parentheses to force continuation to the ending parentheses.
In [18]:
a = 5; b = 6       # two commands on one line separated by ;

4.45 + \
6.7                # command runs onto next line with 

(4.45 +            # or use parentheses

Markdown Cells

Markdown is used for formatting text cells.

More on this later

In [ ]: