This page intentionally left blank Python for Software Design


ORDER OF OPERATIONS When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence



tải về 1.38 Mb.
Chế độ xem pdf
trang12/83
Chuyển đổi dữ liệu13.08.2023
Kích1.38 Mb.
#55046
1   ...   8   9   10   11   12   13   14   15   ...   83
- Python for Software Design How to Think Like a Computer Scientist-Cambridge University Press (2009)

    Điều hướng trang này:
  • PEMDAS
2.7
ORDER OF OPERATIONS
When more than one operator appears in an expression, the order of evaluation
depends on the rules of precedence. For mathematical operators, Python follows


16
Variables, Expressions, and Statements
mathematical convention. The acronym PEMDAS is a useful way to remember the
rules:

Parentheses have the highest precedence and can be used to force an expression
to evaluate in the order you want. Since expressions in parentheses are evaluated
first, 2 * (3 - 1) is 4, and (1 + 1) ** (5 - 2) is 8. You can also use parentheses to
make an expression easier to read, as in (minute * 100) / 60, even if it doesn’t
change the result.

Exponentiation has the next highest precedence, so 2 ** 1 + 1 is 3, not 4, and
3 * 1 ** 3
is 3, not 27.

Multiplication and Division have the same precedence, which is higher than
Addition and Subtraction, which also have the same precedence. So 2 * 3 - 1
is 5, not 4, and 6 + 4 / 2 is 8, not 5.

Operators with the same precedence are evaluated from left to right. So in the
expression degrees / 2 * pi, the division happens first and the result is multiplied
by pi. To divide by 2
π, you can reorder the operands or use parentheses.
2.8
STRING OPERATIONS
In general, you cannot perform mathematical operations on strings, even if the strings
look like numbers, so the following are illegal:
'2'-'1'
'eggs'/'easy'
'third'*'a charm'
The + operator works with strings, but it might not do what you expect: it performs
concatenation, which means joining the strings by linking them end-to-end. For
example:
first = 'throat'
second = 'warbler'
print first + second
The output of this program is throatwarbler.
The * operator also works on strings; it performs repetition. For example, 'Spam'*3
is 'SpamSpamSpam'. If one of the operands is a string, the other has to be an integer.
This use of + and * makes sense by analogy with addition and multiplication.
Just as 4 * 3 is equivalent to 4 + 4 + 4, we expect 'Spam'*3 to be the same as
'Spam'+'Spam'+'Spam'
, and it is. On the other hand, there is a significant way
in which string concatenation and repetition are different from integer addition and
multiplication. Can you think of a property that addition has that string concatenation
does not?


2.10 Debugging
17
2.9
COMMENTS
As programs get bigger and more complicated, they get more difficult to read. Formal
languages are dense, and it is often difficult to look at a piece of code and figure out
what it is doing, or why.
For this reason, it is a good idea to add notes to your programs to explain in natural
language what the program is doing. These notes are called comments, and they start
with the # symbol:
# compute the percentage of the hour that has elapsed
percentage = (minute * 100) / 60
In this case, the comment appears on a line by itself. You can also put comments at
the end of a line:
percentage = (minute * 100) / 60
# percentage of an hour
Everything from the # to the end of the line is ignored – it has no effect on the
program.
Comments are most useful when they document non-obvious features of the code. It
is reasonable to assume that the reader can figure out what the code does; it is much
more useful to explain why.
This comment is redundant with the code and useless:
v = 5
# assign 5 to v
This comment contains useful information that is not in the code:
v = 5
# velocity in meters/second.
Good variable names can reduce the need for comments, but long names can make
complex expressions hard to read, so there is a tradeoff.
2.10
DEBUGGING
At this point the syntax error you are most likely to make is an illegal variable name,
like class and yield, which are keywords, or odd˜job and US$, which contain illegal
characters.


18
Variables, Expressions, and Statements
If you put a space in a variable name, Python thinks it is two operands without an
operator:
>>> bad name = 5
SyntaxError: invalid syntax
For syntax errors, the error messages don’t help much. The most common messages
are SyntaxError: invalid syntax and SyntaxError: invalid token, neither of
which is very informative.
The runtime error you are most likely to make is a “use before def;” that is, trying
to use a variable before you have assigned a value. This can happen if you spell a
variable name wrong:
>>> principal = 327.68
>>> interest = principle * rate
NameError: name 'principle' is not defined
Variables names are case sensitive, so LaTeX is not the same as latex.
At this point the most likely cause of a semantic error is the order of operations. For
example, to evaluate
1
2
π
, you might be tempted to write
>>> 1.0 / 2.0 * pi
But the division happens first, so you would get
π / 2, which is not the same thing!
There is no way for Python to know what you meant to write, so in this case you
don’t get an error message; you just get the wrong answer.

tải về 1.38 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   8   9   10   11   12   13   14   15   ...   83




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương