0

Python: why are strings evaluated true?

print("aaa" if True else "zzz") Why is the output aaa?

12th Sep 2020, 8:37 PM
Solus
Solus - avatar
5 Réponses
+ 2
Not all strings are True - an empty string '' is False. This allows for convenient stuff like: name = input() or 'anonymous'
12th Sep 2020, 11:24 PM
HonFu
HonFu - avatar
0
Your condition is True for if block so it prints "aaa" print("aaa" if False else "zzz") prints "zzz" That is python one liner if-else block, (ternary operator) Edit: Solus It is same as if True : print("aaa") else : print("zzz")
12th Sep 2020, 8:55 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 What is the condition here?
12th Sep 2020, 9:07 PM
Solus
Solus - avatar
0
Solus The boolean.
12th Sep 2020, 10:34 PM
Seb TheS
Seb TheS - avatar
0
Ternary operator: a if condition else b It means if the condition is true then a, else b. "aaa" if True else "zzz" The condition is True. Goes print("aaa")
13th Sep 2020, 1:39 AM
你知道規則,我也是
你知道規則,我也是 - avatar