+ 2
[Solved] Python one-liner if-else statement
I'm trying to write a one-liner if-else statement a = 1 b = 10 c = 20 c = 9 if a > b else c -= 1 My expectation is c will become 19, instead it returns SyntaxError: invalid syntax, highlighting character '-'. If changing to... (c = 9) if a>b else (c -= 1) It also returns invalid syntax. Maybe you meant '==' or ':=' instead of '='?, highlighting '(c' on the left. However below code works. c = 9 if a > b else 100 Why it is not possible increase / decrease the existing c in one-liner?
13 Respuestas
+ 8
Wong Hei Ming ,
since assignments can not be used in ternary conditional expressions, we can use in this case:
a = 1
b = 10
c = 20
c = 9 if a > b else c - 1
print(c) # result => 19
+ 5
how about
a = 1
b = 10
c = 20
#c = 9 if a > b else c -= 1
c -= 11 if a>b else 1
print(c)
+ 4
You can use the walrus operator to make an assignment in the same line where you use it.
c = 9 if a > b else (c := 20) - 1
But I find it utterly pointless because the value 20 is never actually used or remembered any longer, anyway.
Other oneliner options:
d = 9 if a > b else 19
e = [19, 9][a > b]
+ 2
"One line" if statements in python are called ternary operator and it differs from traditional if,else blocks.
https://www.geeksforgeeks.org/ternary-operator-in-python/
https://www.dataquest.io/blog/python-ternary-operator/
+ 2
you have to use += to increase or -= to decrease python variables
+ 1
Bob_Li
Do you mean...?
c -= 1 if a < b else 9
Putting shorthand on the left side and reverse the condition?
+ 1
yes, that is a another way to write it.
But I thought you wanted c=9 for the else condition? So it should be
c -= 1 if a<b else 11
because 20-11=9
+ 1
Bob_Li
I'm practicing class, getter and setter. And I just make up the example above to aviod my real code.
The setter logic is very simple and think one-liner can save space.
Once it is done I may post it somewhere.
But unfortunately it won't run in playground because it is highly interactive.
+ 1
Mokarom Hasan everyone already knows that.
The question was how to write it in one line.
+ 1
Tibor Santa
that last one is an innovative way of using index 0 or 1. False, True resolves to 0 and 1.
It is the shortest if else statement I have ever seen.
It also works for tuples
c = (19,9)[a>b]
or doing if else without writing if-else:
opt = input()
print(('you picked 1', 'something else')[opt!='1'])
0
Wong Hei Ming
taking all the above suggestions and going crazy with it.
You can actually do a chaining if-else-if. But this is just for fun. Codes like these are unreadable.
a, b, c = 1, 10, 20
p_all = lambda:print(f'a={a} b={b} c={c}')
print('initial state:')
p_all()
#ordering of conditions is tricky because of short circuiting.
mut_c = lambda x: 47 if a==b else 100 if b==100 else 9 if a>b else x-1 if a<b else x
print('\nc after aplying mutation function:')
c=mut_c(c)
p_all()
print('\ntests:')
a = 11
c=mut_c(c)
p_all()
a = 10
c=mut_c(c)
p_all()
b = 100
c=mut_c(c)
p_all()
a = 100
c=mut_c(c)
p_all()
b = 5
c=mut_c(c)
p_all()
0
a = 1
b = 10
c = 20
if a > b :
c = 9
else :
c -= 1
print(c)
0
hello