PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 11
Q439 - Data Types
What is the expected output of the following code?
- print(not 0)
- print(not 23)
- print(not '')
- print(not 'Peter')
- print(not None)
-
A
- False
- False
- True
- False
- True
-
B
- True
- False
- True
- False
- True
-
C
- True
- False
- False
- False
- True
-
D
- True
- False
- True
- False
- False
Reveal correct answer
Correct answer: B
Explanation
Topics: not boolean
Try it yourself:
- print(not 0) # True
- print(not 23) # False
- print(not '') # True
- print(not 'Peter') # False
- print(not None) # True
Explanation:
The values 23 and 'Peter' will evaluate to True
and the rest will evaluate to False
The not turns everything around.
The values that become False in Python are the following:
- print(bool('')) # False
- print(bool(0)) # False
- print(bool(0.0)) # False
- print(bool(0j)) # False
- print(bool(None)) # False
- print(bool([])) # False
- print(bool(())) # False
- print(bool({})) # False
- print(bool(set())) # False
- print(bool(range(0))) # False
Q439 (Please refer to this number, if you want to write me about this question.)
A. The 'not' operator in Python returns the boolean opposite of the operand. In this code snippet, 'not 0' evaluates to True because 0 is considered False in Python, so the opposite is True. 'not 23' evaluates to False because 23 is considered True, so the opposite is False. 'not '' ' evaluates to True because an empty string is considered False, so the opposite is True. 'not 'Peter' ' evaluates to False because a non-empty string is considered True, so the opposite is False. 'not None' evaluates to True because None is considered False, so the opposite is True.
B. The 'not' operator in Python returns the boolean opposite of the operand. In this code snippet, 'not 0' evaluates to True because 0 is considered False in Python, so the opposite is True. 'not 23' evaluates to False because 23 is considered True, so the opposite is False. 'not '' ' evaluates to True because an empty string is considered False, so the opposite is True. 'not 'Peter' ' evaluates to False because a non-empty string is considered True, so the opposite is False. 'not None' evaluates to True because None is considered False, so the opposite is True.
C. The 'not' operator in Python returns the boolean opposite of the operand. In this code snippet, 'not 0' evaluates to True because 0 is considered False in Python, so the opposite is True. 'not 23' evaluates to False because 23 is considered True, so the opposite is False. 'not '' ' evaluates to True because an empty string is considered False, so the opposite is True. 'not 'Peter' ' evaluates to False because a non-empty string is considered True, so the opposite is False. 'not None' evaluates to True because None is considered False, so the opposite is True.
D. The 'not' operator in Python returns the boolean opposite of the operand. In this code snippet, 'not 0' evaluates to True because 0 is considered False in Python, so the opposite is True. 'not 23' evaluates to False because 23 is considered True, so the opposite is False. 'not '' ' evaluates to True because an empty string is considered False, so the opposite is True. 'not 'Peter' ' evaluates to False because a non-empty string is considered True, so the opposite is False. 'not None' evaluates to True because None is considered False, so the opposite is True.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
