Nearby lessons

18 of 159

Python - bool Data Type

📌 What You Will Learn
  • Define the bool data type and its two allowed values
  • Explain the internal representation of True and False
  • Use Boolean values in comparison operations and if statements
  • Identify truthy and falsy values with the bool() function
  • Perform arithmetic operations on Boolean values

Introduction

The bool data type is used to represent Boolean (logical) values.

Only two values are allowed:

  • True
  • False

Boolean values are mainly used in:

  • Conditional statements
  • Decision making
  • Comparison operations
  • Loops

Example

Loading editor…

Internal Representation of Boolean Values

Internally, Python represents Boolean values as integers.

Boolean Value Internal Value
True 1
False 0

Boolean Values from Comparison Operators

Comparison operators always return a Boolean value.

Loading editor…

More Examples

Loading editor…

Boolean Arithmetic Operations

Since True is treated as 1 and False as 0, arithmetic operations can be performed on Boolean values.

Addition

Loading editor…

Subtraction

Loading editor…

Multiplication

Loading editor…

Using bool in Conditional Statements

Boolean values are commonly used with if statements.

Loading editor…

Example with False

Loading editor…

Checking the Data Type

The type() function returns the data type of a value.

Loading editor…

Truthy and Falsy Values

Python treats some values as Truthy and others as Falsy.

Falsy Values:

  • False
  • 0
  • 0.0
  • None
  • "" (Empty String)
  • [] (Empty List)
  • {} (Empty Dictionary)

Truthy Values:

Any non-zero number or non-empty object is treated as True.

Example

Loading editor…

Difference Between int and bool

int bool
Represents numbers Represents logical values
Example: 10 Example: True
Many possible values Only True or False

Applications of Boolean Values

Boolean values are widely used in programming for:

  • Decision making
  • Conditional statements
  • Loops
  • Comparison operations
  • Logical expressions

Important Points

  • The bool data type is immutable.
  • Only two values are allowed: True and False.
  • Internally, True = 1 and False = 0.
  • Comparison operators return Boolean values.
  • Boolean values can participate in arithmetic operations.

Key Points

  • The bool data type stores logical values.
  • Only True and False are valid Boolean values.
  • Use type() to check the data type.
  • Boolean values are mainly used in conditions and loops.
  • Python supports Truthy and Falsy values.

Quick Summary

Feature Description
Data Typebool
Allowed ValuesTrue, False
Internal RepresentationTrue = 1, False = 0
Used ForConditions, Comparisons, Loops
MutableNo (Immutable)
📝 Key Takeaways
  • The bool data type stores only True and False
  • Internally, True is represented as 1 and False as 0
  • Comparison operators always return a Boolean value
  • bool(0) returns False, while any non-zero number returns True
  • An empty string returns False, while a non-empty string returns True

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.