Which of the following is the correct precedence of operators in Python?
2023
Which of the following is the correct precedence of operators in Python?
Answer: D. Braces, not, and, or — ConceptOperator precedence decides which operation is grouped first when an expression has no explicit grouping. Grouping parentheses override ordinary…
- A.
Braces, or, and, not
- B.
not, Braces, and, or
- C.
not, Braces, or, and
- D.
Braces, not, and, or
Attempted by 2168 students.
Show answer & explanation
Correct answer: D
Concept
Operator precedence decides which operation is grouped first when an expression has no explicit grouping.
Grouping parentheses override ordinary precedence. Among Python Boolean operators, not binds more tightly than and, and and binds more tightly than or.
Application
The source options use “Braces” to mean grouping parentheses. Therefore the relevant high-to-low order is parentheses, not, and, then or. Trace not False and False or True:
not Falseevaluates toTrue, so the expression becomesTrue and False or True.True and Falseevaluates toFalse, so the expression becomesFalse or True.False or Trueevaluates toTrue.
Cross-check
The same precedence inserts grouping as ((not False) and False) or True, which also evaluates to True.
Result
Hence the precedence from highest to lowest is: Braces, not, and, or.