0

Suppose that I have the following Python Code:

def fun5(a, b, c):
    return a <= b if b <= c else False
fun5(2, 4, 6)

The output of fun5 is True. How is this code being evaluated by Python?

I was expecting a SyntaxError because of the lack of indentations and Python requires indentation.

martineau
  • 119,623
  • 25
  • 170
  • 301
Parker Queen
  • 619
  • 2
  • 12
  • 27

1 Answers1

1

What you're looking at is called a conditional expression/ternary operator, and it is perfectly valid syntax.

It's equivalent to:

if b <= c:
    return a<=b
else:
    return False
TerryA
  • 58,805
  • 11
  • 114
  • 143