Python “If” statement questions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

5th Apr 2019, 4:59 PM
John
John - avatar
4 Answers
+ 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!
5th Apr 2019, 10:51 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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
5th Apr 2019, 5:26 PM
Gordon
Gordon - avatar
+ 2
You can also try this. if condition == False: # do something
5th Apr 2019, 8:18 PM
Diego
Diego - avatar
+ 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!'
6th Apr 2019, 4:03 AM
HonFu
HonFu - avatar