Python in Excel - Beginner Tutorial
Log in
YouTube
by
YarenBayram
Hi everyone, Kevin here. Today, we're going to look at how you can use Python directly
within Microsoft Excel. If you've never heard of Python before, it's a powerful and also popular
programming language that supports things like data manipulation and visualization, statistical
modeling, machine learning, just to name a few. By bringing Python into Excel, you can take advantage
of all of these different capabilities. It basically turbocharges Excel. We're going to start
with the absolute basics of how you even just use Python in Excel, and then we'll look at some more
advanced scenarios. Let's check this out. Here I am in Microsoft Excel, and if you'd like to
follow along today, you can access the workbook down below in the description of this video.
In Excel, there are a few different ways that you can access Python. The first way, up on the top
tabs up above, let's click on the option titled formulas. Right here in the center of the ribbon,
you'll see that there's now a new category titled Python, and right here, we can insert Python code.
I’ll click on this icon, and you'll notice that a few different things have changed on the screen.
Within the formula bar, we now see this PY icon. This indicates that we can enter Python code here.
Also, if we look down at cell A1, you'll also see the PY. You can also enter the code directly into
cell A1. With Excel, there are many different ways that you could insert Python code. Here,
I'll click on this X icon. You can also simply enter in a function. I'll type in = PY,
and then open parenthesis, and that too also turns on the Python mode. Here you see that
icon again in the formula bar and also within the cell. Let's click on the X one more time.
You can also use a shortcut key. You could press control, alt, shift, and P, and that too also
turns on Python mode. Now that we have Python enabled, we can start entering in some code. You
can enter Python code directly into the cell, or you could also enter code directly into the
formula bar up above. Now, this is a multi-line editor, but currently we only see one line here.
To give you a little bit more space, over on the right-hand side, you can click on this dropdown
arrow, and that exposes a few more lines. Here, I could also collapse it. You could also click on
this dividing line, and here I could expand it, or here I could also collapse it. Now,
a quick shortcut key, you could press control, shift, U, and that'll expose more lines, and you
press it again, and it'll toggle back to one line. I'll leave the multi-line view turned on. Let's
now start entering in some code, and we're going to start with my favorite example. Let's just say,
Hello World in Python. I'll insert a quote, and let's type in Hello World, and here I'll close
out the quotes. And then let's press enter. But you notice when I press enter, it doesn't really
do anything. It just inserts a new line within the multi-line editor. To commit the Python code,
here you'll see this tool tip that says you need to press control, enter. So, let's try that. I'll
press control, enter, and here now in the cell, we see that it says Hello World. Congratulations. You
just wrote your first Python code in Excel, making some nice progress. Now, one thing you might've
noticed is when I press control, enter here, in cell A1, it said busy for a very brief moment
before it showed the results hello world. Now you might wonder why. Hello world is really simple
Python code. Well, the reason why is the Python code runs in the Microsoft cloud. Basically, that
means on Microsoft servers. It doesn't actually run on your local computer. So, it causes a slight
delay. Now you might think that's a disadvantage, but by having Python run on Microsoft servers,
you don't actually have to install Python on your local computer and keep track of all