Not able to understand below mentioned Python basic code... | Sololearn: Learn to code for FREE!

+13

Not able to understand below mentioned Python basic code...

if not True: print("1") elif not (1 + 1 == 3): print("2") else: print("3") Not able to understand above question, can anybody help me to give some context for this

12/26/2017 7:34:09 AM

Ghanshyam Savaliya

47 Answers

New Answer

+39

If not True-> if false elif not 1+1==3-> if 1+1 is not equal to 3-> if true else-> if the above if statements return false Hence, "2" is printed.

+18

if statements are the basic way a programmer can control the flow of a program. it takes the form if boolean where boolean represents a true or false value. Examples are: [True], [not False], [1 < 3]. If the boolean expression is true THEN the code in the if block is executed, otherwise it is skipped. There is no error, this is the intended flow of the program. For example, if you had a code representing a bouncer at a club the if statement would check for the entrant's age being over 18. if age > 18 let them in if this is false, the program just flows on, the bouncer doesn't melt and throw an error. Now there is almost no situation where you would code 'not True' into your code, except for debugging perhaps, so this is just to illustrate the concept. not and True are keywords in Python, True always evaluates to true (so we're not comparing anything, just telling Python that this statement is true) and not 'inverts' a boolean, so not False is true and not True is false

+11

Sorry for asking again but I think, Still not getting because of that question What is the result of this code? if not True: print("1") why does this statement not have any output? because as per my understanding this should have the error message. I am not sure in this statement they are asking for what? if not True: here, I just want to understand, with what they are comparing? I know this might be the very easy question for you guys but I won't be able to get it properly(just Started Learning python through Solo App) it will be really helpful if have some more Idea about it.

+8

if not true: => if false: I.e since the condition in our if statement is false, we will jump to elif statement I.e, elif not (2==3) => elif not (false) =>elif(true) I.e it will enter the elif statement printing 2

+5

as you may know if statements only run if their condition will be true and not make every things(true or false) upside down so the opposite of true (not true in your code) means false So the first line never appear because it's always false and becase the 1+1 isn't equal to 3 elif code will run and it not go else because the elif statement runs

+5

+4

I know what Ghanshyam means. But surprisingly these guys didn't directly address his/ her problem. The thing is in any if statement we need a condition + a statement. But in If not true: print("1") something goes wrong, because there isn't actually a proper condition. "If not true" doesn't make sense. What is supposed to be evaluated? If WHAT not true?!

+4

So if I get it right, True can be replaced by any β€œtrue” condition, in order to better understanding the logic behind boolean statements. So I changed the simple True statement with some values that will return true. if not (1 + 2 == 3): print(β€œ1”) elif not (1 + 1 == 3): print (β€œ2”) else: print (β€œ3”) I hope I understand it the right way and it may help others. Please let me know if I misunderstood it, I don’t want to confuse anyone with this, if it is not right.

+4

Yes Elif not (1+1==3) Meint dass if 1+1 Nicht gleich 3 Schreib 2

+4

For example, if the first line was written as If True: print(β€œ1”) The code would of ran and output is 1. Since it is If notTrue: The code never runs as you need a true statement to run the if statement

+4

+3

Yes sure. But where is the condition here !!

+3

The stuff in parentheses. Remember, we are writing if (boolean statement) Normally you see something like 'if x > 5'. When x takes a value, then this statement becomes equivalent to 'if (True)' or 'if (False)' i.e. 6 > 5 = True. Python has the keywords True and False to represent these values, and you can choose (usually for debugging or illustrative purposes) to hard-code these values in, so if (True) # stuff to execute the 'stuff to execute' will always run, because the expression always evaluates to True.

+3

***Not x==y is like x=!y*** Look at the statement in front of IF as a whole!!! If <whatever in front of if> is True then the line will be executed,but if the statement is false the next line wont be executed and the next condition in front of elif will be analyzed!!! If <not true==false> Line won't be executed Elif < not1+1==3 is like a 1+1 !=3 > This line will be executed because it's true and the next line of ((this if elif loop ))wont be even read! So the result is 2

+2

+2

not operator is like opposite game. If I say yes(True) then 'not' operator says no(False) and vice verse. But, 'if' function executes only if the condition is true. But not operator makes it reverse. So if function executes if the condition is False, because not function reverse it to True. In the above code this is what happening.

+2

If statement only execute when it is true, so when i say if not true or if false, i am saying if statement is false, & when it's false it skips & goto elif statement.

+2

If not True means False and when you have false ,next line isn't run . But in third line because not false is true , The next line after it is run😊

+2

if not True can be understood as if False. elif not can also be understood as another if False. (1 + 1 == 3) -> (2 == 3), which is false. So the output is 2. Therefore do not continue to the next line since the second statement worked (printed an output).

+1

Hello Devika, I didn't get what you meant by: if not true: => if false: I.e since the condition in our if statement is false, we will jump to elif statement which condition is false? what does if false mean?