+ 3

Can anyone explain this Python code to me?

if not True: print("1") elif not(1+1==3): print("2") else: print("3") This code is there in Python 3 Tutorial→Control Structure→Boolean Logic→Question 3/3

24th Aug 2018, 2:48 PM
deepak panwar
deepak panwar - avatar
7 Respostas
+ 2
A condition after the 'if' is evaluated either as 'True' or 'False'. The keyword True itself though will always be 'True'. The first condition is like: 'If True is not true...' And since that can never be the case, the code moves on to check for the elif. (Is the rest of the code clear?)
24th Aug 2018, 3:09 PM
HonFu
HonFu - avatar
+ 4
this prints 2 because "not True" equates to false, "not(1+1==3)" equates to "not(False)" which is true and the else doesn't execute as one of the other conditions was met
24th Aug 2018, 3:06 PM
hinanawi
hinanawi - avatar
+ 3
hinanawi has already answered your questions, but I think it's can be helpful. https://code.sololearn.com/cy0t3518QBDv/?ref=app
24th Aug 2018, 3:22 PM
<<Meowg!i>>
<<Meowg!i>> - avatar
+ 2
It is the same as this? if False: print("1") elif True: print("2") else: print("3")
25th Aug 2018, 2:07 AM
谢小波(bobo)
谢小波(bobo) - avatar
+ 1
i have rewritten the code for simple understanding #the following if returns 'False' as not of 'True' is 'False' if not(True): print("1") #The following Line returns 'True' as (2==3) returns 'False' and not('False' ) is 'True' elif not(1+1==3): print("2") else: print("3") Hence the answer is 2
24th Aug 2018, 6:05 PM
Kailas Walldoddi
Kailas Walldoddi - avatar
+ 1
Basically yes, bobo. Normally you would have some expression that still has to be evaluated as True or False, like: if type(x) != float: print('Enter a number I said!!) But you can also go with the result directly, like when you want to create an eternal loop: while True: print('a cat is', end =' ')
25th Aug 2018, 6:23 AM
HonFu
HonFu - avatar
+ 1
In the code three conditions are used with if, elif and else statement. At Step1-- code will go in the if loop and check for the logic. here condition is if true is not true i.e. true is false, then print 1. But True cannot be equal to False, so this condition does not satisfied and this step is skipped. code further check condition in step2 Step2-- Code check if not 1+1 == 3, which is true. hence it will execute elif part and print 2 as result. Hope this will answer your question.
5th Sep 2018, 2:49 PM
Abhinav Vaidya