Codectionary / Developer documentation / Python

True

True is one of Python’s two Boolean values. It represents an affirmative result or state, such as a successful comparison or an enabled setting. Conditions also accept other truthy objects; an object does not have to be the literal True to enter an if branch.

How it works

Use a Boolean name such as is_ready to describe a yes/no state. A comparison such as count > 0 calculates that state from data. The bool() function explicitly converts a value using its truth-value rules.

True must have a capital T. The string "True" is text, not a Boolean; it is truthy because it is nonempty. bool is a subclass of int, so True == 1, but this does not mean True is the same object as the integer 1.

while True starts an unconditional repetition. A reachable break, return or exception must end it if it is not intended to run forever. Terminal input examples need a console; they are not browser prompt demonstrations.

Common mistakes and boundaries

Write if is_ready when you mean truthiness. Comparing == True can exclude truthy objects that are not equal to True.

A simple shape check on text is not a complete security validation. The email example deliberately demonstrates Boolean combination only; it does not establish deliverability or safe registration.

Try it yourself

Compare True, 1, "True", "False" and "" using bool(). Explain why both nonempty strings are truthy even though their words disagree.

Syntax

True

Examples

Direct Assignment

Assigning True directly to a variable for boolean flags and state management.

is_authenticated = True
feature_enabled = True

if is_authenticated:
    print("User is logged in")

if feature_enabled:
    print("Feature is active")
  1. Both flags are true, so both branches run. Names describe the states being tested.
  2. Expected output: User is logged in Feature is active

Conditional Statements

Using True in if statements to control program flow based on conditions.

age = 25
is_adult = age >= 18  # Evaluates to True

if is_adult:
    print("Access granted")
else:
    print("Access denied")

# Multiple conditions
has_permission = True
is_verified = True

if has_permission and is_verified:
    print("You can proceed")
  1. The age comparison creates a Boolean; the second decision requires both Boolean flags.
  2. Expected output: Access granted You can proceed

While Loops

Using True to create continuous loops that run until explicitly broken.

count = 0

while True:
    response = input("Type 'quit' to exit: ")
    if response.lower() == 'quit':
        break
    count += 1
    print(f"You have entered {count} commands")

print("Loop exited")
  1. In a local terminal, enter hello and then quit. Each non-quit command increments the counter, while quit reaches break. The sample transcript excludes typed-input echo.
  2. Expected output with the sample input below: Type 'quit' to exit: You have entered 1 commands Type 'quit' to exit: Loop exited
  3. Run in a local terminal or an editor with console input. Sample input, one response per line: hello quit

Logical Operations

Combining True with boolean operators (and, or, not) for complex logic.

# AND operator - both must be True
result1 = True and True   # True
result2 = True and False  # False

# OR operator - at least one must be True
result3 = True or False   # True
result4 = False or False  # False

# NOT operator - inverts the value
result5 = not True        # False
result6 = not False       # True

# Complex expression
is_weekend = True
has_money = True
is_sunny = False

go_out = is_weekend and has_money and (is_sunny or not is_sunny)
print(f"Should go out: {go_out}")
  1. The Boolean combinations demonstrate and, or and not; here every operand is already Boolean. The final expression is true because is_sunny or not is_sunny is always true for this Boolean.
  2. Expected output: Should go out: True

Comparisons

How comparisons return True when the evaluated condition meets the criteria.

# Numeric comparisons
print(10 > 5)      # True
print(100 >= 100)  # True
print(5 == 5)      # True

# String comparisons
print("hello" == "hello")  # True
print("abc" < "xyz")       # True

# Membership testing
fruits = ["apple", "banana", "orange"]
print("apple" in fruits)  # True

# Type checking
print(isinstance(42, int))  # True
  1. These ordinary scalar comparisons, membership tests and isinstance checks all produce True for the chosen inputs.
  2. Expected output: True True True True True True True

Input Validation

Using True in validation scenarios to verify user input or data integrity.

def has_email_shape(text):
    # Teaching example only, not complete email validation.
    return "@" in text and "." in text and len(text) > 5

if has_email_shape("user@example.com"):
    print("Toy shape check passed")
  1. The toy check combines three conditions. It accepts this sample but also accepts malformed addresses; never treat these three conditions as complete email validation.
  2. Expected output: Toy shape check passed

Best practices

  • Use True directly in conditions rather than comparing (if is_active: instead of if is_active == True:)
  • Choose descriptive boolean variable names that read like questions (is_valid, has_permission, can_access)
  • Remember that True is case-sensitive - it must be capitalized in Python
  • Avoid redundant comparisons with True/False - the variable itself is already boolean
  • Use True in while loops with break conditions for continuous processes
  • Leverage True in complex boolean expressions with and, or, not operators for readable logic

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading