How to use Python boolean data type and Operators?
The data type specifies what values a variable can accommodate and the operations that can be performed on them. As every Python type is a class, an instance of it is an object rather than a variable.
A boolean type can take either a True
or a False
value. The following example illustrates how to use a boolean variable.
x = True;
y = False;
#Check the type of x and y.
print(type(x));
print(type(y));
Type of x <class 'bool'>
Type of y <class 'bool'>
NOTE: The True and False keywords are case-sensitive. They must start with a capital letter. Ignoring the case (e.g., true or false) will give an error.
Integers/Floats values as Boolean-
The Integer and floating-point numbers have values of either zero or nonzero. If we convert an integer or floating-point value into a boolean type, a value zero converts into False and all others into True.
For conversion, we can use the bool() function, which takes an integer value as an argument and returns a boolean.
Below is an example of how integers and floating-point values can be converted into Boolean values.
A = 100;
B = -20;
C = 3.6;
D = -4.3;
E = 0;
F = 0.0;
Conversion into boolean
print("Boolean value of", A, " after conversion = ", bool(A));
print("Boolean value of", B, " after conversion = ", bool(A));
print("Boolean value of", C, " after conversion = ", bool(C));
print("Boolean value of", D, " after conversion = ", bool(D));
print("Boolean value of", E, " after conversion = ", bool(E));
print("Boolean value of", F, " after conversion = ", bool(F));
Boolean value of 100 after conversion = True
Boolean value of -20 after conversion = True
Boolean value of 3.6 after conversion = True
Boolean value of -4.3 after conversion = True
Boolean value of 0 after conversion = False
Boolean value of 0.0 after conversion = False
How to convert Boolean values into integers?
True and False Boolean constants represent One and Zero if converted into integers. The following example shows the conversion of Boolean values into integers.
A = True;
B = False;
print("Conversion of", A, " into an integer = ", int(A));
print("Conversion of", B, " into an integer = ", int(B));
Conversion of True into an integer = 1
Conversion of False into an integer = 0
Why is there a boolean type if 0/1 can do the job?
The keyword True and False look like it’s two integer values. So why is there a different type itself?
The reason is optimization. As for Boolean values, one bit is sufficient to store in the memory.
What are the Boolean Arithmetic operations?
All bit-level operations apply to the Boolean types. Arithmetic involves logical operations for single-bit fields with operators. The operators know boolean operators.
Boolean Operator | Meaning and Use |
---|---|
== | Meaning: Check Equality Usage: x == y, returns true if both x and y are the same. |
!= | Meaning: Check does not equal Usage: x != y, returns true if both x and y are not the same |
or | Meaning: Either Usage: x or y returns true if any of x or y is True. |
and | Meaning: Both Usage: x and y, return true if both x and y are True. |
not | Meaning: Flip the value Usage: not x, returns true if x is false or returns false if x is true. |
Here is a Python example demonstrating the use of all Boolean operators.
A = True;
B = False;
print(" Operator == , applies on A and B result = ", A == B);
print(" Operator != applies on A and B result = ", A != B);
print(" Operator and , applies on A and B result = ", A and B);
print(" Operator or , applies on A and B result = ", A or B);
print(" Operator not , applies on A result = ", not A);
print(" Operator or , applies on B result = ", not B);
Operator == , applies on A and B result = False
Operator != applies on A and B result = True
Operator and , applies on A and B result = False
Operator or , applies on A and B result = True
Operator not , applies on A result = False
Operator or , applies on B result = Truee