BYU logo Computer Science

Introduction to Jupyter

To install jupyter, open your terminal and type:

conda activate cs110
conda install jupyter

To launch the jupyter notebook, type:

jupyter notebook

This launches a small server on your computer. As long as that server is running, you can use notebooks in your web browser.

Your terminal will start to print a bunch of stuff. You can ignore it, but you do need to leave the terminal open!

When you are done, you can shut down the server by closing your terminal window.

How to navigate the notebook

  • There are two modes:
    • Command mode
      • Blue highlight
      • Arrow keys move from cell to cell
      • Use enter to switch to insert mode
      • Other keys have special functions (or are ignored)
        • For example, h will bring up a help view with a list of keyboard shortcuts
    • Insert mode
      • Green highlight
      • Arrow keys move within a cell, like a regular text editor
      • Pres esc to exit insert mode and go back to command mode

Cell types

There are two commonly-used types of cells:

  • Code
    • These cells contain python code
    • They come with an output section that shows the output from running the python code
  • Markdown
    • These cells contain markdown
    • Markdown is a simple language for writing formatted text with simple text syntax

The following is what Markdown looks like:

### This is a markdown cell.

You can use hashes to make headers.

One asterisk or underscore surrounding a phrase will make the text _italic_.

Two asterisks makes the test **bold**.

You can also make lists:

- With items
  - That are nested
- Like this

When I execute a markdown cell, the markdown renders:

This is a markdown cell.

You can use hashes to make headers.

One asterisk makes the text italic.

Two asterisks makes the test bold.

You can also make lists:

  • With items
    • That are nested
  • Like this
# This is a code cell
print("When I execute a code cell, it runs the code!")

# If the last line returns a value, that value get's printed as output
foo = 17
foo
    When I execute a code cell, it runs the code!





    17

Running a cell

To run the code in a cell, or to render the markdown text, press

shift+enter.

This works in both command and insert modes.

  • shift+enter will run the current cell and move you to the next cell - This is nice when you want to run through several cells in a row quickly
  • ctrl+enter will run the current cell and stay there - This is nice when you want to stay on the current cell while you keep working on it
def print_with_pizzaz(statement):
    print(f"{statement}✨✨✨✨✨✨✨")
print_with_pizzaz("I love this class!")
    I love this class!✨✨✨✨✨✨✨
print_with_pizzaz("I love programming!")
    I love programming!✨✨✨✨✨✨✨
import random
def say_something_random():
    subjects = ["clouds", "trees", "squirrels", "students"]
    verbs = ["like", "study", "grow on", "eat", "float over"]
    objects = ["the ground", "fruit", "books", "grass"]
    prepositions = ["all the time", "with great gusto", "quickly", "on occasion", "very carefully"]
    pieces = [
        random.choice(subjects),
        random.choice(verbs),
        random.choice(objects),
        random.choice(prepositions)
    ]
    print(" ".join(pieces))
say_something_random()
    students grow on fruit with great gusto

Exporting to HTML

You can export your notebook to HTML.

We may ask you to do this when you turn in any assignments done in a jupyter notebook.

Click File → Download As → HTML.