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

+12

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

34 Answers

New Answer

+31

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.

+15

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

+6

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.

+4

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

+3

+3

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

+2

Yes sure. But where is the condition here !!

+2

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.

+2

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

+2

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?!

+2

***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

+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?

+1

I presume Devika means 'implies' or 'equivalent', basically [if (not true)] means the same as [if (false)]

+1

the issue might be with the way you spelt "True", make sure you start with capital letter "T". I hope this helps. ;)

+1

Not will revert the answer of true

+1

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.

+1

If first condition not True Run another condition else if 1 + 1 == 3 is not True then print 2 but if all this conditions are not met print the statement with else

+1

Can anyone explain the code step by step? if no true: print("1")

+1

Just imagine this keyword using NOT If the statement is True, meaning the answer is False If the statement is False, meaning the answer is True not true=False not false=true In the second statement we have elif not(1+1==3) The value inside the parameters is False so not False is equal to True.

+1

Think about it this way, it started "if not true", and for the example it shows that when something is not true it is false. It is shown that any value is seen by python as true(as long as it's not empty). So looking at it, "if not true" also means "if false" what Will be printed out. So the only false outcome there is the incorrect math.