473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to do "new_variable = (variable) ? True : False;" (php) on python?

Hello to all,

How can I do

new_variable = (variable) ? True : False;

in Python in one line?

I want to do something like this:

dic = {'item1': (variable) ? True-part : False-part}

Any suggestions?

Daniel

Nov 22 '05 #1
10 5208
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)

Peter
Nov 22 '05 #2
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)

Peter
Nov 22 '05 #3
Daniel Crespo wrote:
Hello to all,

How can I do

new_variable = (variable) ? True : False;

in Python in one line?

I want to do something like this:

dic = {'item1': (variable) ? True-part : False-part}

Any suggestions?

Daniel


There's a trick using the short-circuiting boolean logic operators to
emulate the ternary operator in Python. Basically, you do
TEST and TRUE_PART or FALSE_PART

However, this fails if TRUE_PART evaluates to a False value; you end up
with FALSE_PART's value instead.

This is a trick/hack, though, and shouldn't really be used much - use
an if statement instead, or wait til 2.5 when an if expression is
coming in.

Nov 22 '05 #4
Daniel Crespo wrote:
Hello to all,

How can I do

new_variable = (variable) ? True : False;

in Python in one line?

I want to do something like this:

dic = {'item1': (variable) ? True-part : False-part}

Any suggestions?

Daniel


There's a trick using the short-circuiting boolean logic operators to
emulate the ternary operator in Python. Basically, you do
TEST and TRUE_PART or FALSE_PART

However, this fails if TRUE_PART evaluates to a False value; you end up
with FALSE_PART's value instead.

This is a trick/hack, though, and shouldn't really be used much - use
an if statement instead, or wait til 2.5 when an if expression is
coming in.

Nov 22 '05 #5
Peter Otten wrote:
Daniel Crespo wrote:
new_variable = (variable) ? True : False;
in Python in one line?
new_variable = variable


Of course to answer your actual question:

new_variable = variable and True or False

But you should consider that Peter has given
you a better answer than you think.

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
:-)

--Scott David Daniels
sc***********@acm.org
Nov 22 '05 #6
Peter Otten wrote:
Daniel Crespo wrote:
new_variable = (variable) ? True : False;
in Python in one line?
new_variable = variable


Of course to answer your actual question:

new_variable = variable and True or False

But you should consider that Peter has given
you a better answer than you think.

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
:-)

--Scott David Daniels
sc***********@acm.org
Nov 22 '05 #7
On Fri, 18 Nov 2005 14:15:06 -0800 in comp.lang.python, Scott David
Daniels <sc***********@acm.org> wrote:

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
Wrong, the grammar you have.
Do not Everything to the type you expect force.
Do or do not, there is no try.
Things as they are use.
Duck-typing embrace.
:-)


Ditto. Regards,
-=Dave

--
Change is inevitable, progress is not.
Nov 22 '05 #8
On Fri, 18 Nov 2005 14:15:06 -0800 in comp.lang.python, Scott David
Daniels <sc***********@acm.org> wrote:

<yoda>
Don't try to force everything to the type you expect,
use things as they are; embrace duck-typing.
</yoda>
Wrong, the grammar you have.
Do not Everything to the type you expect force.
Do or do not, there is no try.
Things as they are use.
Duck-typing embrace.
:-)


Ditto. Regards,
-=Dave

--
Change is inevitable, progress is not.
Nov 22 '05 #9

Peter Otten wrote:
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)


I would assume the OP is just lazy and don't want to type :

new_variable = (variable_is_true_value) ?
some_expression_when_variable_is_true_value is true :
some_expression_when_variable_is_true_value is false

rather than simple True/False.

Nov 22 '05 #10

Peter Otten wrote:
Daniel Crespo wrote:
How can I do

new_variable = (variable) ? True : False;

in Python in one line?


new_variable = variable

:-)


I would assume the OP is just lazy and don't want to type :

new_variable = (variable_is_true_value) ?
some_expression_when_variable_is_true_value is true :
some_expression_when_variable_is_true_value is false

rather than simple True/False.

Nov 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daniel Crespo | last post by:
Hello to all, How can I do new_variable = (variable) ? True : False; in Python in one line? I want to do something like this:
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.