3

I have a working conditional statement:

if True:
    pass
else:
    continue

that I would like to turn it into a ternary expression:

pass if True else continue

However, Python does not allow this. Can anyone help?

Thanks!

Kodiak North
  • 71
  • 1
  • 9

3 Answers3

3

pass and continue are a statements, and cannot be used within ternary operator, since the operator expects expressions, not statements. Statements don't evaluate to values in Python.

Nevertheless, you can still shorten the condition you brought like this:

if False: continue
Israel Unterman
  • 11,748
  • 2
  • 22
  • 31
1

Ternary expression are used to compute values; neither pass nor continue are values.

Scott Hunter
  • 44,196
  • 8
  • 51
  • 88
1

Point 1: Are your sure your condition is right? Because if True will always be True and code will never go to else block.

Point 2: pass and continue are not expressions or values, but a action instead You can not use these is one line. Instead if you use, 3 if x else 4 <-- it will work

Anonymous
  • 40,020
  • 8
  • 82
  • 111