what is the meaning of the line 1 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is the meaning of the line 1 ?

if not True: print("1") elif not (1 + 1 == 3): print("2") else: print("3")

19th Feb 2019, 2:27 PM
Dee Pan Kar
Dee Pan Kar - avatar
4 Answers
+ 3
Conditions in Python evaluate as True or False. Normally you first ask a question: if x < 5: .... And depending on what x is, the if block will be executed or not. Sometimes you want a condition to always be True, for example in an eternal loop: while True: ... True is always True, so the loop will run and run and run. while not True: .... could be translated to while True is not True: And that is never, because True is always True, so the answer is False and the loop will not be executed. Same with if not True: .... it's dead code, basically. I hope this wasn't too confusing. 🙄😉
19th Feb 2019, 2:41 PM
HonFu
HonFu - avatar
+ 11
First if will never executed as always is false.
19th Feb 2019, 2:40 PM
Hubert Dudek
Hubert Dudek - avatar
0
thanks to all
19th Feb 2019, 3:12 PM
Dee Pan Kar
Dee Pan Kar - avatar
0
The if construction is like a door. True means that the door is open and you can enter inside (in the if construction block). False - the door is closed. With 'not True' you have always False. So, True and False are not supposed to be used literally in the if constructions. You should use real conditions: x = 5 y = 9 if (x > y): # (5 > 9 = False) print("x > y") else if x < y: # (5 < 9 = True) print("x < y") # We enter here else: # (all above = False) print("x = y") is_greater_y = y > x # (True) if is_greater_y: # True print("y > x") # We enter here else: print("y <= x") if not (x >= y): # True print("x < y") else: print("x >= y")
19th Feb 2019, 3:58 PM
Boris Batinkov
Boris Batinkov - avatar