Python: Boolean [Data Type]

Boolean represents a value that can be either true or false. Everything that is evaluated to a boolean can be one of 2 values-

  • True
  • False

If we convert any other data type to Boolean, it will be either True or False.

Create Boolean

To create a boolean value, assign True or False to create a boolean value-

big_box_bool = True

print(big_box_bool)

some_false_val = False

print(some_false_val)

print(type(big_box_bool))
Python

Output:

True

False

<class 'bool'>
Plaintext

Or, we can use the “bool()” function to create a boolean value-

Pass nothing, empty string, or zero to bool() and it will create a false value.
Pass any valid, non-empty value, and it will create a true value.
some_bool_val = bool()

print(f"bool() with blank is: {some_bool_val}")

some_bool_val = bool("")

print(f"Bool of empty string is: {some_bool_val}")

some_bool_val = bool("BigBoxCode")

print(f"Bool of BigBoxCode is: {some_bool_val}")

some_bool_val = bool(0)

print(f"Bool of 0 is: {some_bool_val}")

some_bool_val = bool(1)

print(f"Bool of 1 is: {some_bool_val}")

some_bool_val = bool(True)

print(f"Bool of True is: {some_bool_val}")

some_bool_val = bool(False)

print(f"Bool of False is: {some_bool_val}")
Python

Output:

bool() with blank is: False
Bool of empty string is: False
Bool of BigBoxCode is: True
Bool of 0 is: False
Bool of 1 is: True
Bool of True is: True
Bool of False is: False
Plaintext

Size of Boolean

Let’s check the size of a boolean value in memory.

To get the size we have to use “getsizeof()” function from “sys” package. This function returns the size in bytes.

We can see the size of a boolean in memory is 28 bytes, in a 64-bit system.

NOTES

We are using a 64-bit system. So the result is for a 64-bit system.

import sys

print(sys.getsizeof(True))
print(sys.getsizeof(False))
Python

Output:

28
28
Plaintext

Boolean Reassignment Internals

Let’s create some boolean variables and check their internal ID.

first_bool = True

print("first_bool: ", id(first_bool))

second_bool = False

print("second_bool: ", id(second_bool))


third_bool = True

print("third_bool: ", id(third_bool))

fourth_bool = False

print("fourth_bool: ", id(fourth_bool))


fifth_bool = True

print("fifth_bool: ", id(fifth_bool))
Python

Output:

All the True values have the same ID and all the false values have the same ID. Because all True variables refer to the same object in memory and the same for the False variables.

first_bool:  140727304839568

second_bool:  140727304839600

third_bool:  140727304839568

fourth_bool:  140727304839600

fifth_bool:  140727304839568
Plaintext

Here is how it looks in memory-

Python Boolean Internal ID
Python Boolean Internal ID

Now let’s see what happens when we change the value of a boolean variable-

import ctypes

big_box_bool = True

first_id = id(big_box_bool)

print("Initial id: ", first_id)

big_box_bool = False

print("ID after change: ", id(big_box_bool))

obj = ctypes.cast(first_id, ctypes.py_object)

print("Old boolean object: ", obj)
Python

Output:

When reassigned, the variable will point to a new object in memory. And the previous boolean object remains the same in memory.

Initial id:  140727304839568
ID after change:  140727304839600

Old boolean object:  py_object(True)
Plaintext

Here is how the reassignment operation looks in memory-

Python Boolean Reassignment
Python Boolean Reassignment

Value of Boolean

When a boolean is evaluated for its value, we get-

  • True: is equivalent to integer value 1.
  • False: is equivalent to integer value 0.
sum_one = True + 1

print("Value of True + 1 is: ", sum_one)

sum_two = False + 1

print("Value of False + 1 is: ", sum_two)
Python

Output:

Value of True + 1 is:  2
Value of False + 1 is:  1
Plaintext

Logical Operation

big_box_bool = True and False

print("Value of (True and False) is: ", big_box_bool)

big_box_bool = True or False

print("Value of (True or False) is: ", big_box_bool)

big_box_bool = not True

print("Value of (not True) is: ", big_box_bool)

big_box_bool = not False

print("Value of (not False) is: ", big_box_bool)
Python

Output:

Value of (True and False) is:  False
Value of (True or False) is:  True
Value of (not True) is:  False
Value of (not False) is:  True
Plaintext

Comparison Operator

print("Value of 10 == 10 : ", 10 == 10)
print("Value of 10 == 3 : ", 10 == 3)
print("Value of 10 != 3 : ", 10 != 3)
print("Value of 10 != 10 : ", 10 != 10)
print("Value of 10 > 3 : ", 10 > 3)
print("Value of 10 < 3 : ", 10 < 3)

print("Value of 10 >= 3 : ", 10 >= 3)
print("Value of 10 >= 10 : ", 10 >= 10)
print("Value of 10 >= 100 : ", 10 >= 100)

print("Value of 10 <= 3 : ", 10 <= 3)
print("Value of 10 <= 10 : ", 10 <= 10)
print("Value of 10 <= 100 : ", 10 <= 100)
Python

Output:

Value of 10 == 10 :  True
Value of 10 == 3 :  False
Value of 10 != 3 :  True
Value of 10 != 10 :  False
Value of 10 > 3 :  True
Value of 10 < 3 :  False
Value of 10 >= 3 :  True
Value of 10 >= 10 :  True
Value of 10 >= 100 :  False
Value of 10 <= 3 :  False
Value of 10 <= 10 :  True
Value of 10 <= 100 :  True
Plaintext

Converting to Boolean

Here is what happens when we convert other data types to boolean-

Integer to Boolean

NOTES

Here is a summary of what happens when integers are converted to boolean-

Integer value(s) to convertBoolean Value
0False
All positive valuesTrue
All negative valuesTrue
print("Value of bool(1) is: ", bool(1))
print("Value of bool(-1) is: ", bool(-1))
print("Value of bool(100) is: ", bool(100))
print("Value of bool(-100) is: ", bool(-100))
print("Value of bool(0) is: ", bool(0))
print("Value of bool(-0) is: ", bool(-0))
Python

Output:

Value of bool(1) is:  True
Value of bool(-1) is:  True
Value of bool(100) is:  True
Value of bool(-100) is:  True
Value of bool(0) is:  False
Value of bool(-0) is:  False
Plaintext

Float to Boolean

NOTES

Here is a summary of what happens when float are converted to boolean-

Float value(s) to convertBoolean Value
0.0False
All positive valuesTrue
All negative valuesTrue
print("Value of bool(1.0) is: ", bool(1.0))
print("Value of bool(-1.0) is: ", bool(-1.0))
print("Value of bool(0.1) is: ", bool(0.1))
print("Value of bool(-0.1) is: ", bool(-0.1))

print("Value of bool(0.0) is: ", bool(0.0))
print("Value of bool(-0.0) is: ", bool(-0.0))

print("Value of bool(1e100) is: ", bool(1e100))
print("Value of bool(-1e100) is: ", bool(-1e100))

print("Value of bool(float('inf')) is: ", bool(float('inf')))
print("Value of bool(float('-inf')) is: ", bool(float('-inf')))
print("Value of bool(float('nan')) is: ", bool(float('nan')))
Python

Output:

Value of bool(1.0) is:  True
Value of bool(-1.0) is:  True
Value of bool(0.1) is:  True
Value of bool(-0.1) is:  True

Value of bool(0.0) is:  False
Value of bool(-0.0) is:  False

Value of bool(1e100) is:  True
Value of bool(-1e100) is:  True

Value of bool(float('inf')) is:  True
Value of bool(float('-inf')) is:  True
Value of bool(float('nan')) is:  True
Plaintext

String to Boolean

NOTES

Here is a summary of what happens when strings are converted to boolean-

String value(s) to convertBoolean Value
Empty StringFalse
Non-Empty StringTrue
print("Value of bool('') is: ", bool(''))
print("Value of bool(' ') is: ", bool(' '))
print("Value of bool('\\n') is: ", bool('\n'))
print("Value of bool('true') is: ", bool('true'))
print("Value of bool('TRUE') is: ", bool('TRUE'))
print("Value of bool('1') is: ", bool("1"))
print("Value of bool('yes) is: ", bool("yes"))
print("Value of bool('no') is: ", bool("no"))
print("Value of bool('false') is: ", bool("false"))
print("Value of bool('FALSE') is: ", bool("FALSE"))
print("Value of bool('0') is: ", bool("0"))
print("Value of bool('on') is: ", bool("on"))
print("Value of bool('off') is: ", bool("off"))
print("Value of bool('maybe') is: ", bool("maybe"))
print("Value of bool('123') is: ", bool("123"))
print("Value of bool('null') is: ", bool("null"))
Python

Output:

Value of bool('') is:  False
Value of bool(' ') is:  True
Value of bool('\n') is:  True
Value of bool('true') is:  True
Value of bool('TRUE') is:  True
Value of bool('1') is:  True
Value of bool('yes) is:  True
Value of bool('no') is:  True
Value of bool('false') is:  True
Value of bool('FALSE') is:  True
Value of bool('0') is:  True
Value of bool('on') is:  True
Value of bool('off') is:  True
Value of bool('maybe') is:  True
Value of bool('123') is:  True
Value of bool('null') is:  True
Plaintext

Tuple to Boolean

NOTES

Here is a summary of what happens when tuples are converted to boolean-

Tuple value(s) to convertBoolean Value
Empty TupleFalse
Non-Empty TupleTrue
print("Value of bool(()) is: ", bool(()))
print("Value of bool((1, 2, 3)) is: ", bool((1, 2, 3)))
print("Value of bool((None,)) is: ", bool((None,)))
print("Value of bool((False,)) is: ", bool((False,)))
Python

Output:

Value of bool(()) is:  False
Value of bool((1, 2, 3)) is:  True
Value of bool((None,)) is:  True
Value of bool((False,)) is:  True
Plaintext

List to Boolean

NOTES

Here is a summary of what happens when lists are converted to boolean-

List value(s) to convertBoolean Value
Empty ListFalse
Non-Empty ListTrue
print("Value of bool([]) is: ", bool([]))
print("Value of bool([1, 2, 3]) is: ", bool([1, 2, 3]))
print("Value of bool([0]) is: ", bool([0]))
print("Value of bool([""]) is: ", bool([""]))
print("Value of bool([None]) is: ", bool([None]))
print("Value of bool([0, None, "", [], False]) is: ", bool([0, None, "", [], False]))
print("Value of bool([1, "", None, 2]) is: ", bool([1, "", None, 2]))
print("Value of bool([[], [], []]) is: ", bool([[], [], []]))
print("Value of bool([1, 'Hello'', [1, 2]]) is: ", bool([1, "Hello", [1, 2]]))
Python

Output:

Value of bool([]) is:  False
Value of bool([1, 2, 3]) is:  True
Value of bool([0]) is:  True
Value of bool([]) is:  True
Value of bool([None]) is:  True
Value of bool([0, None, , [], False]) is:  True
Value of bool([1, , None, 2]) is:  True
Value of bool([[], [], []]) is:  True
Value of bool([1, 'Hello'', [1, 2]]) is:  True
Plaintext

Dictionary to Boolean

NOTES

Here is a summary of what happens when dictionaries are converted to boolean-

Dictionary value(s) to convertBoolean Value
Empty DictionaryFalse
Non-Empty DictionaryTrue
print("Value of bool({}) is: ", bool({}))
print("Value of bool({\"key\": \"value\"}) is: ", bool({"key": "value"}))
print("Value of bool({\"key\": 0}) is: ", bool({"key": 0}))
print("Value of bool({\"key\": None}) is: ", bool({"key": None}))
print("Value of bool({\"key\": ""}) is: ", bool({"key": ""}))
print("Value of bool({\"key1\": 0, \"key2\": None}) is: ", bool({"key1": 0, "key2": None}))
print("Value of bool({\"key1\": [], \"key2\": {}}) is: ", bool({"key1": [], "key2": {}}))
print("Value of bool({\"key1\": True, \"key2\": \"value\"}) is: ", bool({"key1": True, "key2": "value"}))
Python

Output:

Value of bool({}) is:  False
Value of bool({"key": "value"}) is:  True
Value of bool({"key": 0}) is:  True
Value of bool({"key": None}) is:  True
Value of bool({"key": }) is:  True
Value of bool({"key1": 0, "key2": None}) is:  True
Value of bool({"key1": [], "key2": {}}) is:  True
Value of bool({"key1": True, "key2": "value"}) is:  True
Plaintext

Set to Boolean

NOTES

Here is a summary of what happens when sets are converted to boolean-

Set value(s) to convertBoolean Value
Empty SetFalse
Non-Empty SetTrue
print("Value of bool(set()) is: ", bool(set()))
print("Value of bool({1, 2, 3}) is: ", bool({1, 2, 3}))
print("Value of bool({0}) is: ", bool({0}))
print("Value of bool({None}) is: ", bool({None}))
print("Value of bool({\"\", 0}) is: ", bool({"", 0}))
print("Value of bool({1, \"hello\", True}) is: ", bool({1, "hello", True}))
print("Value of bool({0, None, False}) is: ", bool({0, None, False}))
Python

Output:

Value of bool(set()) is:  False
Value of bool({1, 2, 3}) is:  True
Value of bool({0}) is:  True
Value of bool({None}) is:  True
Value of bool({"", 0}) is:  True
Value of bool({1, "hello", True}) is:  True
Value of bool({0, None, False}) is:  True
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.