Python’s truth value (bool) type

Tue Nguyen
4 min readApr 15, 2022

--

Python uses bool to represent truth values. There are only two truth values: True and False. Remember that Python is case-sensitive, so using true or false will produce an error.

Create a bool variable

We can create a bool variable through an assignment with either True or False.

# Initialize two bool variables
x = True
y = False
# Check there type
print(type(x))
print(type(y))
<class 'bool'>
<class 'bool'>

You can also create a bool variable by assigning to it an expression that produces a bool value (normally through comparison).

Examples

# Initalize two variable
x = 1000
y = 2000
# Value-equality comparison
x == y
False# Value-inequality comparison
x != y
True# Another way is to negate == with not
not(x == y)
True# ID-equality comparison
x is y
False# ID-inequality comparison
x is not y
True# Greater than
x > y
False# Greater than or equal
x >= y
False# Less than
x < y
True# Less than ro equal
x <= y
True

When use bool?

A bool value is normally used as the condition for branching in an if statement (more on this in the control flows chapter). For now, consider the following simple example.

grade = 8

if grade >= 4:
print("Passed")
else:
print("Failed")
Passed

As you can see, "Passed" was printed out because grade >= 4 produces True (since 8 > 4), so Python executes the statements under if.

If in the first line, we change 8 to 3, then grade >= 4 will produces False, and Python will run the statements under else.

Let’s confirm this.

grade = 3

if grade >= 4:
print("Passed")
else:
print("Failed")
Failed

Typecasting

Type-casting (or type coercion) is the action of converting a value of one type to another value of a different type based on a pre-defined conversion rule. Think of it as converting USD to Euro.

To convert a value to bool, we use bool() function.

When converting a value of another type to bool

  • 0, None, and values considered as empty will produces False
  • None-zero and non-empty values will produce True

Here are some examples.

From NoneType

bool(None)False

From int

# Zero
bool(0)
False# Non-zero
bool(-1)
True

From float

# Zero
bool(0.0)
False# Non-zero
bool(1.23)
True

From list

# Empty list
bool([])
False# Non-empty list
bool([1, 2])
True

From str

# Empty string
bool("")
False# Non-empty string
bool("Hello")
True

Python sometimes does this conversion implicitly, for example, in a if statement where it expects a bool value to make a decision. Consider the following example.

x = 12

if x:
print("There is something")
else:
print("There is nothing")
There is something

Here, in if x, Python expect a True or False, but we give it an integer x. Thus, Python tries to convert x to a bool and get True (because bool(12) gives True).

Try to change 12 to None, 0, or [], and you will see "There is nothing" printed out.

x = []

if x:
print("There is something")
else:
print("There is nothing")
There is nothing

Operations on bool

Since bool type is also simple, there are not many operations on them. Normally we only perform logical operations (and, or, not) on bool values.

Logical and

x and y operation returns True only when both x and y are True. Otherwise, it returns False.

Think of this as “both must be true”.

True and TrueTrueTrue and FalseFalseFalse and TrueFalseFalse and FalseFalse

True and True might look weird, but think of it as the results of some comparison as in the following example.

age = 70

if (age >= 20) and (age <= 50):
print("Proceed to the interview round")
else:
print("Too young or too old for the job")
Too young or too old for the job

In the example above, age >= 20 gives True but age <= 50 give False. Thus,(age >= 20) and (age <= 50) is equivalent to True and False, and we get False in the end.

Logical or

x or y returns False only when both x and y are False. Otherwise, it returns True.

Think of this as “at least one must be true”.

True or TrueTrueTrue or FalseTrueFalse or TrueTrueFalse or FalseFalse

Logical not

not x returns False if x is True and True if x is False.

Think of this as “just the opposite of x".

not TrueFalsenot FalseTrue

Practice

Ex 1

Do the following

  • Initialize a variable x with value True
  • Print the value associated with x
  • Print the data type of x

Ex 2

Do the following

  • Initialize a variable x with value 10
  • Initialize a variable y with a value from a comparison that checks whether x is greater than 5 or not
  • Print the value associated with y
  • Print the data type of y

Ex 3

Do the following

  • Initialize a variable x with value True
  • Initialize a variable y with value 100
  • Check if x is of type bool (Hint: type ?isinstance to see how you can use it)
  • Similarly, check if y is of type bool

Ex 4

Do the following

  • Initialize a variable x with value 7
  • Check if x is greater than or equal to 5. If true, print "Above average". Otherwise, print "Below average".

Ex 5

Replicate Ex 4, but this time, use not

Ex 6

Give three typecasting examples that produce True and three others that produce False.

Ex 7

Do the following

  • Initialize a variable x with an arbitrary value
  • If x is even, print "Even". Otherwise, print "Odd"
  • Try your code with different values of x

Ex 8

Do the following

  • Replicate Ex 7, but this time assign 5.5 to x
  • What happens?
  • What is your opinion?

Navigation

Previous: Python’s NoneType type
Next: Python’s integer (int) type

--

--

Tue Nguyen

Former data scientist. MSc student in quantitative economics. Love sharing data science stuff.