+ 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?

16th Nov 2023, 1:47 PM
Wong Hei Ming
Wong Hei Ming - avatar
13 Réponses
+ 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
16th Nov 2023, 3:39 PM
Lothar
Lothar - avatar
+ 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)
16th Nov 2023, 2:02 PM
Bob_Li
Bob_Li - avatar
+ 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]
17th Nov 2023, 8:33 AM
Tibor Santa
Tibor Santa - avatar
+ 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/
16th Nov 2023, 2:10 PM
Henrik Hultgren
Henrik Hultgren - avatar
+ 2
you have to use += to increase or -= to decrease python variables
16th Nov 2023, 8:35 PM
CodeWarrior
CodeWarrior - avatar
+ 1
Bob_Li Do you mean...? c -= 1 if a < b else 9 Putting shorthand on the left side and reverse the condition?
16th Nov 2023, 2:24 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 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
16th Nov 2023, 2:26 PM
Bob_Li
Bob_Li - avatar
+ 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.
16th Nov 2023, 4:18 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Mokarom Hasan everyone already knows that. The question was how to write it in one line.
17th Nov 2023, 5:44 AM
Bob_Li
Bob_Li - avatar
+ 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'])
17th Nov 2023, 8:52 AM
Bob_Li
Bob_Li - avatar
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()
17th Nov 2023, 3:11 AM
Bob_Li
Bob_Li - avatar
0
a = 1 b = 10 c = 20 if a > b : c = 9 else : c -= 1 print(c)
17th Nov 2023, 4:26 AM
Mokarom Hasan
Mokarom Hasan - avatar
0
hello
17th Nov 2023, 1:07 PM
‌ ‌