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

🐍Code Cell
1b = True
2 
3print(b)
4print(type(b))
Output
True

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.

🐍Code Cell
1a = 10
2b = 20
3 
4c = a < b
5 
6print(c)
Output
True

More Examples

🐍Code Cell
1print(10 > 5)
2print(10 < 5)
3print(10 == 10)
4print(10 != 20)
Output
True
False
True
True

Boolean Arithmetic Operations

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

Addition

🐍Code Cell
1print(True + True)
2print(True + False)
3print(False + False)
Output
2
1
0

Subtraction

🐍Code Cell
1print(True - False)
2print(False - True)
Output
1
-1

Multiplication

🐍Code Cell
1print(True * 5)
2print(False * 10)
Output
5
0

Using bool in Conditional Statements

Boolean values are commonly used with if statements.

🐍Code Cell
1is_logged_in = True
2 
3if is_logged_in:
4 print("Welcome User")
Output
Welcome User

Example with False

🐍Code Cell
1is_admin = False
2 
3if is_admin:
4 print("Access Granted")
5else:
6 print("Access Denied")
Output
Access Denied

Checking the Data Type

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

🐍Code Cell
1a = False
2 
3print(type(a))
Output
No output captured.

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

🐍Code Cell
1print(bool(0))
2print(bool(1))
3print(bool(""))
4print(bool("Python"))
Output
False
True
False
True

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