True in Python: the true boolean value

True is the true value of Python's boolean type: a capitalised keyword that counts as 1 in arithmetic and drives every condition.
5 min read
Believemy logo

Definition

A program spends its time deciding things: show this message, run this step, grant that access. To decide, it needs a way to state plainly that the answer is yes. That is the role of True, one of the two boolean values in Python, the other being False: it stands for whatever holds, whether a comparison that checks out or an access granted.

True is written with a capital letter and no quotes: it is a keyword of the language, not a variable you define yourself. Like None, it points at a single object in memory.

PYTHON
# True is the answer of a comparison that checks out
is_adult = True

if is_adult:
    print("Access granted")

print(3 > 2)
# True

Every comparison written in Python returns one of these two values. The type carrying them is called bool, produced by an expression such as 3 > 2.


Being true does not always mean being written True

A good part of the code never compares explicitly to True: we write if my_list: rather than if len(my_list) > 0:, and it works even though my_list is not a boolean. That is the role of a condition: it accepts any value, and Python reduces it to true or false following one short rule.

There is just one thing to remember: whatever is empty or zero is false, everything else is true. Here are the most common cases.

ValueTreated as
True, 1, -7True
"text", [1, 2]True
False, 0, 0.0False
"", [], {}False
NoneFalse

The nuance shows up the moment a value is compared rather than tested. [1, 2] == True answers false, while the same list does trigger the block of an if, treated as true. Hence the preference for the direct test.


Counting what is true, without a loop or a counter

Picture a list of marks and the need to know how many clear the pass grade. The explicit way would open a loop and a counter. Python offers a shortcut, because True is worth 1 in a calculation and False is worth 0: the boolean type derives from the int type, an integer limited to two values.

PYTHON
marks = [12, 8, 15, 4, 17]

# each comparison is True (1) or False (0); sum() adds them up
passes = sum(mark >= 10 for mark in marks)

print(passes)
# 3

Each comparison produces a boolean, and sum() adds it up as a number. Same mechanism for True + True, which equals 2: a boolean stays an integer in disguise, which lets you count occurrences without ever writing a loop.


while True: when you do not know in advance when to stop

Some loops know their number of turns before they start, like walking through a list of five items. Others do not: waiting for keyboard input, running a menu, keeping a program on standby. Impossible, then, to write a stopping condition in advance. Placing True as the condition of a while loop answers this need: the loop runs with no fixed limit, and the code inside decides to stop it with a break.

PYTHON
while True:
    answer = input("Your choice: ")
    if answer == "q":
        break  # the only way out of the loop
    print("Received:", answer)

No need to guess how many times the user will answer: the loop keeps going until "q", then stops.

Warning

A while True loop with no break, no return and no exception never stops. The program freezes, and someone has to interrupt it by hand: it is the most common mistake on this kind of loop.


The traps that come from another language

Most mistakes around True do not come from a misunderstanding of Python, but from a habit picked up elsewhere.

The first comes from JavaScript or JSON, where the true boolean is written in lowercase. In Python, true without a capital does not exist and immediately raises a NameError. The reflex to build is simple: a capital letter at the start of the word, just like False and None.

The second concerns the explicit comparison. if is_active == True: works, but if is_active: says the same thing in fewer characters, and stays correct even when is_active holds a true value without being exactly True, such as a filled list.

The third is the identity test with is, stricter than == True: it checks that you are holding exactly the same object, not merely an equivalent value. 1 is True therefore answers false, while 1 == True answers true. True exists as a single instance in memory, like None, which makes this test reliable; it is only justified to tell a genuine boolean apart from a value that is merely equal to it.


Frequently asked questions

Question

Why does True take a capital letter in Python?

Because it is a keyword of the language, in line with False and None which are capitalised too. The lowercase form belongs to JavaScript and to the JSON format, not to Python.

Question

Should if active == True be written?

No, if active: is enough and reads better. The comparison brings no safety, it adds one more chance of typing a single equals sign, and it rules out true values that are not the boolean itself.

Question

Are True and 1 interchangeable?

Inside a calculation, yes, since the boolean derives from the integer. Inside a dictionary they go further and collide: {1: "a", True: "b"} holds one single key, whose value is the one written last.

Related terms

Discover our python glossary

Browse the terms and definitions most commonly used in development with Python.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.