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
Browse the buttons and menus
- You can insert cells above or below a selected cell
- You can execute a cell using the "play" button
- Or, press
ctrl-enter(VScode), orshift-enter(Jupyter lab/notebook), which is faster
- Or, press
- Cells can be dragged to different locations
- Buttons to Run All Cells, Restart the kernel, etc
VScode, Juypyter lab, etc., are editors for notebook files that are attached to independent, language-specific kernels that run the code. Notebooks can run code for many different languages.
Code cells¶
Code cells are where we write python code.
The code is the same syntax whether we are using VScode, Jupyter Lab, Jupyter Notebook, or running python using a text file and a Python interpreter.
Comments¶
- 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¶
- 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.
Examples
- Compare the two versions below for readability
In [ ]:
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)
In [ ]:
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)
Math operators¶
- Use the standard operators:
+ - / * - Use a double star to take a power:
2**4 - Use parentheses to group expressions
Indentation¶
Indentation matters in Python. Blocks of code have to have the same indentation level
- This usually shows up for
- conditional blocks (like
ifstatements) - loops
- functions
- classes
- conditional blocks (like
- This is good. Consistent indentation makes code more readable.
Examples
In [ ]:
T = 5
v_3 = 6 # this line won't work if indented
Notice how Python gives you an error, and tells you what is wrong.
In [ ]:
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")
Strings¶
- 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
printstatement
In [ ]:
pi = 3.14
Rg = 8314.46
me = "David"
print("Hi there,", me)
print("Rg =", Rg, "and pi =", pi)
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)
In [ ]:
print?
In [ ]:
help(print)
Continuation¶
- 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 [ ]:
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)
Cell order¶
O
- Code is run top to bottom in a given cell, and have values depending on the order cells are run in.
- Variables and functions need to be defined when used, not necessarily by order in the notebook.
- Variables and functions run in cells are visible and can be changed in other cells
- The numbers to the left of cells indicate the order the cells have been run in.
- Normally cells are run from top to bottom through the notebook
- If you get into trouble, clear the outputs and restart the kernel
In [1]:
a = 5
In [3]:
print(a,b)
5 6
In [2]:
b = 6