Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a cell from an if statement?

Tags:

python

excel

I'm learning Python (using MU) and trying to implement it in Excel (with Python in Excel), however I have found some difficulties which I can't understand:

Cell values are (just an example):

C1= 1

C2= 3

D1:

i1=xl("C1") #gives value 1

D2:

i2=xl("C2") #gives value 3

F1:

i3=0 #i3 is defined, just to see what is happening

F2:

i4=0 #same as above

F6:

if i1<i2:
    i3=i1+i2
else:    
    i3=i1*i2 #comparing values there is no output

F9:

i3 #outputs the value and seems to work well

What I can't understand is why is in this case value of F6 cell none (Python editor says "No output"), even if I adjust the code to:

if i1<i2:
    i1+i2
else:
    i1*i2

If i write

i1+i2 

in a random python activated cell, it gives off value as expected, but inside the if statement, it doesn't work.

What seems to work is adding if statement into function:

def foo(x1, x2):
    if x1<x2:
        return x1+x2
    elif x1>x2:
        return x1*x2
    else:
        return x1
        
foo(i1, i2)

in that case function returns to a cell and cell has a value as expected.

If there is no way to achieve cell value directly from if statement, I assume using a function is a way to go.

like image 841
MGKota Avatar asked Aug 31 '25 03:08

MGKota


1 Answers

TL;DR

Python in Excel will only evaluate the last expression or assignment as output.

Option 1: Add i3 to the code block after the if statement as the last expression to be evaluated.

if i1 < i2:
    i3 = i1 + i2
else:    
    i3 = i1 * i2
i3 # add

Option 2: Use a conditional expression.

i3 = i1 + i2 if i1 < i2 else i1 * i2

Option 3: Declare a function and call it (as in OP's "EDIT" section).


The "documentation" on Python in Excel (hereafter: PiE) seems rather meagre. However, this feature being a collaboration with Anaconda, there are some useful blogs. In 5 Quick Tips for Using Python in Excel, one can read:

So what constitutes a valid output for a cell? The execution of each Python cell works in a read–eval–print loop (REPL) fashion, similar to running Python in Jupyter Notebook cells. The last expression in the cell that will be evaluated (e.g., a Python object or the return value of a called Python function) will represent the output of the cell.

The comparison with Jupyter Notebook is instructive, but rather imprecise. Cf. InteractiveShell.ast_node_interactivity, which shows you that it uses 'last_expr' as the default. PiE, however, seems to use the equivalent of 'last_expr_or_assign'. Cf. Jupyter Notebook:

Jupyter Notebook output evaluation

With PiE:

x = 1
y = 2

Example of PiE showing last assignment as output

Otherwise, it is true that Jupyter Notebook (by default) also does not output an expression that is part of a control flow like the if statement. But it can be made to do so for an expression with 'all':

Jupyter Notebook output expression inside if statement

It would appear that PiE simply does not allow this. As a result, it seems you are left with 3 options:

Option 1: Add the variable at the end of the code block, after the if statement:

x = 1
if 1 == 1:
    x = 2
x

Repeat variable at the end with PiE

Option 2: Use a conditional expression ("ternary operator", cf. this SO post):

x = 1
x = 2 if 1 == 1 else x

Use ternary operator in PiE

Option 3: The workaround already provided by the OP; declare a function and call it.

like image 122
ouroboros1 Avatar answered Sep 02 '25 16:09

ouroboros1