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

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

A Sneak Peek at What’s Coming

The cell below is hidden — but run it anyway! You’ll see the kind of 3D scene you’ll be building by the end of this week.

Once the scene appears, try navigating it with your mouse: - Left mouse button + drag — rotate the scene - Scroll wheel — zoom in and out - Right mouse button + drag — pan the camera

import scene3d

scene = scene3d.Scene()
scene.set_sky(scene3d.Sky.DEEP_SPACE)

ground = scene.set_ground(length=20, width=20)
ground.set_material(scene3d.Material.Gravel.LightGray)
ground.set_tiling(8)

planet = scene3d.Shapes.Sphere(diameter=3, segments=32)
planet.set_material(scene3d.Material.Planets.Earth)
planet.set_position(0, 1.5, 0)
scene.add(planet)

box = scene3d.Shapes.Box(width=1.5, height=2.5, depth=1.5)
box.set_material(scene3d.Material.Bricks.DarkClay)
box.set_position(-4, 1.25, 0)
scene.add(box)

cylinder = scene3d.Shapes.Cylinder(diameter=1.2, height=2, tessellation=24)
cylinder.set_material(scene3d.Material.Marble.Gray)
cylinder.set_glossiness(0.7)
cylinder.set_position(4, 1, 0)
scene.add(cylinder)

What’s one thing about Python notebooks that surprised you?

And after seeing the 3D scene above, what would you like to build this week?

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”: “” }