Operators and precedence

Arithmetic operators (floor division, modulo, power), comparison and logical operators, the order in which an expression is evaluated (precedence, associativity) and the short-circuit evaluation of logical operators.

  • 27 cards
  • 9 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. Scenarios
  6. Understand
  7. Line by line
  8. Step by step
  9. Cell by cell

A few cards from the deck

  • Operator precedence: what is it?

    Show the answer

    The rule that says which operator is applied first when an expression mixes several without parentheses.

    2 + 3 * 4 is 14: * goes before +. Parentheses always win: (2 + 3) * 4 is 20.

  • What does this statement print?

    ```python
    print(2 3 2)
    `

    • 512
    • 64
    • 36
    • 12
    Show the answer

    512

    ** is right-associative: 3 ** 2 is computed first (9), then 2 ** 9 gives 512. Going left to right, (2 ** 3) ** 2, would give 64.

  • In a + 2, a and 2 are the … of the + operator. A … operator takes a single operand, like not or the minus of -x; a binary operator takes two.

    Show the answer

    In a + 2, a and 2 are the operands of the + operator. A unary operator takes a single operand, like not or the minus of -x; a binary operator takes two.

    The same sign can play both parts: - is binary in a - b and unary in -b.