True
In Python, the True keyword represents a Boolean value indicating a truth value in logical operations and expressions. It’s defined as a built-in constant that’s an instance of the bool type, which is a subclass of int, and it has a numeric value of 1.
Python True Keyword Examples
Here’s an example demonstrating how the True keyword can be used in Python:
>>> is_adult = True
>>> if is_adult:
... print("They're more than 18 years old")
... else:
... print("They're less than 18 years old")
...
They're more than 18 years old
In this example, the conditional checks the value of is_adult. Because it’s True, the if clause runs.
Python True Keyword Use Cases
- Logical operations and comparisons to indicate a true condition.
- Control flow statements like
ifandwhileto dictate the execution of code blocks. - Boolean flags in functions to toggle features or options.
- Default argument value in functions and methods
Related Resources
Tutorial
Python Booleans: Use Truth Values in Your Code
In this tutorial, you'll learn about the built-in Python Boolean data type, which is used to represent the truth value of an expression. You'll see how to use Booleans to compare values, check for identity and membership, and control the flow of your programs with conditionals.
For additional information on related topics, take a look at the following resources:
- Python Keywords: An Introduction (Tutorial)
- Conditional Statements in Python (Tutorial)
- Python while Loops: Repeating Tasks Conditionally (Tutorial)
- Python Booleans: Leveraging the Values of Truth (Course)
- Exploring Keywords in Python (Course)
- Python Keywords: An Introduction (Quiz)
- Conditional Statements in Python (if/elif/else) (Course)
- Python Conditional Statements (Quiz)
- Mastering While Loops (Course)
- Python while Loops: Repeating Tasks Conditionally (Quiz)