Nearby lessons
18 of 108Python - bool Data Type
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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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.
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
More Examples
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
2 1 0
Subtraction
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
1 -1
Multiplication
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
5 0
Using bool in Conditional Statements
Boolean values are commonly used with if statements.
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Welcome User
Example with False
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Access Denied
Checking the Data Type
The type() function returns the data type of a value.
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
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) |
🧠 Test Your Knowledge
8 QuestionsProgress: 0 / 8
Keep Going!Python - String Data Type