1. Saving your work#
In MATLAB, you can save your work in a couple of ways:
Creating a script (
.m
file) or live script (.mlx
file).Using the
diary
command to log text inputs and outputs (no graphics).
But this is a Python notebook, so we’re not going to worry about any of that. Much like a live script, Jupyter notebooks are nice for reproducible science (and your homework) because all of the text, code, inputs, and outputs (including graphs) are in one place! We will demonstrate this by introducing some of the capabilities of Python.
Summary of commands#
In this exercise, we will demonstrate the following:
print()
- Function to display values.Primitive data types - Including
int
,float
, andstr
.
Python as a calculator#
You can directly type numerical expressions into a code cell and execute it to run the computation.
In the example below, the asterisk *
symbolizes scalar multiplication.
You might hear *
be called an operator.
60 * 60 * 24 * 365
31536000
Python even understands scientific notation using the _e_
notation, where the two blanks are the decimal and exponent.
1.3e5
130000.0
In addition to integers (int
) and floats (float
, decimals), another data type you will often encounter is strings (str
), which are literal expressions for text.
They can be enclosed in double "
or single '
quotes, but you have to be consistent.
"Hello, World!"
'Hello, World!'
Note that in the above two examples, the output automatically appeared when the cell was executed.
This will occur for the last line only and is sometimes nice, often unintentional.
To force display, you can use the print()
function, like so:
# comments start with a pound sign and aren't printed
print(3.1415)
print('CME 100')
3.1415
CME 100
print()
is very helpful for debugging!
Feel free to experiment on your own.