Jupyter

From PrattWiki
Revision as of 03:36, 13 January 2023 by DukeEgr93 (talk | contribs) (Tutorial)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page is meant to be a startup guide for using Jupyter Notebooks with Python. It assumes you have installed Anaconda from https://www.anaconda.com/. Most of this guide was written running Python 3.9 and Jupyter Notebooks 6.4.12.

Starting Up

  • To start Jupyter Notebooks with Anaconda:
    • On Windows, go to the Anaconda folder in the Start Menu or open the Anaconda Navigator and start Jupyter Notebooks from there.
    • On macOS, open the Anaconda Navigator and start Jupyter Notebooks from there.

Depending on your settings, you may get a new browser that points to your localhost or you may get a window with a web address that you need to copy and paste into a web browser (in which case, do that). In either case, the end result should be a web page open to the jupyter page with tabs for Files, Running, and Clusters.

Tutorial

There's a great tutorial at https://www.dataquest.io/blog/jupyter-notebook-tutorial/! You do not need to sign in or click the Start Free button to follow the tutorial.

A few notes:

  • What is a Jupyter Notebook?
    • Nothing to add
  • How to Follow This Tutorial
    • Nothing to add
  • Installation
    • You can skip this if you already have Anaconda and have already started Jupyter Notebook in a browser.
  • Creating Your First Notebook
    • You should already be at the Running Jupyter phase.
    • The New-> Python 3 might look like New->Python 3 (ipykernel)
    • In the Cells part:
      • CTRL-Enter or the $$\blacktriangleright\!\shortmid$$ runs the current cell; SHIFT-Enter runs the current cell and provides a new empty cell below it. SHIFT-Enter is generally the way to go as it runs the cell and gives you a new input line (rather than having to insert a new one)
      • ESC and ENTER toggle between command mode and edit mode. In edit mode, there is a pencil icon at the top right; in command mode, there isn't. Also, if you click in the edit part of a cell you enter edit mode; if you click in the space between the In [ ]: and the $$\blacktriangleright\!\shortmid$$ you enter command mode.
    • In the Markdown part:
      • In addition to the commands shown, Markdown understands basic LaTeX (Greek letters, fractions, integrals, etc). Use single $ around commands for inline and $$ around commands for displaymath.
    • In the Kernels part:
      • For the commands that print formatted strings, the tutorial uses the string modulo method. To relate this to using format, and also to using the new (as of Python 3.6) f-string, here are three ways of printing the same information:
        a = 2
        b = 4
        c = 2.5
        d = 6.25
        # string modulo
        print('%d squared is %d and %0.2e squared is %0.2e' % (a, b, c, d))
        # format
        print('{:d} squared is {:d} and {:0.2e} squared is {:0.2e}'.format(a, b, c, d))
        # f-string
        print(f'{a:d} squared is {b:d} and {c:0.2e} squared is {d:0.2e}')
        
        You can see that all three are similar; the f-string puts the variable at the same location it will end up printing in the string rather than way at the end.
  • Example Analysis
    • Starting with the "Setup" section, the page starts to go into some advanced data analysis with Pandas; they always give you the code, but it may be confusing! You can stop here in terms of understanding what you need to know for the class. If you choose to continue, however, note:
      • You will need to have saved their data file to the folder where you are saving your notebook. The file is in the "Example Data Analysis in a Jupyter Notebook" section way at the top of the page, or you can get it from https://s3.amazonaws.com/dq-blog-files/fortune500.csv.
      • There needs to be a carriage return after "import seaborn as sns"