Python: Logical Operators

Question Click to View Answer

What does the following code print to the console?

print(4 == 99)

False is printed to the console because 4 is not equal to 99.

In this example, 4 and 99 are the operands and == is the operator.

What does the following code print to the console?

print(10 == 10)

True is printed to the console because 10 equals 10.

The == operator returns True when both operands are the same.

What does the following code print to the console?

print("cat" != "dog")

True is printed to the because the string "cat" is not the same as the string "dog".

The != operator returns True when the operands are different.

What does the following code print to the console?

print("mouse" != "mouse")

False is printed because the string "mouse" is equivalent to the string "mouse".

The != operator returns False when the operands are the same and True when the operands are different.

What does the following code print to the console?

print(True and True)

True is printed to the console.

The and operator returns True when both of the operands are True.

What does the following code print to the console?

print(False and True)

False is printed to the console.

The and operator returns False when either one of the operands is False.

What does the following code print to the console?

print(False or True)

True is printed to the console.

The or operator returns True when either one of the operands is True.

What does the following code print to the console?

print(False or False)

False is printed to the console.

The or operator returns False when both of the operands are False.

What does the following code print to the console?

print(not True)

False is printed to the console.

In this example, not is the operator and True is the operand. not returns False when the operand is True.

What does the following code print to the console?

print(not False)

True is printed to the console.

not returns True when the operand is False.

What does the following code print to the console?

print(True and (4 == 4))

True is printed to the console.

4 == 4 evaluates to True.

True and True evaluates to True for the final result.

What does the following code print to the console?

print("lion" == "cat" or 99 != 88)

True is printed.

"lion" == "cat" evaluates to False and 99 != 88 evaluates to True.

False or True evaluates to True for the final result.