PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 54
Q479 - Operators
Which of the following expressions evaluates to False and raises no exception?
(Select two answers.)
-
A
- not ('a' not in 'abc')
-
B
- '\n' in """
- """
-
C
- not 'a' in 'abc'
-
D
- '123' in '1-2-3'
Reveal correct answers
Correct answers: C, D
Explanation
Topics: in operator not operator escape sequence
Try it yourself/Explanation:
- print('123' in '1-2-3') # False
- # "123" is not a part of "1-2-3"
- print(not 'a' in 'abc') # False
- # "a" is a part of "abc"
- print('\n' in """
- """) # True
- # A newline character is a part of every multi-line string.
- print(not ('a' not in 'abc')) # True
- print(not False) # True
- # "a" is a part of "abc"
- # Two times not and it is True again.
Q479 (Please refer to this number, if you want to write me about this question.)
A.
The expression ('a' not in 'abc') evaluates to False because the character 'a' is present in the string 'abc'. The outer 'not' operator negates the result of the inner expression, making it True.
B.
The expression
'\n' in """
"""
evaluates to True because the newline character '\n' is found in the triple-quoted string.
C. The expression not 'a' in 'abc' evaluates to False because the character 'a' is found in the string 'abc'. The 'not' operator negates the result, making it False, and no exception is raised during this evaluation.
D. The expression '123' in '1-2-3' evaluates to False because the string '123' is not found as a substring in the string '1-2-3'. This comparison results in False without raising an exception.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
