PY function
Runs Python code in a cell using Microsoft’s cloud runtime.
Logical · Microsoft 365 · Guided Excel example
Where can you use it?
Teams building reports and business rules. Adapt the example’s decision rule when you need to test a condition or display different results based on it.
Scenario in the workbook
Insert a Python cell from Formulas. Enter the code and return an Excel value. A supported subscription is required.
A worked example from the workbook
| Row | A |
|---|---|
| 6 | Input |
| 7 | 2 |
| 8 | 3 |
| 9 | 5 |
sum([2, 3, 5])
Illustrative output10
See the inputs and instructions for all 3 examplesSyntax
PY(python_code, return_type)
Open the editor with Formulas → Insert Python. Enter sum([2, 3, 5]) in the cell and return an Excel value. The result is 10.
Worked examples
Insert a Python cell from Formulas. Enter the code and return an Excel value. A supported subscription is required.
Run the code in a Python cell. Pasting it as text into an ordinary worksheet cell does not execute it.
| Row | A |
|---|---|
| 6 | Input |
| 7 | 2 |
| 8 | 3 |
| 9 | 5 |
| Trial | Expression | Sample output |
|---|---|---|
| Record 1 | sum([2, 3, 5]) |
10 |
| Record 2 | sum([2, 3, 5]) |
10 |
| Record 3 | sum([2, 3, 5]) |
10 |