Python Boolean (original) (raw)

Last Updated : 05 Dec, 2024

**Python **Boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False. Generally, it is used to represent the truth values of the expressions.

Python Boolean Type

Boolean value can be of two types only i.e. either True or False. The output ****<class ‘bool’>** indicates the variable is a Boolean data type.

Python `

a = True print(type(a))

b = False print(type(b))

`

Output

<class 'bool'> <class 'bool'>

Let’s take a detailed look at Python Boolean:

Table of Content

Evaluate Variables and Expressions

We can evaluate 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, using the standard truth testing procedure.

Python bool() Function

In Python, the bool() function is used to convert a value or expression to its corresponding Boolean value (True or False). In the example below the variable res will store the boolean value of False after the equality comparison takes place.

Python `

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 = 'GeeksforGeeks' print(bool(x))

`

Output

False False False False True

Note: It’s not always necessary to use bool() directly. Python automatically evaluates expressions in terms of their Boolean values, especially when used in conditions like if statements or loops.

**Integers and Floats as Boolean

In Python, integers and floats can be used as Boolean values with the bool() function. Any number with a value of zero (0, 0.0) is considered False while any non-zero number (positive or negative) is considered True.

Python `

var1 = 0 print(bool(var1))

var2 = 1 print(bool(var2))

var3 = -9.7 print(bool(var3))

`

**Boolean Operators

Boolean Operations in Python are simple arithmetic of True and False values. These values can be manipulated by the use of boolean operators which include **AND or and NOT. Common boolean operations are –

Boolean OR Operator

Boolean or operator returns True if any one of the inputs is True else returns False.

Python `

a = 5 b = 3 c = 8

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

`

**Explanation:

Boolean And Operator

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

**Example:

Python `

a = 0 b = 2 c = 4

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

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

`

**Explanation:

Boolean Not Operator

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.

**Example:

Python `

a = 0

if not a: print("False")

`

**Explanation:

Python 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.

**Example:

Python `

a = 0 b = 1

if a == 0: print(True)

if a == b: print(True)

if a != b: print(True)

`

**Explanation:

Python is Operator

is keyword 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.

**Example:

Python `

x = 10 y = 10

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

`

**Explanation:

Python in Operator

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

Python `

Create a list

a = [1, 2, 2]

Check if lion in list or not

if 1 in a: print(True)

`

**Explanation:

Suggested Quiz

10 Questions

What does bool(0) return?

Explanation:

In Python, 0 is considered a "falsey" value, which means bool(0) evaluates to False.

Which of the following is considered a "truthy" value in Python?

Explanation:

In Python, any non-zero integer is considered "truthy," so 1 evaluates to True when converted to a boolean.

What is the result of bool(None)?

Explanation:

None is considered a "falsey" value in Python, thus bool(None) returns False.

In Python, which operator checks if two variables point to the same object in memory?

Explanation:

In Python, the is operator checks if two variables point to the same object in memory. This operator returns True if the variables are indeed referencing the same object, indicating they are identical in terms of memory location.

What is the result of not True?

Explanation:

The not operator negates the boolean value, so not True is False.

How does Python evaluate the expression bool(0) and bool(1)?

Explanation:

bool(0) is False and bool(1) is True, but the and operator requires both operands to be True to return True.

Evaluate the result of False and not False?

Explanation:

not False is True, but since False and True evaluates to False, the overall result is False.

Which of the following statements is correct regarding the comparison operators in Python?

Explanation:

The == operator is used to compare the values of two objects for equality. It assesses whether the contents or data of the objects are equivalent, regardless of whether they are the same object in memory.

What is the result of the code snippet: not any([False, False, False])?

Explanation:

The any() function returns False if all elements are false. not False then converts this result to True.

What will all([True, True, False]) return?

Explanation:

The all() function returns True only if all elements in the iterable are True. Since there is a False in the list, the result is False.

Quiz Completed Successfully

Your Score : 2/10

Accuracy : 0%

Login to View Explanation

1/10

1/10 < Previous Next >