+ 17

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

26th Dec 2017, 7:34 AM
Ghanshyam Savaliya
Ghanshyam Savaliya - avatar
51 Respostas
+ 43
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.
26th Dec 2017, 7:40 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 22
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
26th Dec 2017, 9:48 AM
Dan Walker
Dan Walker - avatar
+ 17
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.
26th Dec 2017, 8:22 AM
Ghanshyam Savaliya
Ghanshyam Savaliya - avatar
+ 9
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
27th Dec 2017, 5:23 AM
Devika
Devika - avatar
+ 7
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
23rd Sep 2021, 6:00 AM
Can Ken Conquer
+ 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
20th Mar 2021, 6:24 AM
Ali Kabiri
Ali Kabiri - avatar
+ 5
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?!
24th Apr 2021, 11:37 PM
Mohammad Javid
Mohammad Javid - avatar
+ 5
Good 🤔😉☺️👍
22nd Nov 2021, 7:07 PM
✓`•°`™AnOnimus™`°•`✓ فايز الله‎
✓`•°`™AnOnimus™`°•`✓   فايز الله‎ - avatar
+ 5
#2
6th Mar 2022, 9:00 PM
Akhartouz Youssef
Akhartouz Youssef - avatar
+ 4
Yes Elif not (1+1==3) Meint dass if 1+1 Nicht gleich 3 Schreib 2
11th Jun 2021, 2:18 PM
Abdelrahman shaheen
+ 3
Yes sure. But where is the condition here !!
3rd Jan 2018, 6:40 PM
Nourhan Hassan
Nourhan Hassan - avatar
+ 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.
3rd Jan 2018, 7:12 PM
Dan Walker
Dan Walker - avatar
+ 3
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.
17th May 2021, 8:11 AM
Diána Hrabovszki
Diána Hrabovszki - avatar
+ 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
27th Sep 2021, 3:04 PM
Ali Azimi
Ali Azimi - avatar
+ 2
2
22nd Mar 2018, 2:47 PM
Zeta Riemann
Zeta Riemann - avatar
+ 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.
10th Oct 2021, 5:59 PM
Suresh
+ 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.
14th Nov 2021, 1:47 PM
Vikas Dubey
Vikas Dubey - avatar
+ 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😊
3rd Jan 2022, 12:46 PM
Arezoo
+ 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).
5th Jan 2022, 6:04 PM
Kennedy Keyes
+ 2
У тебя условие, что not True, значит пропускаем это условие, далее условие elif not(1+1==3): Условие звучит так: если 1+1 не равняется 3 то выводим 2, поэтому программа выведет 2.
5th Feb 2022, 8:11 AM
Ronin Tadamasa
Ronin Tadamasa - avatar