Python function always returns a value True or False | Code

Assign the Boolean value to variable and return it, it will make the function always returns a value True or False in Python.

def foo(x, y):
    answer = False
    if x > y:
        answer = True
    else:
        answer = False
    return answer


print("X is Greater then Y: ", foo(12, 5))

Output:

Python function always returns a value True or False

Another example

A function that always returns True (or False) for every parameter (or even multiple parameters).

false = lambda *_: False
true  = lambda *_: True

Do comment if you have any doubts and suggestions on this Python example code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.