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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

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

Portability Tip 2.1<br />

In <strong>Python</strong> version 3.0 (due to be released no sooner than 2003), the / operator can perform<br />

only true division. After the release of version 3.0, programmers need to update applications<br />

to compensate for the new behavior. For more information on this future change, see<br />

python.sourceforge.net/peps/pep-0238.html 2.1<br />

<strong>Python</strong> provides the modulus operator (%), which yields the remainder after integer<br />

division. The expression x % y yields the remainder after x is divided by y. Thus, 7 %4<br />

yields 3 and 17 % 5 yields 2. This operator is most commonly used with integer operands,<br />

but also can be used with other arithmetic types. In later chapters, we discuss many interesting<br />

applications of the modulus operator, such as determining whether one number is a<br />

multiple of another. (A special case of this is determining whether a number is odd or even.)<br />

[Note: The modulus operator can be used with both integer and floating-point numbers.]<br />

Arithmetic expressions in <strong>Python</strong> must be entered into the computer in straight-line<br />

form. Thus, expressions such as “a divided by b” must be written as a / b, so that all constants,<br />

variables and operators appear in a straight line. The algebraic notation<br />

--<br />

a<br />

b<br />

is generally not acceptable to compilers or interpreters, although some special-purpose<br />

software packages do exist that support more natural notation for complex mathematical<br />

expressions.<br />

Parentheses are used in <strong>Python</strong> expressions in much the same manner as in algebraic<br />

expressions. For example, to multiply a times the quantity b + c, we write<br />

a * (b + c)<br />

<strong>Python</strong> applies the operators in arithmetic expressions in a precise sequence determined<br />

by the following rules of operator precedence, which are generally the same as those<br />

followed in algebra:<br />

1. Expressions contained within pairs of parentheses are evaluated first. Thus, parentheses<br />

may force the order of evaluation to occur in any sequence desired by the<br />

programmer. Parentheses are said to be at the “highest level of precedence.” In<br />

cases of nested, or embedded, parentheses, the operators in the innermost pair of<br />

parentheses are applied first.<br />

2. Exponentiation operations are applied next. If an expression contains several exponentiation<br />

operations, operators are applied from right to left.<br />

3. Multiplication, division and modulus operations are applied next. If an expression<br />

contains several multiplication, division and modulus operations, operators are<br />

applied from left to right. Multiplication, division and modulus are said to be on<br />

the same level of precedence.<br />

4. Addition and subtraction operations are applied last. If an expression contains several<br />

addition and subtraction operations, operators are applied from left to right.<br />

Addition and subtraction also have the same level of precedence.

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

Saved successfully!

Ooh no, something went wrong!