HomeCourseModule 02 › Turn on Python inside Excel (=PY())

Turn on Python inside Excel (=PY())

Module 02 · Setting Up Your Environment8 min readBeginner

What you'll learn

  • Check that your Excel version supports =PY()
  • Enter your first =PY() formula
  • Know the difference between regular Excel and =PY() cells

What you need

📝 Don't have Microsoft 365?
You can still do this entire course. We use =PY() heavily in Module 8; everywhere else, regular Python + the Anaconda setup is enough. If you can get the company 365 license, do.

Quick setup walkthrough

Update Excel

In Excel, go to File → Account → Update Options → Update Now. Make sure you're on a recent build. Python in Excel is generally available in Microsoft 365 since 2024.

Make a new blank workbook

Open a fresh workbook so you don't accidentally break a real one.

Click an empty cell and type the new formula

Click cell A1. Type =PY( — when you do this, Excel switches the cell into Python mode. You'll notice the formula bar turns into a multi-line area and shows a little green "PY" badge.

Inside the cell, type:

1 + 1

Press Ctrl+Enter (Windows) or Cmd+Enter (Mac) to commit. After a brief "calculating in the cloud" moment, the cell shows 2.

Try a slightly bigger one

Click another empty cell, type =PY(, then:

import pandas as pd
df = pd.DataFrame({"city": ["NYC", "LA", "Chicago"], "pop_m": [8.3, 3.9, 2.7]})
df

Ctrl+Enter. Excel now shows the DataFrame as a special "Python object" cell. Right-click it → Output as → Excel values to spill it into proper Excel cells.

What just happened?

When you type =PY( Excel sends your code to Microsoft's cloud, runs it in a real Python interpreter (with pandas, NumPy, matplotlib pre-installed), and returns the result back into your spreadsheet. Total round trip: usually 1–3 seconds.

💡 The two output modes
A Python cell can return its result either as a Python object (a single cell that holds, say, a whole DataFrame) or as Excel values (spilled into a range, one cell per value). Toggle with Right-click → Output as.

Key takeaways

  • =PY() needs Microsoft 365 Excel.
  • Type =PY( then your Python, then Ctrl+Enter.
  • Results can be a single Python object or spilled Excel values.
  • Full coverage in Module 8 — this lesson is just "is it on?"

Quick check

In a fresh Excel sheet, get this to work end-to-end:

=PY(
import pandas as pd
ages = pd.Series([24, 31, 45, 22, 38])
ages.mean()
)

You should see 32 in the cell. If you do — Python in Excel is on, and Module 8 will be a breeze.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.