PYTHON

Python True: The True Keyword in Python

In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations.

How to Use True in Python

You can assign True directly to a variable or get True as a result of comparisons or logical operations.

is_authenticated = True
is_positive = 100 > 0

When to Use True in Python

The True keyword is fundamental when you need to make decisions based on truth conditions. Here are some common applications:

Conditional Statements

True is essential for if statements and other conditional statements in Python. If the condition of an if statement evaluates to True, the block of code below the if statement executes. Otherwise the lines of code below the else statement execute.

feature_enabled = True
if feature_enabled:
    print("Feature is on.")  # This line executes
else:
    print("Feature is off.")

Comparisons

Comparisons return True if the evaluated condition meets the criteria set by the comparison operator.

For example, in a benefit-check scenario, you might compare the age of a user with a certain threshold age.

age = 70
if age >= 65:
		print("Qualifies for senior benefits.")  # This line will execute because the comparison evaluates to True

While Loops

In a Python while loop, a condition needs to be True for the loop to continue or execute in the first place.

At some point, however, the condition needs to become False, or the loop needs to trigger a break statement. If the condition always evaluates to True, the loop becomes infinite ("infinite loop").

while True:
    response = input("Enter 'quit' to exit: ")
    if response == 'quit':
        break

Logical Operations

Logical operations with boolean operators allow you to combine, invert, or compare boolean expressions. The three boolean operators in Python are and, or, and not.

True plays a key role in logical operations, helping to evaluate the truth of expressions.

a = True
b = False
result1 = a and b  # Result is False because both operands are not True
result2 = a or b  # Result is True because one operand is True
result3 = a and not b  # Result is True because a is True and b is not True

Examples of True in Python

You’ll find the True keyword in virtually every Python application. Here are some popular examples:

Input Validation

True often signifies a valid condition in input validation scenarios.

user_input = "valid"
while True:
    if user_input:
        break  # Exit if input is non-empty
    user_input = input("Please enter a valid input: ")

User Authentication

True can control the flow of execution within a program, for example based on a user’s authentication status.

logged_in = True
if logged_in:
    print("You're logged in.")
else:
		print("You need to log in.")

Function Return Values

Functions might return True to indicate success or validity.

def check_prime(number):
    if number > 1:
        for i in range(2, number):
            if (number % i) == 0:
                return False
        return True
    else:
        return False

print(check_prime(11))  # Output: True

Learn More About True in Python

Python False to True Conversion

You can convert False to True using the not operator. Inverting is useful when you need the opposite of a boolean expression or value.

is_active = True
is_inactive = not is_active  # Converts True to False

True in Data Structures

Within data structures, True often represents the presence or truth of an element.

user_permissions = {"read": True, "write": False}  # 'read' permission is True

Truthy Values in Python

Besides the explicit True keyword, the Python programming language evaluates several other values as True in logical operations. These "truthy" values include non-zero numbers, non-empty strings, lists, tuples, dictionaries, and sets.

Using truthy values makes Python code concise and expressive, enabling easier and more readable condition checks.

my_list = [1, 2, 3]

if my_list:  # Evaluates to True because the list is not empty
    print("The list is not empty.")
Learn Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2024 Mimo GmbH