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

29. Python in Excel

Excel

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

Example first. Isha 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 FreshGo Maharashtra data

Expected result — After Ctrl+Enter

Cell Python code Result
K2 xl("G2") + xl("G3") 334
K3 xl("G2") * 1.05 67.2

About this data: Teaching sample for a fictional shop “Orbit Logistics”. Figures and names (e.g. Vikram Rao) are made up for this lesson on your first py cell insert python ctrl alt shift.

Before table and formula as text

Before — Orders sheet (part)

Cell Value
G2 64
G3 270
K2 (empty)

About this data: Demo table only — not real customer or company data. Built to practise your first py cell insert python ctrl alt shift.

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!)

About this data: Made-up rows so you can copy the pattern for your first py cell insert python ctrl alt shift. Currencies may use ₹, £, € or $.

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

About this data: Fictional worksheet used in class. Safe to edit; nothing here is live production data.

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.

Practice tasks (6)

Use the Orders sheet from this lesson. The first two orders have the Amounts 64 and 270. Open a hint only if you are stuck, and check your result against the answer.

1. Start a Python cell in an empty cell without using the mouse.

Hint: There is a four-key shortcut, or you can type the formula name and press Tab.

Answer: Ctrl+Alt+Shift+P, or type =PY and press Tab. A PY icon appears in the cell.

2. Write Python that subtracts the first order's Amount from the second, and commit it.

Hint: Click the cells while typing so Excel writes the xl() references for you. Commit with the Python key, not Enter.

Answer: xl("G3") - xl("G2") committed with Ctrl+Enter gives 206.

3. You pressed Enter and the cell only moved to a new line. Explain why and fix it.

Hint: In a Python cell, Enter and Ctrl+Enter do different jobs.

Answer: Enter adds a new line of code. Press Ctrl+Enter to run the code and show the result.

4. Add 10% to the first order's Amount in a Python cell.

Hint: Python has no % sign for percentages. Multiply by a decimal.

Answer: xl("G2") * 1.1 gives 70.4.

5. Open the larger Python code editor pane.

Hint: The shortcut is Ctrl+Alt+Shift with a function key.

Answer: Ctrl+Alt+Shift+F2 opens the Python in Excel code editor.

6. A Python cell shows #BUSY! for a long time. Which shortcut resets the Python runtime?

Hint: It uses Ctrl+Alt+Shift with the recalculation key.

Answer: Ctrl+Alt+Shift+F9 resets the Python runtime.

Loading practice tasks...

  1. len(df) (Easy): How many rows are in the Order ID column?
  2. df['Amount'].sum() (Easy): Add up the Amount column.
  3. df['Qty'].max() (Easy): What is the largest value in the Qty column?
  4. df['Amount'].mean() (Easy): Find the average Amount, rounded to 0 decimals.
  5. value_counts() for App (Medium): How many rows have Channel "App"?
  6. groupby('Channel').sum() for Store (Medium): Add up Amount where Channel is "Store".
  7. df['Amount'].median() (Medium): Find the median of the Amount column.
  8. df['Amount'].min() (Medium): What is the smallest value in the Amount column?
  9. df[df['Qty']>=5] (Medium): How many rows have Qty of 5 or more?
  10. std() of Amount (Hard): Find the sample standard deviation of Amount, rounded to 1 decimal.
  11. groupby mean for Website (Hard): Find the average Amount where Channel is "Website", rounded to 0 decimals.
  12. idxmax() of Amount (Hard): Which Customer has the largest Amount?