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
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.
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
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.
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
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
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
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?!
Yes Elif not (1+1==3) Meint dass if 1+1 Nicht gleich 3 Schreib 2
Yes sure. But where is the condition here !!
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.
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.
***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
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.
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.
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😊
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).
У тебя условие, что not True, значит пропускаем это условие, далее условие elif not(1+1==3): Условие звучит так: если 1+1 не равняется 3 то выводим 2, поэтому программа выведет 2.
Error in Program