Operator precedence in python ppt

Just for you: FREE 60-day trial to the world’s largest digital library.

The SlideShare family just got bigger. Enjoy access to millions of ebooks, audiobooks, magazines, and more from Scribd.

Read free for 60 days

Cancel anytime.

Presentation on theme: "Precedence and Associativity"— Presentation transcript:

1 Precedence and Associativity
The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators with a higher precedence are executed before operators with a lower precedence. In the following table, operators are in descending order of precedence, so those with the highest precedence are at the top. Operators within the same group in the table are of equal precedence. Precedence and Associativity tMyn

2 The sequence of execution of operators with the same precedence in an expression is determined from their associativity. The associativity of an operator determines how it groups with its operands in an expression. Operators may be left-associative, right-associative or non-associative. Precedence and Associativity tMyn

3 Almost all other operators are left associative.
All unary operators and all assignment operators are right associative. Almost all other operators are left associative. With the throw operator [exception throwing] the associativity is not available. Precedence and Associativity tMyn

4 Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence. Consider the expression The result could be either [7-4]+2 = 5 or 7-[4+2] = 1. The former result corresponds to the case when + and - are left-associative. The latter result corresponds to the case when + and - are right-associative. Operators with the same precedence always have the same associativity. The operators +, -, * and / are left-associative. Precedence and Associativity tMyn

5 A left-associative operator can be said to associate "to the left", and similarly a right-associative operator can be said to associate "to the right". To understand the intuition behind these names, consider again the expression If + and - are left-associative then the middle operand [4] belongs to the operator on its left [hence the name "to the left"]. If + and - are right-associative then the middle operand belongs to the operator on its right [hence the name "to the right"]. Precedence and Associativity tMyn

6 A left-associative operator may also be said to have "left to right" associativity, and a right-associative operator may also be said to have "right to left" associativity. This is somewhat counter-intuitive considering the above paragraph. To understand the intuition behind these names consider the expression If + is left-associative, the addition operations are carried out left to right, i.e. the result is [[[1+2]+3]+4]+5. If + is right-associative, the addition operations are carried out right to left, i.e. the result is 1+[2+[3+[4+5]]]. Precedence and Associativity tMyn

7 Direct member selection
Operator Name or Meaning Associativity :: Scope resolution Left to right . Direct member selection -> Indirect member selection [] Subscript [] Function call ++ Postfix increment -- Postfix decrement static_cast Explicit static cast dynamic_cast Dynamic cast const_cast Cast away const reinterpret_cast Unchecked cast typeid Type identification Precedence and Associativity tMyn

8 Explicit cast [old style]
Operator Name or Meaning Associativity + Unary plus Right to left - Unary minus ++ Prefix increment -- Prefix decrement ! Logical negation - not ~ Bitwise complement & Address-of * Dereference [type] Explicit cast [old style] sizeof Size of object or type new Allocate memory delete Deallocate memory Precedence and Associativity tMyn

9 Direct pointer-to- member selection Left to right
Operator Name or Meaning Associativity .* Direct pointer-to- member selection Left to right ->* Indirect pointer-to- member selection * Multiply / Divide % Modulus + Binary addition - Binary subtraction Shift right Precedence and Associativity tMyn

10 Greater than or equal to
Operator Name or Meaning Associativity = Greater than or equal to == Equal to != Not equal to & Bitwise AND ^ Bitwise exclusive OR | Bitwise OR Precedence and Associativity tMyn

11 *= /= %= += -= &= ^= |= = Compound assignment
Operator Name or Meaning Associativity && Logical AND Left to right || Logical OR ?: Conditional operator Right to left = Assignment *= /= %= += -= &= ^= |= = Compound assignment Throw Throw exception , Comma Precedence and Associativity tMyn

What is the precedence of operators in Python?

The operator precedence in Python is listed in the following table. ... Precedence of Python Operators..

What are the operators in Python?

Python Operators.
Arithmetic operators..
Comparison operators..
Assignment Operators..
Logical Operators..
Bitwise Operators..
Membership Operators..
Identity Operators..

What is the order of precedence for operators?

The logical-AND operator [ && ] has higher precedence than the logical-OR operator [ || ], so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .

What are the 5 arithmetic operators?

Definition. The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.

Chủ Đề