Booleans
Introduction to Python
Getting Started
Comments
Variables
Data Types
Type Casting
Input and Output
Operators
Strings
Booleans
Conditional Statements
While Loop
For Loop
Loop Control Statements
Lists
Tuples
Sets
Dictionaries
Functions
Function Arguments and Return Values
Variable Scope
Recursion
Lambda Functions
Arrays and Python Lists
Modules
Dates and Math
String Formatting
File Handling
Try and Except
Classes and Objects
Inheritance
Iterators
JSON
RegEx
Booleans
Definition: Boolean values represent one of two values: True or False. They are used to represent the truth value of an expression.
Why: Booleans are essential for decision-making in programming. They are used in conditions and comparisons to control the flow of the program based on whether a statement is true or false.
Syntax
is_active = True
is_expired = False
Example
x = 10
print(x > 5) # Output: True
print(x < 3) # Output: False
Explanation
- The expression
x > 5is evaluated. Since 10 is greater than 5, the result isTrue. - The expression
x < 3is evaluated. Since 10 is not less than 3, the result isFalse.
Key Notes
- In Python, Boolean values must always start with a capital letter:
TrueandFalse. - Many expressions in Python evaluate to a Boolean, especially those using comparison operators (like
==,>, or<). - Almost any value can be evaluated as a Boolean; for example, most non-zero numbers are
True, while0and empty strings areFalse.
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute