=PY( — Excel switches the cell into Python mode. A green "PY" badge appears.Don't just hit Enter — that adds a newline inside the cell. Ctrl+Enter is the "submit" key.
# 1 — basic math
=PY(1 + 1) # 2
# 2 — a variable
=PY(
x = 100
x * 1.08
) # 108
# 3 — string
=PY("hello".upper()) # HELLO
# 4 — list aggregation
=PY(sum([10, 20, 30])) # 60
# 5 — current date
=PY(
from datetime import date
date.today()
)
# 6 — a Series
=PY(
import pandas as pd
pd.Series([1, 2, 3, 4, 5]).mean()
) # 3.0
# 7 — a small DataFrame
=PY(
import pandas as pd
pd.DataFrame({"x": [1,2,3], "y": [4,5,6]})
)
# 8 — random
=PY(
import random
random.choice(["heads", "tails"])
)
# 9 — string manipulation
=PY("alice@example.com".split("@")[1]) # example.com
# 10 — sorted list
=PY(sorted([3, 1, 4, 1, 5, 9, 2, 6]))
By default, a =PY() cell shows a Python object — a single cell that holds (say) a whole DataFrame. To spill the contents into proper Excel cells:
Toggle back to Python-object mode if you want to use the cell's value in another =PY() formula.
Type any 10 numbers in cells A1 through A10.
=PY(
xl("A1:A10").mean()
)
The xl(...) helper grabs an Excel range as a pandas Series or DataFrame. .mean() averages it.
=PY(
s = xl("A1:A10")
{"count": s.count(),
"sum": s.sum(),
"mean": s.mean(),
"min": s.min(),
"max": s.max()}
)
Right-click → Output as → Excel values. The dict spills into a two-column table.
=PY( + your Python + Ctrl+Enter.xl("A1:A10") brings an Excel range into Python as a Series/DataFrame.In a fresh workbook, put numbers in A1:A20. In B1, write a =PY() formula that computes the median. In B2, one that computes the standard deviation.