Use a Boolean variable inside the function that always returns a value True or False in Python. You can set the initial value of the variable to true and false, then change it according to the conditions.
Example function returns Only True or False value in Python
A simple example code gets the true or false return value.
def foo(a, b):
answer = False
if a > b:
answer = True
return answer
print(foo(12,3))
Output:
Another example
def is_even(number):
if number % 2 == 0:
return True
else:
return False
result = is_even(4)
print(result) # Output: True
result = is_even(7)
print(result) # Output: False
Sure! Here’s an example of a Python function that only returns either True
or False
:
pythonCopy codedef is_even(number):
if number % 2 == 0:
return True
else:
return False
In this example, the function is_even
takes a number as an argument and checks if it is even. If the number is divisible by 2 without a remainder, the function returns True
. Otherwise, it returns False
.
Do comment if you have any doubts or suggestions on this Python function program.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.