Last modified: Sep 10, 2026
Check If Function Returns True Python
In Python, many functions return a boolean value, either True or False. Understanding how to check these return values is essential for writing reliable code. This guide will walk you through several methods to determine if a function returns True.
Why Check Function Return Values
Functions often return True or False to indicate success, failure, or a condition. Checking these values helps control program flow and make decisions.
Using Conditional Statements
The most common way to check if a function returns True is with an if statement. Python treats True as truthy and False as falsy in conditionals.
def is_even(number):
# Returns True if number is even
return number % 2 == 0
# Check if function returns True
if is_even(4):
print("The number is even.")
else:
print("The number is odd.")
The number is even.
Explicit Comparison to True
You can also explicitly compare the return value to True. This is useful when clarity is important.
def is_positive(number):
# Returns True if number is positive
return number > 0
# Explicit check against True
result = is_positive(5)
if result == True:
print("The number is positive.")
The number is positive.
Using Identity Check is True
To strictly verify that a function returns exactly True (and not just a truthy value), use the is operator.
def always_true():
# Always returns the boolean True
return True
# Strict identity check
if always_true() is True:
print("Confirmed: function returned True.")
Confirmed: function returned True.
Storing and Inspecting Return Values
Sometimes you need to store the result first. This allows you to inspect or log the return value before making a decision.
def check_length(text):
# Returns True if text length is greater than 5
return len(text) > 5
# Store and inspect
result = check_length("hello world")
print(f"Return value: {result}")
if result:
print("Text is long enough.")
Return value: True
Text is long enough.
Handling Functions That Return None
Not all functions return True or False. Some return None by default. Always be aware of this to avoid unexpected behavior.
def do_something():
# No return statement, returns None
print("Doing something...")
# Check what the function returns
result = do_something()
print(f"Return value: {result}")
if result is None:
print("Function did not return True.")
Doing something...
Return value: None
Function did not return True.
Using Boolean Functions Safely
When working with functions that return True or False, ensure they always return a boolean. Avoid mixing types like returning 1 instead of True.
def is_admin(user):
# Always returns a boolean
return user == "admin"
# Safe usage
if is_admin("admin"):
print("Access granted.")
Access granted.
Combining with Other Boolean Logic
You can combine multiple function checks using logical operators like and, or, and not. This is useful for complex conditions.
def is_logged_in():
return True
def has_permission():
return False
# Combine checks
if is_logged_in() and has_permission():
print("User can access resource.")
else:
print("Access denied.")
Access denied.
Conclusion
Checking if a function returns True in Python is straightforward once you understand the available methods. Use if statements for general checks, explicit comparisons for clarity, and is True for strict identity verification. Always be mindful of functions that return None or non-boolean values. By following these practices, you can write cleaner and more reliable Python code.
For more details on boolean operations and logic in Python, check out our Python Boolean Combinations Guide and Python Booleans: True, False, Logic Guide.