Table of Contents
- How to return true or false in Python?
- Creating and using user-defined functions to return true or false in Python.
- Using Comparison Operators and The print Statement to Return True or False in Python.
- Using an if...else statement to return true or false in Python.
- Using the bool() function to return true or false in Python.
- Using an in-built function to return true or false in Python.
- Was this post helpful?
In this tutorial, we will see how to return true or false in Python.
There is often a need in programming to get to know the results in plain black or white, without any other uncertainties. This is where Boolean values come into play. Boolean returns either True
or False
, with no other values in between.
Boolean is one of the several built-in data types provided in Python and is utilized to return the values in the form of 0
or 1
. Any expression, when evaluated in Python, is capable of returning either a True
or a False
value. This tutorial demonstrates the different ways available to return True
or False
in Python.
How to return true or false in Python?
Creating and using user-defined functions to return true or false in Python.
We can simply create a user-defined function and program it in such a way that it generates the output as either True
or False
. This method might come in handy in complex program customizations.
The following code creates and uses a user-defined function to return true or false in Python.
1 2 3 4 5 |
def x() : return False print(x()) |
The above code provides the following output:
Using Comparison Operators and The print
Statement to Return True or False in Python.
We can simply utilize the comparison operators along with the print
statement to directly return a True
or a False
value as the output in Python.
The following code uses comparison operators and the print
statement to return true or false in Python.
1 2 3 4 |
print(27 > 16) print(27 < 16) |
The above code provides the following output:
False
Further reading:
Using an if...else
statement to return true or false in Python.
A condition, when run in an if...else
statement, is also capable of generating a True
or False
value in Python. It is often used along with relational operators.
The following code uses an if...else
statement to return true or false in Python.
1 2 3 4 5 6 7 8 |
x = 27 y = 16 if y > x: print(False) else: print(True) |
The above code provides the following output:
Using the bool()
function to return true or false in Python.
The bool()
function takes any value as its argument and returns either a True
or a False
value with the help of the generic truth testing procedure.
The bool()
function has a syntax that is simple to understand and is mentioned below.
1 2 3 |
bool([value]) |
The bool()
function generally takes a single parameter value, which could be anything. If no values are passed through the bool()
function, then it defaults to False
.
Most of the values in this function are considered to be True
, with the exception of a few values that are taken as False
, all of which are mentioned below for further clarity.
- Any values that come out to be
False
. - The value
None
. - If the passed value contains only zero’s of any given type. For Example,
0j
,0.00
, etc. - Any empty values. For example,
{}
,()
,[]
,''
. - Any class objects that return
False
or0
.
The following code uses the bool()
function to return true or false in Python.
1 2 3 4 |
print(bool([])) print(bool([27])) |
The above code provides the following output:
True
Using an in-built function to return true or false in Python.
Python provides several built-in functions that return only True
or False
values. Depending on the programmer’s needs, these functions can be utilized to return true or false in Python.
For example, we will take the isnumeric()
function which takes in a string and returns a True
value if the whole string contains only numeric values while returning a False
value if this criteria does not match.
The following code uses the isnumeric()
function to return true or false in Python.
1 2 3 4 |
x = "27" print(x.isnumeric()) |
The above code provides the following output:
That’s all about how to return true or false in Python,