Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Creating and Running a Code Snippet

If you want to learn about setting up Jupyter Notebooks, you can check out an introduction to Jupyter Notebooks or watch a dedicated video course on using Jupyter Notebooks.

00:00 Welcome to this section where we’re going to create and run our for loop code snippet inside of Jupyter Notebooks.

00:07 I’m not going to go over the installation of Jupyter Notebooks and how to get it running—we have a separate course on how to do that, and also a tutorial that you can check out.

00:16 So if you don’t have Jupyter Notebooks yet, then go ahead and check this one out and then come back to here. And if you have it running, what you do is simply you type your code—like, you click into one of those cells, make sure that it’s a code cell—but that’s what it’s going to be by default—

00:33 and then if it turns green and you’re editing, you’re writing inside of the cell, and simply start typing for x in range(5):. You can see that there’s some autocomplete, also, that’s part of it.

00:46 It’s giving me here different options of what I want to use. Ha, that was a bit not so helpful. I said range(), and the autocomplete is also using the Tab character.

00:59 5. You can also see, we have this automatic formatting—it indents for us, which makes it much easier to write. We do x = x**2 (x squared).

01:10 Syntax highlighting makes it much easier to read. And then we print out our x again. Okay. So here’s our for loop. If I want to run this, I can press the Run button up here, or use a keyboard shortcut that is Shift + Enter.

01:26 I’m going to press Run here now. And our output appears right under here. The structure of this is a little bit different than other things we’ve seen so far. Usually we have this one window that’s a code window, and then we have a different window that’s our terminal. In Jupyter Notebooks, the structure is different, and it’s kind of integrated.

01:44 You can think of it like a mix of a script editor and a REPL. It’s as if you would be writing little scripts in each cell, and then you get your output right underneath there. However, it’s not that the output is lost as it would be in a real REPL, but instead, we have access to things down here.

02:02 So x is still defined. It’s the last value that got assigned to x. And in this next cell, I can still have access to x. I can do something with it.

02:12 I can say x + 4 and get the output right down here.

02:21 So, you can see that this is a very dynamic way of working with code. I think it’s actually a nice way to start coding in general because you have this immediate feedback on the code that you’re typing.

02:33 It might get a bit confusing because if I go back up here now—let’s say I assigned this back to x. Okay.

02:43 So down here now, x equals 20. You might assume that x up here is still 16,

02:50 but Jupyter doesn’t keep track of the order of execution. So it does, in this—you can see it in here on the side which cell got executed when—but when I now go back up here, then we can see that x is now 20.

03:04 So there’s this big pool of variables that sits somewhere behind the screen and that keeps track of what the variable currently refers to. So, this might get a bit confusing but just for exploration and for getting started and for working through some kind of, like, logical thought process of exploring some data, Jupyter Notebooks is really a great choice.

03:27 Okay, so what if something goes wrong? How do we go about to debug our code? We will look at that in the upcoming video. See you over there.

Become a Member to join the conversation.