0
Python “If” statement questions
Sorry if my questions are too basic :( Why does the If statement only evaluates true statement? Can I make the If statement evaluates false statement? If possible please show me how. Thanks
4 odpowiedzi
+ 10
I don't know why you need to write statement like that,
Try to reverse your logic!
Python’s use of indentation is clean, concise, and consistent.
I'm not Pythonic guy, but we have the general Python if-else syntax
if condition :
statement block for True condition
else :
statement block for False condition
HonFu will correcting me if I'm wrong! 👍 // Thanks 😊
// OR
You don’t like to be forced to doing things a certain way!
+ 4
There are two ways
Add a not
Otherwise use pass in if code block and write your code block in else.
if not condition:
do something
if condition:
pass
else :
do something
+ 2
You can also try this.
if condition == False:
# do something
+ 2
Imagine a boolean expression like a traffic light: red means 'stop' (False), green means 'go' (True).
So it makes sense that if you give green light, a part of code is executed, otherwise not. If (whatever condition, positive or negative):
Using 'not', you create the opposite statements.
if 3 in [1, 2, 3]:
....
if 3 not in [1, 2, 3]:
...
Everyday logic works the same actually:
'Sir, is it the case that you' re NOT married?'
'Yes, that's TRUE, I'm NOT married.'
'Hm, so... you're married?'
'No, that's FALSE, I just told you I am NOT married!'