Difference between python built-in bool(), bytes() and bin() function with practical example

Python bool()

The python bool() converts a value to boolean(True or False) using the standard truth testing procedure.

Python bool() Example

test1 = []  
print(test1,'is',bool(test1))  
test1 = [0]  
print(test1,'is',bool(test1))  
test1 = 0.0  
print(test1,'is',bool(test1))  
test1 = None  
print(test1,'is',bool(test1))  
test1 = True  
print(test1,'is',bool(test1))  
test1 = 'Easy string'  
print(test1,'is',bool(test1))  

Output:

[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True

Python bytes()

The python bytes() in Python is used for returning a bytes object. It is an immutable version of the bytearray() function.

It can create empty bytes object of the specified size.

Python bytes() Example

string = "Hello World."  
array = bytes(string, 'utf-8')  
print(array)  

Output:

b ' Hello World.'

Python bin() Function

The python bin() function is used to return the binary representation of a specified integer. A result always starts with the prefix 0b.

Python bin() Function Example

x = 10
y = bin(x)
print (y)

Output:

0b1010