0

In PHP, you would do something like:

$answer = ($x != null) ? true : false

instead of doing:

if($x != null) { 
    $answer = true;
} else {
    $answer = false;
}

Is there anyway to do this in python? I've tried to google and to search in here but I couldn't find anything, plus google doesn't take symbols into searching. You might say, why didn't you google search it by name? Well I do not know what that type of line code is called.

Jad
  • 76
  • 8

1 Answers1

1

If you want to use a ternary conditional operator in Python for the above code, You can do something like:

answer = True if x is not None else False # Using ternary conditional operator

Note: Same code can be written using the below line if ternary operator is not to be used

answer = x is not None # returns True/False
Rahul Gupta
  • 39,529
  • 10
  • 89
  • 105