DEV Community

Cover image for PYTHON DATA TYPES[BOOLEAN]
Christopher Ambala
Christopher Ambala

Posted on

PYTHON DATA TYPES[BOOLEAN]

The Python Boolean type has only two possible values:

  • True
  • False No other value will have bool as its type. You can check the type of True and False with the built-in type():
>>> type(False)
<class 'bool'>
>>> type(True)
<class 'bool'>

Input: 1==1
Output: True 

Input: 2<1 
Output: False
Enter fullscreen mode Exit fullscreen mode

Evaluate Variables and Expressions

Bools can be evaluated values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False,

bool([x])
Enter fullscreen mode Exit fullscreen mode

Python bool() Function

Booleans can be evaluated using the bool() function as:

# Python program to illustrate
# built-in method bool()

# Returns False as x is not equal to y
x = 5
y = 10
print(bool(x==y))

# Returns False as x is None
x = None
print(bool(x))

# Returns False as x is an empty sequence
x = ()
print(bool(x))

# Returns False as x is an empty mapping
x = {}
print(bool(x))

# Returns False as x is 0
x = 0.0
print(bool(x))

# Returns True as x is a non empty string
x = 'Greatest'
print(bool(x))

Enter fullscreen mode Exit fullscreen mode

Output:

False
False
False
False
False
True
Enter fullscreen mode Exit fullscreen mode

Convertion of Integers and Floats as Booleans

Numbers can be converted as bool values by using Python’s built-in bool() method. Any integer, floating-point number, or complex number having zero as a value is considered as False, while if they are having value as any positive or negative number then it is considered as True.

var1 = 0
print(bool(var1))

var2 = 1
print(bool(var2))

var3 = -9.7
print(bool(var3))
Enter fullscreen mode Exit fullscreen mode

Output:

False
True
True
Enter fullscreen mode Exit fullscreen mode

Boolean Operators

  • or
  • and
  • not
  • == (equivalent)
  • != (not equivalent) ###Boolean OR Operator The Boolean or operator returns True if any one of the inputs is True else returns False. ###syntax:
# Python program to demonstrate
# or operator

a = 1
b = 2
c = 4

if a > b or b < c:
    print(True)
else:
    print(False)

if a or b or c:
    print("Atleast one number has boolean value as True")
Enter fullscreen mode Exit fullscreen mode

Output:

True
Atleast one number has boolean value as True
Enter fullscreen mode Exit fullscreen mode

Boolean And Operator

The Boolean operator returns False if any one of the inputs is False else returns True.
syntax:

# Python program to demonstrate
# and operator

a = 0
b = 2
c = 4

if a > b and b<c:
    print(True)
else:
    print(False)

if a and b and c:
    print("All the numbers has boolean value as True")
else:
    print("Atleast one number has boolean value as False")
Enter fullscreen mode Exit fullscreen mode

output:

False
Atleast one number has boolean value as False
Enter fullscreen mode Exit fullscreen mode

Boolean Not Operator

The Boolean Not operator only requires one argument and returns the negation of the argument i.e. returns the True for False and False for True.
syntax:

# Python program to demonstrate
# not operator

a = 0

if not a:
    print("Boolean value of a is False")
Enter fullscreen mode Exit fullscreen mode

output:

Boolean value of a is False
Enter fullscreen mode Exit fullscreen mode

Boolean == (equivalent) and != (not equivalent) Operator

Both operators are used to compare two results. == (equivalent operator returns True if two results are equal and != (not equivalent operator returns True if the two results are not same.
syntax:

# Python program to demonstrate
# equivalent an not equivalent
# operator

a = 0
b = 1

if a == 0:
    print(True)

if a == b:
    print(True)

if a != b:
    print(True)
Enter fullscreen mode Exit fullscreen mode

output:

True
True
Enter fullscreen mode Exit fullscreen mode

Python is Operator

Is is used to test whether two variables belong to the same object. The test will return True if the two objects are the same else it will return False even if the two objects are 100% equal.
syntax:

# Python program to demonstrate
# is keyword


x = 10
y = 10

if x is y:
    print(True)
else:
    print(False)

x = ["a", "b", "c", "d"]
y = ["a", "b", "c", "d"]

print(x is y)
Enter fullscreen mode Exit fullscreen mode

output:

True
False
Enter fullscreen mode Exit fullscreen mode

Python in Operator

in operator checks for the membership i.e. checks if the value is present in a list, tuple, range, string.
syntax:

# Python program to demonstrate
# in keyword

# Create a list
animals = ["dog", "lion", "cat"]

# Check if lion in list or not
if "lion" in animals:
    print(True)
Enter fullscreen mode Exit fullscreen mode

output:

True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)