I have a newbie question

I am working my way through a course and there is a piece of code below for a conditional statement. The course says it is testing “if true” I understand what the code is doing but I just don’t understand what is True? In my head it is saying, if true then print true, the output when you run it is: if True, so my head is going round in circles what is True sorry if this is a simple thing but I am the type of person that has to understand what is going on and can’t just skip forward.

I have read around and carried out some research and the nearest I can come to not letting my brain get into and evaluate Pi loop is the following: True is a condition and so True will always = True so the code below if True will always execute, so it’s like saying if mamals breath print mammals breath, well mammals breath so it will always print mammals breath.

After reading further it appears there’s not much use for this and in python 2 you could actually make True not equal True with True=False.

If anyone has a more elegant way of putting this I would like to hear it, or if I am wrong please let me know but above is the only way I have been able to get my head round it.

if True:
print(‘if true’)
elif False:
print(‘elif true’)
else:
print(‘neither true’)

if True:
print(‘if true’)
elif False:
print(‘elif true’)
else:
print(‘neither true’)

This is terrible example code, unless there’s some amazing context
around the example. This is an if-statement which will always print 'if true'. Neither of the other branches can ever fire.

That said, the name True (with the capital “T”) is the builtin Boolean
singleton for truth. False (again, with the capital “F”) is the
builtin Boolean singleton for falsehood.

A “singleton” object is one creted once and used as a reference. This
means, for example, that it will always be the same object with the
same id() value (eg print(id(True))). In particular, Boolean
expressions like:

x == 3

will always return either the True object or the False object.

Note that Python itself takes a looser definition of truth: we sometimes
speak of “true” or (gah!) “truthy” values; this latter “truthy” means a
value which will be treated as true. A true value will satisfy an if:

if something_here:

will fire if something_here is true. Obviously, the True singleton
is true. So, in general, are nonzero numeric values like 1 or 9.2;
conversely 0 is false.

In Python, when you test the truth of something Python calls the
__bool__ method (to obtain the object’s Boolean value) or __len__
(to obtain its length), otherwise simply that the object is not None.

Specifics here: Built-in Types — Python 3.10.0 documentation

This is convenient because you can (a) define classes whose members can
express a special truthiness if that is appropriate and (b) containers
(lists, tuples, set, dicts and many others) are “true” if they are not
empty and false if empty (because their length is nonzero or zero
repsectively) and (c) other objects are considered “true”.

That means:

# start with nothing
items = set()   # an empty set
# look for some things in a database and add them to items
items.update(query_the_db_for_something())
if items:
    print("there are items")
else:
    print("there are no items")

This makes for clear shorthand when seeing if containers are empty,
which is a common thing to test.

Also:

found = None
for obj in some_objects:
    if is_suitable(obj):
        found = obj
        break
if obj:
    print("a suitable object was found:", obj)
else:
    print("no suitable object was found")

In fact, the bool type is a subclass of int because true used to be
a 1, or at least implements as a 1.

Cheers,
Cameron Simpson cs@cskk.id.au

Hi Cameron,

First of all thanks for the best reply I have ever had as a Newbie on any forum. The additional info of it being a “singleton” object clarified things for me putting a name to the concept I was trying to put across. I can say 100 percent that you have answered my question and cleared up my confusion!!

Regards,

Renyk