Hello World Notebook!

This is an example of the Jupyter .ipynb document format

Open In Jupyter K12

# This is an executable cell
print("Hello World!")
# Setting variables in Python
x = 42
x
# Variables persist after being set in previously executed cells
x

Markdown Cells Support Rich Formatting

You can use: - Bold and italic text - Lists (like this one!) - Links - inline code - And even LaTeX math: \(E = mc^2\)

This makes notebooks great for explaining your code!

# Notebooks make it easy to import and use libraries
import math
import random

# Generate a random number and calculate its square root
num = random.randint(1, 100)
sqrt_num = math.sqrt(num)
print(f"The square root of {num} is {sqrt_num:.2f}")

If you aren’t sure how to use a method, Juypter K-12 will give you helpful hints whenever you type a period (.) or an opening parentheses ((). Try typing math.sqrt( on line 3 in the next cell and watch what happens after the . and (

import math

The output of a code cell is often text, but it can also be tables, images, graphs, and other types. Run the following code as an example:

!import matplotlib.pyplot as plt
import numpy as np

x_values = np.linspace(0, 10, 100)
y_values = np.sin(x_values)

plt.figure(figsize=(8, 4))
plt.plot(x_values, y_values)
plt.title('Sine Wave')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.grid(True)
plt.show()

You just created a chart using Python! What do you notice about the sine wave? What happens to the values as x gets larger?

You can click on the “eye” icon in a code cell to hide/show the code. Try with this cell…

# Click on the "eye" icon to hide the code in this cell
print("Hello!")

And sometimes, code cells are hidden by default like this one. Try unhiding this by clicking the “eye” icon again…

# Nice! You unhid this cell to see all its secrets!
print("This cell is hidden, but you can unhide it")

Interactive Form Fields

Notebooks support form fields — interactive controls that let you change values without editing code.

Add #@param after a variable to turn it into a text box, a dropdown, a slider, or a checkbox.

Try changing the values below and re-running the cell:

NAME = "World" #@param
GREETING = "Hello" #@param ["Hello", "Hi", "Hey", "Greetings"]
TIMES = 1 #@param {type:"slider", min:1, max:5, step:1}

for i in range(TIMES):
  print(f"{GREETING}, {NAME}!")

What Happens When There’s an Error?

Run the cell below to see how notebooks handle errors.

The error appears in the output, but other cells continue to work.

# This will cause an error
result = 10 / 0

Check Your Understanding

{ “question_type”: “multiple_choice”, “question”: “What type of cell lets you write and run Python code?”, “options”: [ { “key”: “a”, “text”: “Markdown cell” }, { “key”: “b”, “text”: “Code cell” }, { “key”: “c”, “text”: “Text cell” }, { “key”: “d”, “text”: “Output cell” } ], “answer”: “b”, “submitted_answer”: “” }

{ “question_type”: “true_false”, “question”: “A variable set in one cell can be used in cells you run later.”, “answer”: “True”, “submitted_answer”: “” }

{ “question_type”: “true_false”, “question”: “When one cell causes an error, all other cells in the notebook stop working.”, “answer”: “False”, “submitted_answer”: “” }