We introduced you to the Jupyter Notebook last time. Today builds on those basics, shows you the remaining basic features, and adds visualizations.
We also show how to load other notebooks effortlessly.
The main cell in a notebook is the code cell. We used these exclusively in the last lecture.
The other type of cell is the markdown cell. This lets you add text descriptions and images to your notebooks, creating a narrative/story to assist understanding of your notebook's code and its results. You type simple text with markdown characters like this:
# A little header ## A sub header
These then appear as nicely formatted HTML headers in your Notebook.
The markdown syntax is found here.
You can even include equations in LaTeX format, and they're visualized with the javascript MathJax library:
\begin{equation}
e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i
\end{equation}
Want to show a table? It's a little hacky, but you can do it!
| This | is | |------|------| | a | table|
You can click and drag cells to rearrange.
Use <shift>-enter to execute a cell.
Use the up/down arrow keys on your keyboard. Click on a cell once, and you can then move quickly using the arrows. Hit ENTER on any cell, and you enter 'edit' mode in that cell. Hit ESCAPE to get out of edit mode and use the arrows again. SHIFT-ENTER executes the current cell.
Save your example notebook with the normal .ipynb extension. Now open a text editor on your computer and open the .ipynb file. It's just plaintext! Not only that, it's JSON. Take a look and map it back to what you see in the notebook itself when loaded.
A notebook is just a plaintext file, and you can load someone else's notebook as easily as loading a python program. Notebooks are stored as .ipynb files, so you just open it like any other file in VS Code, and VS Code will recognize the format to display it in a notebook interface.
Try downloading and opening this S&P stock scraper
This notebook contains most of the topics we have covered, and mixes code cells with markdown cells. We will step through this notebook in class. The final code cells contain many of our recent "Python tricks" as well. You'll see great examples of list comprehension.
One gotcha to keep in mind: when you load a notebook, it contains markdown and code, but might also contain the output from a prior run of that code. You may see print results and visuals like graphs and charts. The catch here is that the variables which created those output are no longer initialized. You can't hit "play" on code cells in the middle of the notebook because the prior cells that created those variables haven't been run in your environment. Your python interpreter is obviously not the same as whoever created this notebook, so the session's state is lost. You see the result from a prior run, and the code that created it, but you have to re-run the entire notebook if you want to change something and regenerate the results. There is a "Run All" button in VSCode at the top of your notebook window.
You should see several histogram plots at the bottom of the notebook. This is showing the frequency of S&lP stock tickers that start with each letter of the alphabet. We show a few different views of the same information. Which is most natural to read? How do the code cells differ for each plot?
Now add your own plot to the end. Pull out just the vowels: A,E,I,O,U. Plot the vowels in that order.
Try it yourself, but here's a solution that uses list comprehension:
vowels = [ 'A', 'E', 'I', 'O', 'U' ]
nums = [ counts[vowel] for vowel in vowels ]
plt.bar(range(len(vowels)), nums, tick_label=vowels)
plt.show()