+ 1

A python doubt

sex = "F" if sex == "M" or "m": print("Male") else: print("Female") This prints Male How it is possible

4th Jan 2021, 2:32 AM
QUANTUMSPARK1
QUANTUMSPARK1 - avatar
5 Respostas
+ 11
QUANTUMSPARK1 In python, strings are always evaluated true except empty string :)
4th Jan 2021, 3:14 AM
Simba
Simba - avatar
+ 13
Because you're not comparing "m" to anything, therefore it is naturally a true expression. Your code needs to be like this, sex = "F" if sex == "M" or sex == "m": print("Male") else: print("Female")
4th Jan 2021, 2:55 AM
Simba
Simba - avatar
+ 6
Simba Not only string even if you write number or float value in if statement it will evaluate true. if 10: // true print (11) else: print (12)
4th Jan 2021, 3:20 AM
A͢J
A͢J - avatar
+ 2
4th Jan 2021, 4:36 AM
QUANTUMSPARK1
QUANTUMSPARK1 - avatar
0
Simba That's right but why python considers "m" as True and executes the condition for if statement
4th Jan 2021, 3:01 AM
QUANTUMSPARK1
QUANTUMSPARK1 - avatar