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, orConceptOperator precedence decides which operation is grouped first when an expression has no explicit grouping. Grouping parentheses override ordinary…

  1. A.

    Braces, or, and, not

  2. B.

    not, Braces, and, or

  3. C.

    not, Braces, or, and

  4. 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:

  1. not False evaluates to True, so the expression becomes True and False or True.

  2. True and False evaluates to False, so the expression becomes False or True.

  3. False or True evaluates to True.

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.

Explore the full course: Rssb Senior Computer Instructor

Loading lesson…