+ 6

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

15th Jun 2017, 7:18 PM
Shehzad
Shehzad - avatar
7 Respuestas
+ 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 ...
15th Jun 2017, 8:48 PM
Tim
+ 3
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.
15th Jun 2017, 7:43 PM
Limitless
Limitless - avatar
+ 2
У тебя условие, что not True, значит пропускаем это условие, далее условие elif not(1+1==3): Условие звучит так: если 1+1 не равняется 3 то выводим 2, поэтому программа выведет 2.
5th Feb 2022, 8:14 AM
Ronin Tadamasa
Ronin Tadamasa - avatar
+ 1
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).
5th Jan 2022, 6:05 PM
Kennedy Keyes
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'''
31st Jul 2021, 4:41 PM
Manini Medha
0
Answer is 2 In second case you see that 1+1 actually equals to 2 but here it is written that 1+1=3 Surely it should give false but we had used not here so output will be reversed to true and the elif condition will be executed. I hope I had cleared your doubt☺️
29th Sep 2021, 4:21 PM
Rishav Kumar
Rishav Kumar - avatar
0
if not true means if false as mentioned in some comments above. But the false statements will not be printed, thats why 1 is not printed. elif not (1 + 1 == 3) means if 1 + 1 is not equal to 3, which is True. Because it‘s true, therefore 2 is printed. else: means if both statements above are falls then print 3. But one of the statements above was true, that‘s why 3 is not printed. As a result only 2 is printed, because this statement is true.
7th Feb 2022, 1:46 PM
Ferdaws Kukcha
Ferdaws Kukcha - avatar