What condition caused this True? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: What condition caused this True? (/thread-20573.html) Pages:
1
2
|
What condition caused this True? - majorjohnusa - Aug-20-2019 Hi all, I have a rookie question about True and False. This code will have a result of 1. if True: print("1") elif True: print ("2") else: print("3")On the other hand, the following code will have a result of 2. if not True: print("1") elif not False: print ("2") else: print("3")Based on the code, I realized that the first if statement if True: print("1")resulted as a True. And it appears to me that all statement is by default True unless otherwise specified as False, for example: (1+1==3). But I don't understand: where does this True come from? I did not input any value or give any condition such as (1+1==2). To me this looks more like a "null" because there was nothing there. Thanks. I think I figured out, an if statement is run once if its condition evaluates to True. In this case, it is stated as True in the statement. RE: What condition caused this True? - Malt - Aug-20-2019 Is it something like while True: ? Which we mostly used to run loop with many iterations RE: What condition caused this True? - wavic - Aug-20-2019 When the first True condition is met its code block runs and all other elif conditions are passed by. RE: What condition caused this True? - boring_accountant - Aug-21-2019 You seem to be confusing English and Python, my friend. The basic idea in Python is that anything that follows if/elif/else are statements that are evaluated to True or False (I'm sure this isn't 100% true all the time but this should suffice for our example here). This means that "if True" does not mean literally "if [blank] is True then...", instead it means "if True is True then...". Here: #if True is True (True == True, so this is executed) if True: print("1") #else if True is True (True == True, but the previous statement always executes so this does not run) elif True: print ("2") #all other cases (will never happen, same reason) else: print("3")Now, the second example: #If not True (not True == False) is True (False != True, so this does not run) if not True: print("1") #If not False (not False == True) is True (True == True, so this runs) elif not False: print ("2") #all other cases, this never runs, again else: print("3") RE: What condition caused this True? - Malt - Aug-21-2019 (Aug-21-2019, 02:17 AM)boring_accountant Wrote: You seem to be confusing English and Python, my friend. The basic idea in Python is that anything that follows if/elif/else are statements that are evaluated to True or False (I'm sure this isn't 100% true all the time but this should suffice for our example here). This means that "if True" does not mean literally "if [blank] is True then...", instead it means "if True is True then...". Here:#if True is True (True == True, so this is executed) if True: print("1") #else if True is True (True == True, but the previous statement always executes so this does not run) elif True: print ("2") #all other cases (will never happen, same reason) else: print("3")Now, the second example:#If not True (not True == False) is True (False != True, so this does not run) if not True: print("1") #If not False (not False == True) is True (True == True, so this runs) elif not False: print ("2") #all other cases, this never runs, again else: print("3") But while using while loop we are checking without any condition. I have seen and used in my work wherever we want infinite loops There also we won't be checking with any condition and the code sample would be while True: <Do something> It will run endlessly and we have to forcefully interrupt the execution RE: What condition caused this True? - ndc85430 - Aug-21-2019 True is the value that's being tested in your infinite while loop - it's always True , hence the loop never terminates. Expressions like x == y evaluate to True or False and cause a block to be entered if they satisfy the test. It really isn't that difficult, so possibly you're over thinking.
RE: What condition caused this True? - Malt - Aug-21-2019 (Aug-21-2019, 05:03 AM)ndc85430 Wrote: Yeah, I understand that we are creating loop. As I just started to swim in Pythonic ocean, I would like to know whether using such condition is accepted in coding standard RE: What condition caused this True? - ndc85430 - Aug-21-2019 Also, note that any object can be treated as True or False - empty lists, for example, evaluate to False . Hence, the Pythonic way to test whether a list l is empty isif not l: ...See the docs for more info around this. RE: What condition caused this True? - buran - Aug-21-2019 (Aug-21-2019, 05:22 AM)Malt Wrote: I would like to know whether using such condition is accepted in coding standardalthough it is accepted (i.e. correct syntax) it does not make sense to use explicit True or False as condition/expression in if/elif/else block. Same apply for while False: - it is correct and accepted, but nevertheless non-sense (the "loop" block will never be executed).
RE: What condition caused this True? - Malt - Aug-21-2019 (Aug-21-2019, 05:59 AM)buran Wrote:(Aug-21-2019, 05:22 AM)Malt Wrote: I would like to know whether using such condition is accepted in coding standardalthough it is accepted (i.e. correct syntax) it does not make sense to use explicit Cool I got it |