No history yet

Cells and Interactivity

Transcript

Beau

Okay, so, picking up where we left off... I have it open. It's a blank page titled 'Untitled' with a single, long rectangle on it. It feels... very empty. What am I supposed to do with this?

Jo

That's perfect. That's exactly where you should be. That rectangle? That's the fundamental building block of everything in Jupyter. It's called a cell.

Beau

A cell. Okay. Like a spreadsheet cell?

Jo

Sort of, but way more powerful. Think of it less like a spreadsheet and more like... a Lego brick. You can have different kinds of bricks and you stack them up to build something. In Jupyter, you have two main kinds of 'bricks' or cells.

Beau

Okay, Legos. I can work with that. What are the two types?

Jo

Code cells and Markdown cells. By default, that first one you see is a Code cell. You can tell because it has 'In [ ]:' next to it.

Beau

Yep, I see that. 'In' with empty square brackets.

Jo

Exactly. That's where you write code that you want the computer—the kernel we talked about last time—to actually execute. Click inside that box and just type something simple, like `2 + 2`.

Beau

Okay... typed it. Nothing happened. It's just text.

Jo

Right, because you haven't told it to *run* the cell yet. This is the key interactive part. Hold down the Shift key and press Enter.

Beau

Whoa! Okay. So, underneath it, it now says 'Out [1]:' and shows the number 4. And it gave me a new, empty cell below it.

Jo

Exactly! You just had your first conversation with the kernel. You sent it 'In [1]: 2 + 2', and it sent back 'Out [1]: 4'. That's the interactive loop: you write a bit, you run a bit, you see the result immediately.

Beau

Okay, that makes sense. It's not like a big script where you have to run the whole thing at once. You can just... poke at it, piece by piece.

Jo

That's the magic. Now, for the other cell type. Click on that new empty cell it made. See the blue border around it?

Beau

Yeah, it's highlighted in blue.

Jo

That's called Command Mode. It means you're not typing *in* the cell, you're giving commands *about* the cell. Now click inside the box where you would type.

Beau

Okay... and now the border is green. And a cursor is blinking.

Jo

That's Edit Mode. Green means go, you're editing text. Blue means you're in command. You can switch between them by clicking in and out, or just by pressing Escape to go from green to blue, and Enter to go from blue to green.

Beau

Blue is boss mode, green is typing mode. Got it.

Jo

Perfect analogy. Now, go into 'boss mode'—make sure the cell has a blue border. Then, just press the 'm' key on your keyboard.

Beau

M... okay, the 'In [ ]' thing disappeared.

Jo

You just changed the cell type. 'M' is for Markdown. This is the other Lego brick. This cell isn't for the computer, it's for humans. It's for your notes, your headings, your explanations. Go ahead and type something in there, like a title.

Beau

Okay, I'll type 'My First Calculation'. Now what? Shift+Enter again?

Jo

Yep. Same command to run it.

Beau

Oh, cool. It just turned into clean-looking text. No 'In' or 'Out' labels. So this is how you make a document that has both code and explanations right next to each other.

Jo

You got it. That's the whole point. It's a story with data. Now let's talk about managing these cells. Make sure you're in Command Mode on one of the cells... so the border is blue.

Beau

Okay, blue border. Boss mode.

Jo

Press the 'b' key. Just 'b'.

Beau

A new cell appeared below. B for below, I get it.

Jo

And 'a' adds one above. Try it.

Beau

A for above... works. Okay this is much faster than clicking menus. How do I get rid of them?

Jo

Make sure you're in Command Mode on a cell you want to delete... and quickly press the 'd' key twice. So, d-d.

Beau

d-d... and it's gone. Poof. Okay, that feels dangerous and powerful. a, b, d-d. Got it.

Jo

Last really important concept for today is execution order. Look at the number in the brackets next to your first cell. It says 'In [1]' and 'Out [1]'. That number is the order in which the kernel ran the code.

Beau

Okay, makes sense. It was the first thing I ran.

Jo

Right. Now make a new code cell and type `x = 10`. Then run it.

Beau

Okay, `x = 10`. Shift+Enter. Now it says 'In [2]'. No output, which I guess makes sense.

Jo

Correct. And in another cell below that, type `print(x)` and run it.

Beau

Okay... it printed 10. And this one is 'In [3]'. The numbers are going up: 1, 2, 3.

Jo

Perfect. Now, this is the important part. Go back up to the cell where you wrote `x = 10`. Change it to `x = 50`. But *don't* run it yet. Just change the text.

Beau

Okay, it says `x = 50` but the execution number is still `In [2]`. Now what?

Jo

Now, run the `print(x)` cell again. The one at the bottom.

Beau

Running it... huh. It still printed 10. But the code right above it says x is 50.

Jo

Because the kernel's *memory* of x is still 10. The notebook shows what you've typed, but the kernel only knows what you've *run*. The last time you told the kernel anything about x was in execution number 2. The new cell run is `In [4]`, but the kernel's state is still based on the order of execution, not the order on the page.

Beau

Ohhh, I see. So to fix this, I have to go back and re-run the `x = 50` cell first, and then run the print cell.

Jo

Exactly. Do that now. Run the `x = 50` cell.

Beau

Okay, running it... now it says `In [5]`. The numbers are out of order on the page now: 1, 5, 4. That feels messy.

Jo

It can get messy. That's why a good practice is to make sure your notebook makes sense when run from top to bottom. But now, run the `print(x)` cell one last time.

Beau

And... it prints 50. The kernel finally got the memo. The state is updated.

Jo

Exactly. And if things ever get too confusing, you can always go to the 'Kernel' menu and click 'Restart'. It's like wiping the kernel's memory clean and starting fresh. All the code is still on the page, but the memory is empty. All the 'In' and 'Out' numbers will disappear.

Beau

So that's the panic button if I make a mess of the execution order. Good to know.