It is an immutable data type.
Boolean represents the data type bool.
Boolean data type has only 2 values True and False.
a=True
print(a)
print(type(a))
''''
output:
True
<class 'bool'>
''''
Description:-
type(10) This statement displays that the variable a is a Boolean data type.
<class 'bool'> is a Boolean data type.
Note:-
When you use the values True and False the first letter must always be a capital letter, otherwise you will get an error.
Let's see how else this Boolean data type can be used.
True -> 1
False -> 0
Boolean Data Types Values
print(True+True)
''''
output:
2
''''
Description:-
The statement True + True displays a value of two on the output screen. This is because Boolean data type has 1 for True and 0 for False.
So when we add True + True we get a value of 2.
print(True+False)
''''
output:
1
''''
Description:-
Next when we add True + False we get the value 1.
print(True*15)
''''
output:
15
''''
Description:-
For the statement True*15, the output displays 15.
As we have already seen, True has a value of 1. That's why True*15 gives us a value of 15.
print(int(True))
print(int(False))
''''
output:
1
0
''''
Description:-
int(True) -> First it converts the value given inside the int function to an integer. Here we have given the value true inside the int() function. Since True has a value of 1, we get an output of 1.
int(False) -> False has a value of 0 so the int() function gives us 0 in output.
print(bool("good"))
print(bool())
''''
output:
True
False
''''
Description:-
bool(“good”) -> In this statement, the string “good” is given inside the method bowl, so it displays the value True on the output screen.
bool(“”) -> Here no value is given inside the bool(“”) method so only quotes are given so it gives us False value.
print(bool(["good"]))
print(bool([]))
''''
output:
True
False
''''
Description:-
bool([“good”]) -> Here a list is given inside the method called bool. Inside that list the list item called good is given. So the method called bool returns the value True.
bool([]) -> Here an empty list is given inside the bool() method, so the bool method() returns the value False.
print(bool((1,2,3)))
print(bool()))
''''
output:
True
False
''''
Description:-
bool((1,2,3)) – where a tuple is given inside the bool() method. Because the value (1,2,3) is given inside that tuple, the bool( ) method returns the value True.
bool(()) – Here an empty tuple is given inside the bool() method, so the bool() method returns the value False.
print(bool({1:"java"}))
print(bool({}))
''''
output:
True
False
''''
Description:-
bool({1:”Java”}) -> In this a dictionary is given inside the bool() method. The value {1:”Java”} is given inside this dictionary. So bool() method gives us True as output.
bool({}) -> Here an empty dictionary is given inside the bool method, so it returns False.