Lecture

Boolean Data Type for Determining True or False

To use conditional statements that run only when certain conditions are met, or loops that repeat until a condition becomes false, you must first determine whether a condition is True or False.

For example, consider writing a code that prints a is greater than 10 if the variable a is greater than 10, and prints a is 10 or less otherwise.

Boolean Usage Example
a = 11 if a > 10: # Executes if a is greater than 10 print("a is greater than 10.") else: # Executes if a is 10 or less print("a is 10 or less.")

Here, a > 10 is true if the variable a is greater than 10, and false otherwise.

The data type that represents true and false in this way is called a Boolean.


How to Use Booleans?

In Python, the value representing true is True, and the value representing false is False, with the first letter capitalized.

Boolean variable names often use prefixes like is_, has_, or can_ to make their purpose easier to understand.

Boolean Assignment
# Variable to determine active status is_active = True # Variable to determine login status is_logged_in = False # Variable to check certain permissions has_permission = True # Variable to check edit permissions can_edit = False

Example Usage of Booleans

Booleans are commonly used in conditional statements with if and else keywords to determine the truth or falsehood of a condition.

Using Booleans in if else Statement
is_active = True # Executes if is_active is True if is_active: print("The user is active.") else: print("The user is inactive.")
Quiz
0 / 1

In Python, the Boolean data type is expressed as true and false.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result