Nearby lessons
18 of 159Python - 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:
TrueFalse
Boolean values are mainly used in:
- Conditional statements
- Decision making
- Comparison operations
- Loops
Example
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.
More Examples
Boolean Arithmetic Operations
Since True is treated as 1 and False as 0, arithmetic operations can be performed on Boolean values.
Addition
Subtraction
Multiplication
Using bool in Conditional Statements
Boolean values are commonly used with if statements.
Example with False
Checking the Data Type
The type() function returns the data type of a value.
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
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
booldata type is immutable. - Only two values are allowed:
TrueandFalse. - Internally,
True = 1andFalse = 0. - Comparison operators return Boolean values.
- Boolean values can participate in arithmetic operations.
Key Points
- The
booldata type stores logical values. - Only
TrueandFalseare 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 Type | bool |
| Allowed Values | True, False |
| Internal Representation | True = 1, False = 0 |
| Used For | Conditions, Comparisons, Loops |
| Mutable | No (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 QuestionsProgress: 0 / 8