![]() |
VOOZH | about |
Boolean is a built-in data type that represents only two values: True and False. It is commonly used to check conditions and represent the truth value of expressions in a program.
<class 'bool'> <class 'bool'>
We can check or convert values into Boolean form using the bool() function. It returns either True or False based on the value of the expression.
1. bool() Function: converts any value or expression into its corresponding Boolean value.
False False False False True
Explanation: Empty values like None, (), {} and 0 are treated as False and non-empty strings or values are treated as True.
Note: Python automatically evaluates values as True or False in conditions like if statements, so using bool() explicitly is not always required.
2. Integers and Floats: numbers can also behave like Boolean values. Zero is considered False, while any non-zero number is considered True.
False True True
Boolean operators are used to perform logical operations on True and False values. These operators help in decision-making in programs.
1. OR Operator: returns True if at least one condition is True.
True
Explanation:
2. AND Operator: returns True only if all conditions are True, otherwise it returns False.
False False
Explanation:
3. NOT Operator: reverses the Boolean value of an expression.
False
Explanation: 0 is considered False, not False becomes True, so the condition executes.
4. Equality Operators (== and !=): == operator returns True if both values are equal, while the != operator returns True if the values are not equal.
True True
Explanation:
5. Identity Operator (is): checks whether two variables refer to the same object in memory.
True
Explanation: Both variables refer to the same memory object, so x is y is True.
6. Membership Operator (in): checks whether a value exists inside a sequence like a list, tuple, or string.
True
Explanation: 1 is present in the list, so the condition becomes True.