Master the fundamentals of Jupyter Notebooks to efficiently run and document Python code for data science. Learn how to execute, manage, and reset code blocks for seamless coding and learning experiences.
Key Insights
- Understand Jupyter Notebook's core structure: code and text cells run independently, allowing you to individually execute and document Python code along with explanatory notes for better clarity.
- Recognize the importance of the Python kernel environment running behind Jupyter Notebooks, as it retains variable assignments across cells, enabling later cells to access earlier-defined variables unless the kernel is restarted.
- Grasp practical notebook management skills, such as deleting unnecessary cells, resetting the runtime environment to clear all variables, and using printing or cell evaluation methods for displaying outputs effectively.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
For our last bit of setup, let's talk about Jupyter Notebooks. This course assumes a certain amount of knowledge of Python, and it assumes a certain amount of knowledge of data science. We'll be going over a little bit of both of those.
But it does not assume that you know anything about Jupyter Notebooks, so we're going to cover those fairly quickly. There's a whole lot to take in once you grasp the concept. So, with a Jupyter Notebook, each of these blocks—they're called cells—is an independently run bit of code.
That means that if I run this one, it's got a little checkmark, and it tells me it took 46 seconds to run. This one is not run yet. It does not have a checkmark.
I could run it. There's no need to yet, but I could run it by pressing this play button, this execute button. Let's try creating a code block and trying it out.
So, if you put your cursor above or below a current code block, you can choose code or text. If you want to write some text explaining code blocks, explaining the code that you're running, doing text is the right move, and we've done a lot of that. There's a lot of text in these notebooks, but we're just going to work with code for now.
There's no expectation that you write explanations of your code, although I think it can be very helpful to do so, so that you gain a greater understanding by explaining the code. But again, not something that we're asking for in this course. So, once you've got a code block, you can write some basic Python code.
Let's say name = "colon"
. That's me. If I execute this, nothing much happens, it seems, but that code has been executed.
You have a check mark here. And what will make this apparent that I ran it, is if I change this, you notice the check mark went away because I've changed the block. So, the code as it is has not been run.
And say print(name)
. If I execute that, I'll get an output here. We'll do this sometimes.
We'll print out a value like this, just so that we can see the value in a variable. Now, Jupyter will also output, not in quite the same way as printing it, but Jupyter will also output the last value you put in a notebook. So, if I put name
as the last value in a cell, that is, and I execute it, we'll get the string colon
.
And it'll get the little quotes to show you that's a string. And it doesn't matter if the value is a variable or something else. I can put a direct string, "hello there"
.
And that's a value that gets evaluated. And it's the output of the cell. If I add another value here and run it, what do you think will print out here at the bottom? Give me a second to think about that.
Is it going to be the first one, second one, or both? Pause the video if you want to try to figure that out before executing it. Did you figure it out? The answer is it will put the last value. The last value that is evaluated in a code block will be its output.
You can also, though, if you wanted to print out "hello there"
or name
, you could say print(name)
and it will do both. It will print out that and it will also output the last value that is evaluated in the block. So, we printed out the name colon
and we also evaluated the block.
It's the last value in the code block. So, each cell has a last value. It optionally gets output as well.
And sometimes that's how we'll output something. Now, that evaluation can be any Python evaluation. So, it could be name + " is your instructor"
.
And if we execute that, the last value here will be the last evaluation, which is the string name
concatenated with the string " is your instructor"
. And there we go. So, that's one of the things we need to know about blocks, how we can execute them and how their evaluation and printing works.
We should also look at what's happening in the notebook in general. If I add another code block and, you know, again, I can just put my cursor above or below a current block and do plus code. If I print(name + " is evaluating this cell")
, will it see that value name
? Pause this for a moment and think about whether it will.
And whether it should. If you were designing this, would you do that? Pause it for a second. All right.
Did you think about it? It would be helpful when you're making these blocks, even though we can run them individually, Jupyter designed their notebooks in this way so that there's actually a Python running in the background, that we can always add variables to that environment. If I execute this, name
was added to the environment because I ran it earlier. That means that later blocks can access the values from earlier blocks, which is very, very helpful and which we'll take advantage of.
Now, sometimes you might decide, you know what? I don't like this code block. I'm going to delete it. You can go up to this delete, delete that cell.
Now, if I execute this, it will still work. It will still have the name value "colon"
for the variable name
. And that's because it was added to this Python environment that's running in this notebook.
However, if I want that value totally gone, I want name
gone as a variable, or I want to totally reset name
and just remove all the previous instances where I gave it a value, I can do that. But I am going to need to restart this Python kernel, the environment running in the background. So you can go to the runtime and restart session.
Are you sure you want to restart the runtime? I am. Runtime state including all local variables will be lost. That includes the variable name
.
If I run this, it will restart, as it says down here, Python. And then it says, great, it's restarted. We're connected.
If I run this now, it'll give us an error. NameError: name 'name' is not defined
. And that's because we did not declare it.
We've totally restarted our environment and it's not there anymore. This can be helpful to know for when you need to just start over with a notebook. And why you need to make sure that you're running the values for each one when you run them.
In other words, you want to make sure that if you do restart or you open up a new notebook, none of the variables are there, yet you have to execute the code blocks that declare them in order for those values to exist. So that's the basics of Jupyter Notebook. It's a very powerful way to work with code.
It's very good for two particular things: for explanatory code—where you're writing some code and you want to have out there a tutorial or an article or a blog post or just a document for other people to read to understand what you're doing with your code and how to do something or why to do something. It's also very helpful for learners.
So that you can look at these notebooks that we've provided for you and think about and understand from our writing and from our explanation exactly what each of those bits of code does and why we're doing them. And thus learn. And when you're done with each notebook, you'll have a version where you've written the code and it's already explained because of our comments.
So it's a great way to work, and it's the way we're going to work throughout this course. Speaking of working, let's get started. Let's go dive into some statistics, some data science, some basic Python and get oriented towards machine learning.
I'll see you folks there.