Python True Keyword

In Python, True is a built-in Boolean keyword that represents the truth value of expressions. It is often used in conditional statements, loops, and logical operations.

Syntax

</>
Copy
True

Characteristics

PropertyDescription
TypeBoolean
ValueRepresents logical truth (1 in numeric operations).
UsageUsed in conditional expressions, loops, and logical operations.
ImmutabilityCannot be modified as it is a constant keyword.

Return Value

When used in expressions, True evaluates to 1 in numeric operations and acts as a truthy value in logical conditions.


Examples

1. Using True in Conditional Statements

In this example, we use True in an if statement to control the flow of execution.

Here, the if statement checks whether the condition is True. Since True always evaluates to a truthy value, the block of code inside the if statement runs.

</>
Copy
# Using True in an if statement
if True:
    print("This condition is always True!")

Output:

This condition is always True!

Since True is always considered as a truthy value, the print statement inside the if block is executed.

2. True as a Boolean Expression

Boolean expressions return either True or False. Here, we evaluate simple conditions.

In the example below, we compare two numbers using a relational operator. If the comparison is correct, it returns True.

</>
Copy
# Boolean comparison
result = 10 > 5
print(result)  # Output: True

Output:

True

Here, 10 > 5 is a logical expression that evaluates to True, and the value is stored in the variable result.

3. True in Logical Operations

Logical operations such as and, or, and not use True to determine the outcome.

In the following example, we combine Boolean values with logical operators to see how they interact.

</>
Copy
# Using True in logical expressions
print(True and False)  # Output: False
print(True or False)   # Output: True
print(not True)        # Output: False

Output:

False
True
False

In the first case, True and False results in False because both values must be True for the and operator to return True. In the second case, True or False results in True because at least one value is True. The not operator inverts True to False.

4. True in Numeric Operations

Since True is equivalent to 1 in Python, it can be used in arithmetic calculations.

In the example below, we use True in mathematical expressions, and Python treats it as 1.

</>
Copy
# True in arithmetic operations
print(True + 2)  # Output: 3
print(True * 5)  # Output: 5

Output:

3
5

Here, True + 2 is equivalent to 1 + 2, which results in 3. Similarly, True * 5 is the same as 1 * 5, giving 5.

5. Mistakenly Assigning True as a Variable

Since True is a built-in keyword, assigning a new value to it results in an error.

</>
Copy
# Attempting to assign a value to True
try:
    True = 5  # This will cause an error
except SyntaxError as e:
    print("Error:", e)

Output:

SyntaxError: cannot assign to True

Python does not allow modifying built-in keywords. Attempting to do so results in a SyntaxError.