• Home
  • 3.4 Boolean Type

3.4 Boolean Type

View Categories

3.4 Boolean Type

4 min read

The Boolean data type represents one of two possible values:

  • True
  • False

Python uses the built-in bool data type to represent Boolean values.

Boolean values are mainly used in decision-making, comparisons, loops, and conditional statements. They help a program determine whether a condition is true or false.

In Python, bool is a subclass of the integer (int) type. Internally:

  • True has the value 1
  • False has the value 0

However, Boolean values should be used to represent logical conditions rather than numbers.


Syntax #

variable_name = True

or

variable_name = False

Components #

Component Description
variable_name Name of the variable.
= Assignment operator.
True / False Boolean values (keywords).

Example 1: Creating Boolean Variables #

main.py

is_logged_in = True
is_admin = False

print(is_logged_in)
print(is_admin)

Output #

True
False

Explanation #

  • True and False are the only Boolean values in Python.
  • They are written with an uppercase first letter.
  • Python stores them using the bool data type.

Example 2: Checking the Data Type #

main.py

status = True

print(type(status))

Output #

<class 'bool'>

Explanation #

  • The type() function returns the data type of the variable.
  • Since status stores a Boolean value, its type is bool.

Example 3: Boolean Result of a Comparison #

main.py

age = 20

print(age >= 18)

Output #

True

Explanation #

  • The expression age >= 18 compares two values.
  • Since 20 is greater than or equal to 18, the result is True.
  • Comparison operators always produce Boolean values.

Example 4: Equality Comparison #

main.py

a = 10
b = 20

print(a == b)
print(a != b)

Output #

False
True

Explanation #

  • == checks whether two values are equal.
  • != checks whether two values are different.
  • Both operators return Boolean values.

Example 5: Using Boolean Values in an if Statement #

main.py

is_raining = True

if is_raining:
    print("Take an umbrella.")

Output #

Take an umbrella.

Explanation #

  • The if statement checks whether is_raining is True.
  • Since it is True, the message is printed.
  • Boolean values are commonly used in decision-making.

Example 6: Boolean Values from Logical Expressions #

main.py

print(10 > 5)
print(8 < 3)
print(15 == 15)

Output #

True
False
True

Explanation #

  • Each comparison produces either True or False.
  • Boolean values indicate whether the condition is satisfied.

Example 7: Boolean Values as Integers #

main.py

print(True)
print(False)

print(int(True))
print(int(False))

Output #

True
False
1
0

Explanation #

  • Internally, True behaves like 1.
  • Internally, False behaves like 0.
  • The int() function converts Boolean values to their integer equivalents.

Example 8: Creating Boolean Values Using bool() #

main.py

print(bool(100))
print(bool(0))
print(bool(""))
print(bool("Python"))

Output #

True
False
False
True

Explanation #

  • bool() converts a value to either True or False.
  • Non-zero numbers evaluate to True.
  • Zero evaluates to False.
  • Empty strings evaluate to False.
  • Non-empty strings evaluate to True.

Example 9: Arithmetic with Boolean Values #

main.py

print(True + True)
print(True + False)
print(False + False)

Output #

2
1
0

Explanation #

  • Since True behaves like 1 and False behaves like 0, arithmetic operations can be performed on Boolean values.
  • Although this is valid, it is generally better to use Boolean values for logical operations rather than arithmetic.

Example 10: Comparing Boolean Values #

main.py

print(True == 1)
print(False == 0)

Output #

True
True

Explanation #

  • Internally, True is equivalent to 1.
  • Internally, False is equivalent to 0.
  • Therefore, these comparisons evaluate to True.

Common Mistakes #

1. Using Lowercase true and false #

Incorrect

status = true

Output #

NameError: name 'true' is not defined

Reason

Python uses True and False with uppercase first letters.

Correct

status = True

2. Using Quotes Around Boolean Values #

Incorrect

status = "True"

print(type(status))

Output #

<class 'str'>

Reason

Quotation marks create a string, not a Boolean value.

Correct

status = True

3. Assuming 1 and True Have the Same Data Type #

Incorrect Assumption

print(type(True))
print(type(1))

Output #

<class 'bool'>
<class 'int'>

Reason

Although True behaves like 1, its data type is bool.


4. Comparing Strings with Boolean Values #

Incorrect

print("True" == True)

Output #

False

Reason

A string and a Boolean value are different data types.


Best Practices #

  • Use Boolean variables to represent conditions or states.
  • Always write Boolean values as True and False.
  • Use meaningful Boolean variable names such as is_valid, has_access, or is_connected.
  • Use Boolean values for logical operations instead of numeric calculations.
  • Keep Boolean expressions simple and easy to understand.

Key Points to Remember #

  • The Boolean data type is represented by bool.
  • Boolean values are either True or False.
  • Comparison operators return Boolean values.
  • Boolean values are widely used in conditional statements and loops.
  • Internally, True behaves like 1 and False behaves like 0.
  • The bool() function converts values into Boolean values.
  • Use descriptive variable names for Boolean variables to improve code readability.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *