PY function
PY runs Python code in your workbook through Python in Excel and returns the result, with ranges passed in from cells. The calculation runs in the cloud, so your data leaves the machine.
Example
=PY("1+1",1)
Expected result: n/a Not yet checked in desktop Excel
Syntax
PY(python_code, [return_type])
Arguments in [square brackets] are optional.
-
python_code(required) - The Python code to run, as text (normally typed in the Python editing mode).
-
return_type(optional) - 0 returns a Python object, 1 converts the result to Excel values.
Returns: The result of the Python code.
Watch out for
- Your data leaves the machine to be calculated in the cloud.
- You enter it with a special key sequence, not by typing =PY normally.
Where it works
- Excel
- PY is only in Excel for Microsoft 365 and Excel for the web. Microsoft 365 with Python in Excel. Calculation runs in Microsoft's cloud, needs a supporting licence, and is still rolling out. The name is known to the build this catalog was checked on.
- Google Sheets
- No direct equivalent. Use Apps Script or a Python/Colab link; no inline equivalent.
Part of Add-in and automation functions. Every result on this page was measured in desktop Excel, build 16.0.20527. Last updated .