0
Why if statement evaluates to true?
4 Antworten
+ 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
+ 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")
+ 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