Boolean Logic third question related to not operator | Sololearn: Learn to code for FREE!

+4

Boolean Logic third question related to not operator

What is the result of this code? if not True: print("1") elif not (1 + 1 == 3): print("2") else: print("3") explain me this plz

6/15/2017 7:18:27 PM

Shehzad

5 Answers

New Answer

+12

It will print 2. Think of it like this, an if statement tests whether a condition is true, if it is true then the statements under it will run. "#" indicates a comment, comments are ignored by the interpreter if you didn't know this. if(True) # will run following statements if(False) # won't run not True = False so if (not True) # won't run The parentheses are just so you can understand it better, they aren't necessary. if not True: # False, won't run print("1") elif not (1 + 1 == 3): # True, because 1 + 1 does not equal 3 print("2") else: # Won't run because the elif ran before it print("3") You can also use the != comparison operator. Example: if 1 + 1 != 3: # Returns True and runs the code in the block, 1 + 1 does not equal 3 ...

+4

it's 2 I don't usually do python. but because of the "Not" it makes everything that is true, false and everything that is false, true If you look at the second "if" you can see that 1+1 does not equal 3 which makes it false. but because of the Not it makes that false statement true.

0

Answer 2

0

Here if not True : '''this statement will print 1 when the if returns true but as you know not operator inverts the condition so true became false and statement of the first if block won't run ''' elif not(1+1==3): '''here (1+1==3) is false ,cause 1+1=2 . So , as the not operator inverts false as true , the statement of second block will run and print 2 and else statement will not run'''

0

It's 2