Fundamentals 7 min read

What Happens When You Add True + True in Python?

This article explains how Python treats the Boolean type as a subclass of integer, demonstrates the results of arithmetic and augmented operations on True, and shows why assigning to True or False is allowed in Python 2 but prohibited in Python 3.

AI Cyberspace
AI Cyberspace
AI Cyberspace
What Happens When You Add True + True in Python?

Interview Question: True + True == ?

Python’s "+" operator is overloaded: it performs arithmetic when the operands are numbers and concatenation when they are sequences.

In  : 1 + 1
Out : 2

In  : 'abc' + '123'
Out : 'abc123'
What if the operands are Booleans? Would you think True + True == True ? Before revealing the answer, let’s review Python’s Boolean data type.

Boolean Values

The Boolean type has only two values, True and False, and is used as the basis for logical decisions in statements such as if, while, and for.

Expressions like i > 0 or objects such as 1 or 'abc' are evaluated in a Boolean context, ultimately yielding a Boolean result.

Expression return value:

In  : i = 2
In  : i > 1
Out : True

Custom objects returning Boolean values:

If a custom object implements the special method __nonzero__ (Python 3.x uses __bool__), the method is implicitly called when the object is used in a Boolean context or passed to bool().

class TestBool(object):
    def __nonzero__(self):
        print("[*] Call TestBool.__nonzero__")
        return True

if __name__ == '__main__':
    print("Invoked function bool: %s" % bool(TestBool()))
    if TestBool():
        print("TestBool() return True.")
    else:
        print("TestBool() return False.")

OUTPUT:

[*] Call TestBool.__nonzero__
Invoked function bool: True

[*] Call TestBool.__nonzero__
TestBool() return True.

Besides custom objects, all non‑empty built‑in objects are treated as True in a Boolean context, while empty ones are False.

Python truth table
Python truth table

Boolean Type Is a Special Integer Type

Boolean objects support regular arithmetic because bool is a subclass of int, inheriting many integer methods such as __eq__ and __add__.

In  : bool.__bases__
Out : (int,)

In  : True == 1
Out : True

In  : True + True
Out : 2

In  : True + 1
Out : 2

Even though True and 1 have the same value, they are different objects because their types differ; only when id, value, and type are all identical are two objects considered the same.

In  : True is 1
Out : False

In  : type(True), type(1)
Out : (bool, int)

When using augmented assignment on a Boolean, Python creates a new local variable rather than modifying the built‑in True object.

In  : type(True)
Out : bool

In  : id(True)
Out : 140735725425440

In  : True += 1
In  : True
Out : 2

In  : type(True)
Out : int

In  : id(True)
Out : 140618394261472

In Python 2.x this syntax is allowed because True and False are just variable names in the LEGB namespace; assigning to them can even swap their meanings. In Python 3.x they are reserved keywords, and attempting to assign to them raises a SyntaxError:

In [16]: if = 1
  File "<ipython-input-16-...>", line 1
    if = 1
       ^
SyntaxError: invalid syntax
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythontype-systembooleanoperator overloadingPython 2 vs 3
AI Cyberspace
Written by

AI Cyberspace

AI, big data, cloud computing, and networking.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.