Running Code in Cells3:45 with Kenneth Love
Jupyter Notebooks are made up of cells that can hold plain text, Markdown, Python, and more.
Your notebook started out with a cell already created and active. 0:00 Now, this probably doesn't look like the Python shell that you're used to, but 0:03 this cell is a Python shell. 0:07 Specifically, it's an IPython shell, but don't worry about that I. 0:09 99% of the time, it's going to work and 0:12 behave exactly like the shells that you're used to. 0:14 Let's put a little code into it. 0:16 Maybe you'll recognize this. 0:17 So we'll say name = kenneth. 0:20 And I'll say, greeting = "Treehouse loves", 0:24 and I'll print(greeting+name). 0:29 Now I want this cell to evaluate my code. 0:33 So I can press Ctrl+Enter. 0:35 And that'll be the same on Windows, Linux, or Mac, or should be, at least. 0:39 And that will execute the code. 0:43 Now, I could've also gone up here to the Cell menu and shows in Run Cells, 0:46 or Run All, or Run Cells and Select Below, all these options here. 0:49 And could also have used this button here, which would run all the cells. 0:54 I like to do it just through the keyboard, though. 0:58 It's a little bit nicer. 0:59 So what it does is it runs through the code that's here. 1:01 And then it prints the output of it down below that, 1:03 if there is any output that comes out. 1:05 As you can seem though, I did my code wrong. 1:07 I didn't include a space when I concatenated the strings together. 1:10 So to fix that, I'll click inside the code block, I'll add in my space and 1:13 the plus sign. 1:17 I'll press Ctrl+Enter again, and I get my new output in the same spot. 1:18 One thing that's really handy about cells too is that they're all linked together. 1:23 So let's go ahead and make a new cell, and we'll use this plus sign here. 1:27 And l wanna play with the format method in this one. 1:31 l'm not gonna declare the name or the greeting variables, though. 1:34 I'm just going to print out Treehouse loves. 1:36 And then placeholder, and then format and the name variable. 1:42 Press Ctrl+Enter and it still works. 1:47 Whatever you do in one cell is available in all of the other cells too. 1:49 This means that you can build up a large program throughout a notebook, 1:52 instead of having to code everything all at once. 1:54 The other main type of cell is a Markdown cell. 1:56 Markdown, if you're not familiar, is a plain text-based language for 1:59 formatting text. 2:02 We can do things like mark text as bold or italic, add in images, links, and more. 2:03 Check the teacher's notes for a good guide to Markdown. 2:08 I'm gonna add a new cell. 2:11 If you're still focused on a cell, by the way, you can press Alt + Enter or 2:13 Option + Enter and that will create a new one below it. 2:18 You can also click this plus sign right here and that will add a new cell. 2:20 Either way, though, you're gonna have to change the cell type. 2:24 So here where it says code, I'm gonna click in there and 2:27 then I'm gonna choose Markdown. 2:29 Now I'm gonna add a little bit of text. 2:31 I'm gonna do *Remember*, if I can spell remember, 2:33 the '+' does **not** add spaces, 2:39 you have to add them yourself. 2:44 And again, I'm gonna press Ctrl + Enter. 2:48 And it turns the markdown into HTML, into rendered HTML. 2:51 It's pretty awesome. 2:55 Now, while we're talking about cells, let me show you how to move them around and 2:56 delete them too. 2:59 You can move cells with these up and down arrows that are right here. 3:00 And then you can move them in the Edit menu as well. 3:05 You can see there's Move Cell Up, Move Cell Down. 3:08 And while you're in there, you can Cut Cells, Copy Cells and 3:10 you can Delete Cells. 3:14 So you can delete a cell if you don't need it anymore. 3:16 I'm sure you're already seeing ways you can use Jupiter Notebooks to make your 3:19 future studies easier. 3:22 I know I've used them in the past as a great way to take notes and 3:23 test out ideas when I didn't want to run a Python shell on my own. 3:26 Often, that's because I need to get some visual feedback about what I'm doing. 3:29 Like, if I'm working with charts, graphs, or other graphical areas. 3:32 In the next video, I'll show you how to add extensions to your notebook and 3:36 how to play around with graphing libraries. 3:38 Try to use your notebook a bit first, though, and 3:41 get comfortable with adding cells and running code in them. 3:43
You need to sign up for Treehouse in order to download course files.Sign up