DEV Community

NoobCoder
NoobCoder

Posted on

5 1

How to emulate Ternary Operators of Javascript in Python.

Image description

First For Those Who don't know Ternary Operation

Ternary Operator is a easy and consize way of if-else

Ok So lets flex some JS muscles.

The syntax in JS is something like this.

let v = condition ? "True" : "False"

Here if condition is true.
The value of the variable v becomes True.
While if condition is false.
The value of the variable v becomes False.

In Python's if-else it would be.

if condition:
    v = "True"
else:
    v = "False"
Enter fullscreen mode Exit fullscreen mode

Now To Emulate This in Python ?

ifTrue = "It is a True Value"
ifFalse = "It is a False Value"
trueValue = 432
c1 = [ifFalse, ifTrue][bool(trueValue)]
Enter fullscreen mode Exit fullscreen mode

Understanding The Code

So in simple if else it would be like this

if trueValue:
   c1 = ifTrue
else:
   c2 = ifFalse
Enter fullscreen mode Exit fullscreen mode

So How is this Happening?
Now lets break it into pieces
c1 = [ifFalse, ifTrue] is a List. Do you agree?

c1[0] would be ifFalse.

c2[1] would be ifTrue.

Agreed?
Now,

bool(3) will give True in python

bool(None) will give False in python

Shall We Move on ?

Ok So

a = [ifFalse, ifTrue]
c = a[bool(trueValue)]
Enter fullscreen mode Exit fullscreen mode

here if bool(trueValue) is True then it will get typecasted to integer which is 1.

And, Whats a[1] it's ifTrue.
While it its False. It will get typecasted to a[0] which is ifFalse.

This was a Long One but for short and helpful tricks
Follow PythonZen On Instagram

Try it Here

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil โ€” patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (4)

Collapse
 
darkwiiplayer profile image
๐’ŽWii ๐Ÿณ๏ธโ€โšง๏ธ โ€ข

I love this, but at the same time I hate it ๐Ÿ˜‚

Reminds me of how you can index arrays in C as 19[arr] (get the 20th element of the arr array) because it just gets translated to *(19+arr)

Is it pretty? No.

Does it make the language look good? No.

Is it fun though? Hell yea!

Collapse
 
vanshcodes profile image
NoobCoder โ€ข

Yeah, I hated the syntax but loved it at the same time.

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข

Why not just do this? ๐Ÿคทโ€โ™‚๏ธ

result = a if condition else b
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vanshcodes profile image
NoobCoder โ€ข

Actually, I know this is better. But just wanted to show some python power to you

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

๐Ÿ‘‹ Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple โ€œthank youโ€ can uplift someoneโ€™s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay