Ravindra BagaleCourses & study guides मराठी Track your progress

29. Python in Excel

29.2 Your First =PY Cell: Insert Python, Ctrl+Alt+Shift+P and Ctrl+Enter

Example first. Ruhi selects cell K2 on the Orders sheet, presses Ctrl+Alt+Shift+P, clicks G2, types +, clicks G3 and presses Ctrl+Enter. K2 shows 334: the amounts of the first two orders (64 + 270). She has written her first Python formula without typing a single bracket of Excel syntax. This lesson covers the three ways to start a Python cell, how to reference cells, and the keys that matter.

Your first Python cell — in 30 seconds

What: A Python cell is a cell whose formula is Python code. You start one with Formulas › Insert Python, by typing =PY and choosing PY, or with Ctrl+Alt+Shift+P. You commit the code with Ctrl+Enter.

Why: Knowing the start and commit keys removes the most common beginner problem: pressing Enter and getting a new line instead of a result.

Tiny example: In K2: start Python, type xl("G2") + xl("G3"), press Ctrl+Enter. Result: 334.

First look at the data (before → after):

Your first Python cell: before and after tables, fictional Blinkit Maharashtra data

Expected result — After Ctrl+Enter

Cell Python code Result
K2 xl("G2") + xl("G3") 334
K3 xl("G2") * 1.05 67.2
Before table and formula as text

Before — Orders sheet (part)

Cell Value
G2 64
G3 270
K2 (empty)

Formula / click path

K2: Ctrl+Alt+Shift+P
xl("G2") + xl("G3")
Ctrl+Enter

Do it step by step:

  1. Select K2 on the Orders sheet.
  2. Press Ctrl+Alt+Shift+P (or select Formulas › Insert Python, or type =PY and press Tab). A PY icon appears.
  3. Click G2 with the mouse. Excel types xl("G2") for you.
  4. Type + and click G3. The cell now reads xl("G2") + xl("G3").
  5. Press Ctrl+Enter to commit. K2 shows 334.
  6. In K3 repeat with xl("G2") * 1.05 and commit: 67.2 (64 plus 5%).

Version note

Shortcuts verified on Microsoft's "Python in Excel keyboard shortcuts" page (Excel for Microsoft 365, Windows). Ctrl+Alt+Shift+P is listed there as "Like =PY, enables Python formulas in the current cell". On a Mac the menu path is the same; check the page for Mac keys.

The keys that matter (from Microsoft's shortcut list)

Keys What they do
=PY then Tab (or =PY( ) Turn the cell into a Python cell
Ctrl+Alt+Shift+P Same as =PY: Python in the current cell
Ctrl+Enter Commit the Python code (Enter adds a new line)
F2 Switch between Enter mode (pick cells with arrow keys) and Edit mode (type code)
Ctrl+Shift+U Expand or collapse the formula bar to see more lines
Ctrl+Alt+Shift+F2 Open the Python in Excel code editor pane (bigger editor, colours, IntelliSense)
Ctrl+Alt+Shift+M Switch the result between Python object and Excel value (lesson 29.4)
Ctrl+Shift+F5 Open the card of a cell that shows a card icon
Ctrl+Alt+Shift+F9 Reset the Python runtime (helps with #BUSY! or #CONNECT!)

Python is not an Excel formula, so the rules differ

Excel formula Python cell
=G2+G3 xl("G2") + xl("G3")
=G2*105% xl("G2") * 1.05 (no % sign in Python)
=ROUND(G2*1.05,1) round(xl("G2") * 1.05, 1)
=IF(H2>15,"Late","OK") "Late" if xl("H2") > 15 else "OK"
Text in double quotes Text in single or double quotes
Case does not matter in function names Case matters: Round is an error, round works

Many lines in one cell. Expand the formula bar (Ctrl+Shift+U) and press Enter for each new line. The value of the last line is what the cell shows. For example:

amount = xl("G2")
fee = amount * 0.05
amount + fee

returns 67.2. Lines that only assign a variable (fee = ...) show nothing by themselves.

When something is wrong the cell shows #PYTHON!. Select the error symbol and Show Error Message, or open the Diagnostics pane (right-click the cell › Diagnostics for this cell). The most common beginner causes: a missing bracket or quote, Round with a capital R, or a variable used before it was defined (lesson 29.7).

Microsoft sources (checked 7 October 2026): Get started with Python in Excel; Python in Excel keyboard shortcuts.

Practice task (try it before you open the answer):

  1. In K4 write a Python cell that returns the Mins of the first order (H2) plus 5.
  2. In K5 return "Late" if H8 is more than 15, else "OK".
  3. Say both results.
Answer
  1. xl("H2") + 5 returns 14 (H2 is 9).
  2. "Late" if xl("H8") > 15 else "OK" returns Late, because H8 (order BLK-1007) is 18.
  3. K4 = 14, K5 = Late. Remember to commit each one with Ctrl+Enter.

Ravindra Bagale's Tip

Learn one shortcut today: Ctrl+Enter to commit. Nine out of ten "my Python cell does nothing" questions in class are an Enter that should have been Ctrl+Enter.