Python Booleans

In this python tutorial, we will discuss Python Booleans. We will also check:

  • What is Boolean in python?
  • Python Boolean types
  • Where to use Boolean in python?
  • Function return Boolean in python
  • True and false in python
  • Boolean string in python
  • Python boolean variable
  • Python boolean logical operators
  • Python concatenate a boolean to a string
  • Python convert boolean to string
  • Python convert boolean to integer
  • Python convert list of booleans to integer
  • Python boolean check for null
  • Python boolean if in list

Let us check out Python Boolean and its types.

What is Boolean in python?

  • In python, Boolean is a data type that is used to store two values True and False.
  • In python, we can evaluate any expression and can get one of two answers.
  • While comparing two values the expression is evaluated to either true or false.
  • Bool is used to test the expression.

Python Boolean types

The type bool is built-in which means it is available in python and doesn’t need to import. The Python Boolean are of only two types:

  1. True
  2. False

Where to use Boolean in python?

In python, Boolean can be used where there is a need to compare two values. After comparing the values it returns either true or false.

You can refer to below example:

my_string = "Hello Sam"
print(my_string.isalnum())

You can refer to the below screenshot:

Where to use Boolean in python
Where to use Boolean in python

Function return Boolean in python

Let us see how to create a function in Python that will return a Boolean value. Here we created the function “def myFunction()” and it return True.

Example:

def myFunction():
  return True
print(myFunction())

You can refer to the below screenshot function return Boolean in python.

Function return Boolean in python
Function return Boolean in python

True or false in python

When we compare two values, the expression is evaluated and it returns the Boolean answer which is either true or false.

Example:

a = 10
b = 12
print(a>b)

After writing the above code (true or false in python), Once we will print then the output will appear as “ False ”. Here, a is not greater than b so it returns the output as false.

You can refer to the below screenshot for true or false in python.

True or false in python
True or false in python

Boolean string in python

In python, string can be tested for Boolean values. Its return type will be either true or false.

Example:

my_string = "Hello David"
print(my_string.endswith('d'))

After writing the above code (Boolean string in python), Once we will print then the output will appear as “ True ”. Here, a variable is created and it has a value that is checked when we are printing. The string ends with ‘d’ so it returns True.

You can refer to the below screenshot Boolean string in python.

Boolean string in python
Boolean string in python

Python boolean variable

Here, we can see python boolean variable.

The bool() function allows you to evaluate any value, and it return True or False.

Example:

var = "Python"
print(bool(var))

After writing the above code (python boolean variable), Once you will print “bool(var)” then the output will appear as “ True ”. Here, a variable is created and the function will evaluate the value and returns True.

You can refer to the below screenshot python boolean variable.

Python boolean variable
Python boolean variable

Python boolean logical operators

Logical operators in python are used for conditional statements that are True or False. Logical operators in python are AND, OR, and Not.

  1. And operator – It returns True if both the operands right side and left side are True.

Example:

x = 8
print(x > 4 and x < 10)

After writing the above code (python boolean AND operators), Once you will print then the output will appear as “ True ”. Here, the AND operator is used, and it will return True because 8 is greater than 4 and 8 is less than 10.

You can refer to the below screenshot python boolean AND operators.

Python boolean logical operators
And operator

2. Or operator – It returns True if either of the operand right side or left side is True.

Example:

x = 6
print(x > 5 or x < 2)

After writing the above code (python boolean Or operators), Once you will print then the output will appear as “ True ”. Here, the Or operator is used, and it will return True because one of the conditions is True. So, 6 is greater than 5, but 6 is not less than 2.

You can refer to the below screenshot python boolean Or operators.

Python boolean logical operators
OR operator

3. Not operator – It returns True if operand is false.

Example:

x = 6
print(not(x > 5 and x < 10))

After writing the above code (python boolean Not operator), Once you will print then the output will appear as “ False ”. Here, the Not operator is used, and it will return False because not is used to reverse the result.

You can refer to the below screenshot python boolean Not operator.

Python boolean logical operators
Not operator

Python concatenate a boolean to a string

To concatenate a boolean to a string, we will first convert boolean to a string and then it will be appended to the another end.

Example:

my_string = "The value is"
boolean_v = True
str_concat = my_string + str(boolean_v)
print(str_concat)

After writing the above code (python concatenate a boolean to a string), Once you will print “str_concat” then the output will appear as “ The value is True ”. Here, str(boolean_v) is used to convert boolean to string. The “+” operator is used to concatenate the value to a string.

You can refer to the below screenshot python concatenate a boolean to a string.

Python concatenate a boolean to a string
Python concatenate a boolean to a string

Python convert boolean to string

To convert boolean to string in python, we will use str(bool) and then it will be converted to string.

Example:

bool = True
my_string = str(bool)
print(my_string)
print(type(my_string))

After writing the above code (python convert boolean to string), Once you will print “my_string and type(my_string)” then the output will appear as “ True <class ‘str’>”. Here, str(bool) is used to convert boolean to string.

You can refer to the below screenshot python convert boolean to string.

Python convert boolean to string
Python convert boolean to string

Python convert boolean to integer

To convert boolean to integer in python, we will use int(bool) and then it will be converted to integer.

Example:

bool = True
my_integer = int(bool)
print(my_integer)
print(type(my_integer))

After writing the above code (python convert boolean to integer), Once you will print “my_integer and type(my_integer)” then the output will appear as “ 1 <class ‘int’>”. Here, int(bool) is used to convert boolean to an integer value.

You can refer to the below screenshot python convert boolean to integer.

Python convert boolean to integer
Python convert boolean to integer

Python booleans count the number of True in a list

Counting the number of True booleans from the list returns the number of boolean objects that evaluate to True. A boolean object with a True value evaluates to 1 in the sum() function and it will return the count of True boolean from the list.

Example:

bool_list = [True, False, True, False, True, True]
count_t = sum(bool_list)
print(count_t)

After writing the above code (python booleans count the number of True in a list), Once you will print “count_t” then the output will appear as “ 4 ”. Here, sum(bool_list) is used to count the number of True booleans in a list.

You can refer to the below screenshot python boolean count the number of True in a list.

Python booleans count the number of True in a list
Python booleans count the number of True in a list

Python convert list of booleans to integer

Converting a list of booleans to integers, we will first convert a list of boolean into an integer. So, we will use a map(function, iterable).

Example:

bool_list = [True, False]
convert = map(int, bool_list)
int_list = list(convert)
print(int_list)

After writing the above code (python convert list of booleans to integer), Once you will print “int_list” then the output will appear as “ [1, 0] ”. Here, to convert a list of boolean to integer we will use list(iterable) with map() as iterable to convert to a list.

You can refer to the below screenshot python convert list of booleans to integer.

Python convert list of booleans to integer
Python convert list of booleans to integer

Python boolean check for null

There is no null in python, instead there is None. For checking for None as a value we will use “is” identity operator. It will check whether the variables refer to the underlying object.

Example:

value = None
print(value is None)

After writing the above code (python boolean check for null), Once you will print “value is None” then the output will appear as “ True ”. It will check whether the value is None or not. If the value is None then it will return True otherwise False.

You can refer to the below screenshot python boolean check for null.

Python boolean check for null
Python boolean check for null

Python boolean function

The name of the boolean function is isdivisible. The isdivisible returns either True or False to indicate whether the a is or not divisible by b.

Example:

def isdivisible(a, b):
    if a % b == 0:
        result = True
    else:
        result = False
    return result
print(isdivisible(20, 5))

After writing the above code (python boolean function), Once you will print “isdivisible” then the output will appear as “ True ”. Here, the result of any expression evaluation can be returned by a function “using return statement”. The function will return boolean values.

You can refer to the below screenshot python boolean function.

Python boolean function
Python boolean function

Python boolean if in list

It checks whether the items evaluate to True. The expression True in list will return a non-iterable boolean value.

Example:

list = [True, False, False]
print(True in list)

After writing the above code (python boolean if in list), Once you will print “True in list” then the output will appear as “ True ”.

You can refer to the below screenshot python boolean if in list

Python boolean if in list
Python boolean if in list

You may like the following Python tutorials:

In this Python tutorial, we have learned about Python Booleans. Also, We covered these below topics:

  • What is Boolean in python?
  • Python Boolean types
  • Where to use Boolean in python?
  • Function return Boolean in python
  • True and false in python
  • Boolean string in python
  • Python boolean variable
  • Python boolean logical operators
  • Python concatenate a boolean to a string
  • Python convert boolean to string
  • Python convert boolean to integer
  • Python convert list of booleans to integer
  • Python boolean check for null
  • Python boolean if in list