Python Forum
What condition caused this True?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What condition caused this True?
#1
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.
Reply
#2
Is it something like while True: ? Which we mostly used to run loop with many iterations
Reply
#3
When the first True condition is met its code block runs and all other elif conditions are passed by.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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")
Reply
#5
(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

Reply
#6
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.
Reply
#7
(Aug-21-2019, 05:03 AM)ndc85430 Wrote: 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.

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
Reply
#8
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 is

if not l:
    ...
See the docs for more info around this.
Reply
#9
(Aug-21-2019, 05:22 AM)Malt Wrote: I would like to know whether using such condition is accepted in coding standard
although 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).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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 standard
although 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).

Cool Cool I got it Idea Clap
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UnicodeEncodeError caused by print when program runs from Popen SheeppOSU 5 2,936 Jan-13-2022, 08:11 AM
Last Post: SheeppOSU
  else condition not called when if condition is false Sandz1286 10 5,912 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,012 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Returning True or False vs. True or None trevorkavanaugh 6 9,276 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020