The notebook is divided into cells of different types.
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.
shift-enter
, which is fasteralt-enter
, which is also handy.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).
#
sign.#
is ignored.$
or &
, etc.Examples
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).
+ - / *
2**4
Indentation matters in Python. Blocks of code have to have the same indentation level.
if
statements)Examples
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.
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
print
statementpi = 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
print?
in a cell (note the "?"). When you run the cell, function documentation appears at the bottom of the screen.help(print)
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
6.7)
11.15