It's Called inline-if, Which has syntax something like this:
a if condition else b
That Simply means if condition is true then result is a else b.
variable = condition ? a : b
Yes We can do that, and that's not a rocket science. We can simply replace the ? with python logical
and operator and : with logical
Let's See how:
variable = condition and a or b
After reading this you might be feel like, "hmmm! that was not much important and useful". But the aim of this article to explain that how logical conditional operators are helpful and just requires a different approach to it.
let data = online && getData();
What it does is if online is true then and then it calls the getData() method.
Short circuiting means that when we are evaluating an AND expression (&&), if the first operand is false it will short-circuit and not even look at the second operand.
So instead code in python like this:
if(online): data = getData()
We can simply use logical
and again to avoid writing if statement using one liner code:
data = online and getData()
Yes I accept that this was not much smart article compare to others but Hope Some Novice programmer will get help form this🤞.
Hope you gained some information from this article✨.
Comment Your Thoughts Below.