Why if statement evaluates to true? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
12th Nov 2021, 4:53 PM
Saumya Mishra
Saumya Mishra - avatar
4 Respuestas
+ 5
Comparison operations (e.g. ==) all have higher precedence than the logical operations "or", "and", "not". The if condition in your code can be rewritten equivalently with parenthesis as follows. if (sex == "M") or "m": Python treats "m" as True. if (sex == "M") or True: This "if" condition always evaluates to True. Ref from old post
12th Nov 2021, 5:00 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
a simple way to solve the issue can also be done by using the membership operator 'in': sex='f' if sex in 'Mm': print ("male") else : print ("female")
13th Nov 2021, 1:46 PM
Lothar
Lothar - avatar
+ 1
It is similar to that of other languages. The if statement contains a logical expression using which data is compared and a decision is made based on the result of the comparison. Syntax if expression: statement(s) If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. If boolean expression evaluates to FALSE, then the first set of code after the end of the if statement(s) is executed. for more visit this link: @https://www.tutorialspoint.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_if_statement.htm
13th Nov 2021, 2:29 AM
Arun Jamson
Arun Jamson - avatar
12th Nov 2021, 5:05 PM
Zubae
Zubae - avatar