Exercise: Python

Questions for: Operators

What is the result of the expression: True and False ?
A:
True
B:
False
C:
None
D:
Error
Answer: B

The and operator in Python is a logical operator that returns True if both operands are true, and False otherwise.

In this case, True and False are the operands, and since the second operand is false, the result is False.

What is the result of the following code?
a = 8
b = 3
print(a / b)
A:
2.6666666666666665
B:
2.6666666666666667
C:
2.67
D:
2.6
Answer: A
The division operator returns a float value in Python 3.x. In this case, a / b is equal to 2.6666666666666665.
What is the output of the following code?
a = 5
b = 3
c = 7
print(a + b * c)
A:
26
B:
56
C:
22
D:
36
Answer: A
The multiplication operator has higher precedence than the addition operator, so b * c is evaluated first and then added to a.
a + b * c
= 5 + 3 * 7
= 5 + 21
= 26
What is the output of the following code?
x = 10
y = 5
result = x != y and x > y

print(result)
A:
True
B:
False
C:
None
D:
Error
Answer: A

The != operator is used to check if two values are not equal. In the code, x != y means 10 is not equal to 5, which is True.

The and operator returns True only if both expressions are True.

So, the expression x != y and x > y becomes True and True, which evaluates to True.

What is the output of the following code?
x = True
y = False
result = not x or y

print(result)
A:
True
B:
False
C:
None
D:
Error
Answer: B
The not operator is used to negate the value of a boolean expression. In the code, not x means not True, which is False. The or operator returns True if at least one of the expressions is True. So, the expression not x or y becomes False or False, which evaluates to False.
Ad Slot (Above Pagination)
Quiz