27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

pythonhtp1_02.fm Page 49 Wednesday, December 12, 2001 12:12 PM<br />

Chapter 2 Introduction to <strong>Python</strong> <strong>Program</strong>ming 49<br />

Operator(s) Operation(s) Order of Evaluation (Precedence)<br />

( ) Parentheses Evaluated first. If the parentheses are nested, the<br />

expression in the innermost pair is evaluated first. If<br />

there are several pairs of parentheses “on the same<br />

level” (i.e., not nested), they are evaluated left to right.<br />

** Exponentiation Evaluated second. If there are several, they are evaluated<br />

right to left.<br />

* / // % Multiplication<br />

Division<br />

Modulus<br />

+ - Addition<br />

Subtraction<br />

Fig. Fig. 2.16 2.16 Precedence of arithmetic operators.<br />

The circled numbers under the statement indicate the order in which <strong>Python</strong> applies the<br />

operators. The multiplication, modulus and division are evaluated first, in left-to-right order<br />

(i.e., they associate from left to right) because they have higher precedence than addition<br />

and subtraction. The addition and subtraction are applied next. These are also<br />

applied left to right. Once the expression has been evaluated, <strong>Python</strong> assigns the result to<br />

variable z.<br />

<strong>To</strong> develop a better understanding of the rules of operator precedence, consider how a<br />

second-degree polynomial is evaluated:<br />

y = a * x ** 2 + b * x + c<br />

2 1 4 3 5<br />

The circled numbers under the statement indicate the order in which <strong>Python</strong> applies the operators.<br />

Suppose variables a, b, c and x are initialized as follows: a=2, b =3, c =7 and<br />

x=5. Figure 2.17 illustrates the order in which the operators are applied in the preceding<br />

second-degree polynomial.<br />

The preceding assignment statement can be parenthesized with unnecessary parentheses,<br />

for clarity, as<br />

y = ( a * ( x ** 2 ) ) + ( b * x ) + c<br />

Evaluated third. If there are several, they are evaluated<br />

left to right. [Note: The // operator is new in version<br />

2.2]<br />

Evaluated last. If there are several, they are evaluated<br />

left to right.<br />

Good <strong>Program</strong>ming Practice 2.7<br />

As in algebra, it is acceptable to place unnecessary parentheses in an expression to make the<br />

expression clearer. These parentheses are called redundant parentheses. Redundant parentheses<br />

are commonly used to group subexpressions in a large expression to make that expression<br />

clearer. Breaking a large statement into a sequence of shorter, simpler statements also<br />

promotes clarity. 2.7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!