Welcome to Modeling and Simulation, welcome to Python, and welcome to Jupyter. This is a Jupyter notebook, which is a development environment where you can write and run Python code. There's a complete tutorial on how to use these notebooks below (Quick notebook tutorial).
How this code is structured¶
There's one directory per chapter (
Chapter 2, etc). Inside each directory you'll find the working notebook along with a copy that includes the solutions to the exercises. Here's a summary:
- Chapter 1: Modeling
- Chapter 2: Bike share
- Chapter 3: Iterative Modeling
- Chapter 4: Sweeping parameters
- Chapter 5: World Population
Along with each chapter's code, we include a directory with code utilities under
code_utils. That code is available under each Chapter.
Welcome to Modeling and Simulation, welcome to Python, and welcome to Jupyter.
This is a Jupyter notebook, which is a development environment where you can write and run Python code. Each notebook is divided into cells. Each cell contains either text (like this cell) or Python code.
Selecting and running cells¶
To select a cell, click in the left margin next to the cell. You should see a blue frame surrounding the selected cell.
To edit a code cell, click inside the cell. You should see a green frame around the selected cell, and you should see a cursor inside the cell.
To edit a text cell, double-click inside the cell. Again, you should see a green frame around the selected cell, and you should see a cursor inside the cell.
To run a cell, hold down SHIFT and press ENTER.
If you run a text cell, Jupyter typesets the text and displays the result.
If you run a code cell, it runs the Python code in the cell and displays the result, if any.
To try it out, edit this cell, change some of the text, and then press SHIFT-ENTER to run it.
Adding and removing cells¶
You can add and remove cells from a notebook using the buttons in the toolbar and the items in the menu, both of which you should see at the top of this notebook.
Try the following exercises:
From the Insert menu select "Insert cell below" to add a cell below this one. By default, you get a code cell, as you can see in the pulldown menu that says "Code".
In the new cell, add a print statement like
print('Hello'), and run it.
Add another cell, select the new cell, and then click on the pulldown menu that says "Code" and select "Markdown". This makes the new cell a text cell.
In the new cell, type some text, and then run it.
Use the arrow buttons in the toolbar to move cells up and down.
Use the cut, copy, and paste buttons to delete, add, and move cells.
As you make changes, Jupyter saves your notebook automatically, but if you want to make sure, you can press the save button, which looks like a floppy disk from the 1990s.
Finally, when you are done with a notebook, select "Close and Halt" from the File menu.
Using the notebooks¶
The notebooks for each chapter contain the code from the chapter along with addition examples, explanatory text, and exercises. I recommend you
- Read the chapter first to understand the concepts and vocabulary,
- Run the notebook to review what you learned and see it in action, and then
- Attempt the exercises.
If you try to work through the notebooks without reading the book, you're gonna have a bad time. The notebooks contain some explanatory text, but it is probably not enough to make sense if you have not read the book. If you are working through a notebook and you get stuck, you might want to re-read (or read!) the corresponding section of the book.
The following cell imports
modsim, which is a collection of functions we will use throughout the book. Whenever you start the notebook, you will have to run the following cell. It does three things:
It uses a Jupyter "magic command" to specify whether figures should appear in the notebook, or pop up in a new window.
It configures Jupyter to display some values that would otherwise be invisible.
It imports everything defined in
Select the following cell and press SHIFT-ENTER to run it.
# Configure Jupyter so figures appear in the notebook %matplotlib inline # Configure Jupyter to display the assigned value after an assignment %config InteractiveShell.ast_node_interactivity='last_expr_or_assign' # import functions from the modsim library from modsim import * print('If this cell runs successfully, it produces no output other than this message.')
The first time you run this on a new installation of Python, it might produce a warning message in pink. That's probably ok, but if you get a message that says
modsim.py depends on Python 3.7 features, that means you have an older version of Python, and some features in
modsim.py won't work correctly.
If you need a newer version of Python, I recommend installing Anaconda. You'll find more information in the preface of the book.
Restart and run all¶
When you change the contents of a cell, you have to run it again for those changes to have an effect. If you forget to do that, the results can be confusing, because the code you are looking at is not the code you ran.
If you ever lose track of which cells have run, and in what order, you should go to the Kernel menu and select "Restart & Run All". Restarting the kernel means that all of your variables get deleted, and running all the cells means all of your code will run again, in the right order.
Exercise: Select "Restart & Run All" now and confirm that it does what you want.