473,326 Members | 2,175 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,326 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 5201
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.