Skip to content
Advertisement

Python one line if condition like in Javascript

This is a valid syntax in JavaScript:

JavaScript

The same behavior can be achieved like this in Python:

JavaScript

Is there a Pythonic way how to get the same one-liner like in Javascript without explicitly using an if statement?

Advertisement

Answer

and is a short-circuit in python (as explained here), so

JavaScript

will give you what you want

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement