In the next week, we are going to get a “functional” start on Python, i.e. get just enough practice on essential actions so that we can cover later lessons. Only practice and time can make you comfortable and fast with Python.
You’ll get much more practice just going through classes as this semester proceeds, so when you are frustrated, or stuck, just remember to keep going!
Python is
One notable drawback: when your code creates outputs (figures, data, or tables) you must output it to new files. Enter...
Jupyter Notebooks (.ipynb
files)
{tip}
In fact, almost every page on this website is a Jupyter Notebook! Notice how there are headers, full text formatting, media inserts, and also code snippets and output. This means readers see output immediately after the relevant code and makes understanding code much easier.
Jupyter Lab
jupyter lab
and hit enterjupyter lab
and hit enterThe left sidebar has several menus (you can expand and collapse each by clicking on them)
In the main work area
In the folder you want the new file to be in,
Jupyter notebook uses a modal editing system.
This means that the effect of typing depends on which mode you are in. The two modes are
Enter
or double-clicking in the cell.ESC
{note}
The modal behavior of the Jupyter notebook is a little tricky at first but very efficient when you get used to it.
Watch this from 1:51 on. Try to repeat everything you see in that video in JupyterLab on your computer.
{tip}
You can speed up youtube videos to save time. Click the "gear" icon in the lower right of the video, then open "Playback speed" submenu.
{important}
I'm proficient enough with python to teach this class. But I have committed to memory VERY few the details about how to use most functions in python. When I want to do something, I often have to check the "proper syntax" for a function.
The [help and documentation](https://jakevdp.github.io/PythonDataScienceHandbook/01.01-help-and-documentation.html) section of the "Python Data Science Handbook" by Jake VanderPlas begins by saying:
> **If you read no other section in this chapter, read this one: I find the tools discussed here to be the most transformative contributions of Jupyter Lab to my daily workflow.**
>
> When a technologically-minded person is asked to help a friend, family member, or colleague with a computer problem, most of the time it's less a matter of knowing the answer as much as knowing how to quickly find an unknown answer. In data science it's the same: searchable web resources such as online documentation, mailing-list threads, and StackOverflow answers contain a wealth of information, even (especially?) if it is a topic you've found yourself searching before. ~~Being an effective practitioner of data science~~ Being efficient in many aspects of life these days is less about memorizing the tool or command you should use for every possible situation, and more about learning to effectively find the information you don't know, whether through a web search engine or another means.
>
> One of the most useful functions of Jupyter Lab is to shorten the gap between the user and the type of documentation and search that will help them do their work effectively. While web searches still play a role in answering complicated questions, an amazing amount of information can be found through Jupyter Lab alone.
{tip}
These are the most helpful commands and tricks you can deploy while you begin work in this class!
Description | How | Example |
---|---|---|
Syntax guides for functions |
shift + tab | type len( +shift + tab |
⭐ ⭐ Automatic if you finish the Jupyter Lab set up | type len( |
|
Sometimes fastest: ? | type len? then run the code |
|
⭐ ⭐ JLab's help menu at the top has links to official docs for Pandas, Python, Jupyter Lab | ||
Autocompletion suggestions | tab | type import pan + tab |
⭐ ⭐ Automatic if you finish the Jupyter Lab set up | type import pan |
|
A search bar for Jupyter Lab commands | cmd + shift + c | Type cmd + shift + c, then "move" |
{admonition}
:class: tip
A tip: In Jupyterlab, pressing <kbd>cmd </kbd> + <kbd> shift </kbd> + <kbd> C </kbd> brings up a search bar so you can search for commands you can run, and it will list shortcuts if available.
{note}
I use <kbd>CTRL</kbd> because I use Windows. Use <kbd>cmd </kbd> on Mac.
You can add extra features to Jupyter Lab by installing extensions. (You can see your installed extensions by clicking the "puzzle" icon on the left sidebar menu within JupyterLab.)
The extensions I have installed are below. Follow these instructions to install them.
extension | notes |
---|---|
jupyterlab_spellchecker | highlights non-words in Markdown cells
variableInspector | right click on a file, and click "Open Variable Inspector" to get a new pane that allows you to see inside of data objects
this is very useful
Note 1: be careful with large datasets
Note 2: It shows the data as it is when you open the viewer, so you have to reopen it to see any changes
jupyterlab-lsp | see the doc: code suggestions, some error detection, and help while writing code
jupyterlab_code_formatter | when I type ctrl + shift + F , this automatically formats my code cells
Students love this!
note: this formats code within a cell, but will not move code between cells
You'll see what these do as we go.
{note}
A common thing you'll want to do is copy chunks of lecture files into your own code (to work on problems, borrow code, etc.) so you don't have to retype big blocks of code.
I think this is the easiest way to copy parts of lecture files into your notes:
{admonition}
:class: tip
You can select a bunch of code blocks and drag them all at once. To do that, click to the left of a cell you want, hold the shift button, and then click the up or down arrow on your keyboard until all the cells you want are highlighted. Then do step 5 above.
Want another amateur youtube video? Here's one of me showing those steps!
(Students in prior years have not used these commands, but I'm leaving this here in case.)
Magic commands are commands in Jupyter Lab that add convenience on top of python functionality. A nice introduction to magics is here.
The most useful magic commands are
%pwd
- what is the working directory at this stage of the code?%ls
- what is in the working directory%timeit
and %%timeit
- times the code. %
times a single line of code, and %%
times the entire cell%conda
- runs the "conda" package manager within IPython (Conda installs, runs and updates packages)%debug
- starts the debugging interface%load
- loads a python file%%file
will save the cell (and just that cell) to a python file%timeit?
%lsmagic
and describe them all %magic
(although just going to the official documentation is probably better)(I don't use magic commands much, but those that do find them very helpful.)
This page borrows from the first chapter of the Python Data Science Handbook and QuantEcon.