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
  • 112,593
  • 23
  • 157
  • 280
Parker Queen
  • 609
  • 2
  • 11
  • 26

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
  • 56,204
  • 11
  • 116
  • 135