Single line if-condition-assign

I have the following code

num1 = 10 someBoolValue = True 

I need to set num1 to 20 if someBoolValue is True ; and do nothing. So here is my code for this

 num1 = 20 if someBoolValue else num1 

Is there something I could avoid ...else num1 part to make it cleaner? Equivalent

 if someBoolValue: num1 = 20 

I tried replacing it with ...else pass as follows: num1=20 if someBoolValue else pass . All I got is a syntax error. And I could not just omit ...else num1 part ...else num1 .

+102
python if-statement
Oct 24 '11 at 8:15
source share
12 answers

I do not think this is possible in Python, because what you are actually trying to do is probably expanding to the following:

 num1 = 20 if someBoolValue else num1 

If you exclude else num1 , you will get a syntax error, as I am pretty sure that the assignment really should return something.

As mentioned above, you can do this, but it’s bad, because you will probably just stop confusing yourself when you read this piece of code the next time:

 if someBoolValue: num1=20 

I'm not a big fan of num1 = someBoolValue and 20 or num1 for the same reason. I have to think twice about what this line does.

The best way to achieve what you want to do is with the original version:

 if someBoolValue: num1 = 20 

The reason the best check is because it is very obvious what you want to do, and you won’t confuse yourself, or anyone else will come into contact with this code later.

Also, as a side note, num1 = 20 if someBoolValue is valid Ruby code, because Ruby works a little differently.

+153
Oct 24 '11 at 8:27
source share

Use this:

 num1 = 20 if someBoolValue else num1 
+39
Oct 24 '11 at 8:22
source share

In one line:

 if someBoolValue: num1 = 20 

But do not do it. This style is usually not expected. People prefer a longer shape for clarity and consistency.

 if someBoolValue: num1 = 20 

(Camel caps should also be avoided, so use some_bool_value .)

Note that the built-in expression some_value if predicate does not exist without the else part, because there would be no return value if the predicate was false. However, in all cases, expressions must have a well-defined return value. This differs from usage, for example, in Ruby or Perl.

+19
Oct 24 '11 at 8:17
source share

You can use one of the following actions:

 (falseVal, trueVal)[TEST] TEST and trueVal or falseVal 
+12
Oct 08 '15 at 8:21
source share

No. You probably hoped that something like num1 = 20 if someBoolValue would work, but it is not. I think the best way with an if , as you wrote it:

 if someBoolValue: num1 = 20 
+5
Oct 24 '11 at 8:18
source share
 num1 = 10 + 10*(someBoolValue == True) 

This is my new final answer. The previous answer was as follows and was excessive for the stated problem. Getting_too_clever == not Good . Here's the previous answer ... still fine if you want to add one thing for True cond and another for False :

 num1 = 10 + (0,10)[someBoolValue == True] 

You mentioned that num1 already has a value that should be left alone. I assumed num1 = 10 , since the first message operator, so the operation to get to 20 is to add 10 .

 num1 = 10 someBoolValue = True num1 = 10 + (0,10)[someBoolValue == True] print(f'num1 = {num1}\nsomeBoolValue = {someBoolValue}') 

created this outlet

 num1 = 20 someBoolValue = True 
+3
Dec 09 '17 at 2:35
source share
 num1 = 20 * someBoolValue or num1 
+1
Sep 29 '15 at 10:29
source share

You can definitely use num1 = (20 if someBoolValue else num1) if you want.

0
Jun 21 '18 at 16:12
source share

Here is what I can offer. Use another variable to display the if clause and assign it num1.

The code:

 num2 =20 if someBoolValue else num1 num1=num2 
0
Jul 25 '18 at 10:39
source share

Another way is num1 = (20*boolVar)+(num1*(not boolVar))

0
Dec 11 '18 at 17:00
source share

This looks cleaner to me, although logically and in execution it looks like the code in the question:

num1 = {True: 20, False: num1} [num1<20]

0
Dec 15 '18 at 5:36
source share

If you want to call a method if some boolean value is set to true, you can specify else None to complete the trine.

 >>> a=1 >>> print(a) if a==1 else None 1 >>> print(a) if a==2 else None >>> a=2 >>> print(a) if a==2 else None 2 >>> print(a) if a==1 else None >>> 
0
Apr 26 '19 at 23:01
source share



All Articles