寄托天下
楼主: vegetable03
打印 上一主题 下一主题

[未归类] 放开我╭(╯^╰)╮我要水! [复制链接]

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

466
发表于 2017-5-4 14:54:25 |只看该作者
  1. \section{Variable Names and Keywords}

  2. Programmers generally choose names for their variables that are meaningful---they
  3. document what the variable is used for.

  4. Variable names can be arbitrarily long. They can contain both letters and numbers, but
  5. they have to begin with a  letter. It is legal to use uppercase letters, but it is a good idea
  6. to begin variable names with a lowercase letter (you'll see why later).

  7. The underscore character, \texttt{\_}, can appear in a name. It is often used in names with multiple
  8. words, such as \texttt{my\_name} or \texttt{airspeed\_of\_unladen\_swallow}.

  9. If you give a variable an illegal name, you get a syntax error:

  10. \begin{pquote}
  11. \begin{verbatim}
  12. >>> 76strombones = 'big parade'
  13. SyntaxError: invalid syntax
  14. >>> more@ = 1000000
  15. SyntaxError: invalid syntax
  16. >>> class = 'Advanced Theoretical Zymurgy'
  17. SyntaxError: invalid syntax
  18. \end{verbatim}
  19. \end{pquote}

  20. \texttt{76trombones} is illegal because it does not begin with a letter. \texttt{more@} is illegal because it
  21. contains an illegal character, \texttt{@}. But what's wrong with \texttt{class}?

  22. It turns out that \texttt{class} is one of Python's \textbf{keywords}. The interpreter uses keywords to
  23. recognize the structure of the program, and they cannot be used as variable names.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

467
发表于 2017-5-4 14:54:36 |只看该作者
  1. Python 2 has 31 keywords:

  2. \begin{pquote}
  3. \begin{multicols}{5}
  4. \begin{verbatim}
  5. and
  6. del
  7. from
  8. not
  9. while
  10. as
  11. elif
  12. global
  13. or
  14. with
  15. assert
  16. else
  17. if
  18. pass
  19. yield
  20. break
  21. except
  22. import
  23. print
  24. class
  25. exec
  26. in
  27. raise
  28. continue
  29. finally
  30. is
  31. return
  32. def
  33. for
  34. lambda
  35. try
  36. \end{verbatim}
  37. \end{multicols}
  38. \end{pquote}

  39. In Python 3, \texttt{exec} is no longer a keyword, but \texttt{nonlocal} is.

  40. You might want to keep this list handy. If the interpreter complains abut one of your
  41. variable names and you don't know why, see if it is on this list.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

468
发表于 2017-5-4 14:55:09 |只看该作者
  1. \section{Operator and Operands}

  2. \textbf{Operators} are special symbols that represent computations like addition and multiplication.
  3. The values the operator is applied to are called \textbf{operands}.

  4. The operators \texttt{+}, \texttt{-}, \texttt{*}, \texttt{$\backslash$} and \texttt{**} perform addition,
  5. subtraction, multiplication, division and exponentiation, as in the following examples:

  6. \begin{pquote}
  7. \begin{verbatim}
  8. 20+32   hour-1   hour*60+minute   minute/60   5**2   (5+9)*(15-7)
  9. \end{verbatim}
  10. \end{pquote}

  11. In some other language, \texttt{\^{}} is used for exponentiation, but in Python it is a bitwise
  12. operator called XOR. I won't cover bitwise operators in this book, but you can read about
  13. them at \textit{http://wiki.python.org/moin/BitwiseOperators}.

  14. In Python 2, the division operator might not do what you expect:

  15. \begin{pquote}
  16. \begin{verbatim}
  17. >>> minute = 59
  18. >>> minute/60
  19. 0
  20. \end{verbatim}
  21. \end{pquote}

  22. The value of \texttt{minute} is 59, and in conventional arithmetic 59 divided by 60 is 0.98333,
  23. not 0. The reason for the discrepancy is that Python is performing \textbf{floor division}. When
  24. both of the operands are integers, result is also an integer; floor division chops off
  25. the fraction part, so in this example it rounds down to zero.

  26. In Python 3, the result of this division is a \texttt{float}. The new operator \texttt{//} performs floor
  27. division.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

469
发表于 2017-5-4 14:55:24 |只看该作者
  1. In either of the operands is a floating-point number, Python performs floating-point
  2. division, and the result is a \texttt{float}:

  3. \begin{pquote}
  4. \begin{verbatim}
  5. >>> minute/60.0
  6. 0.9833333333333333
  7. \end{verbatim}
  8. \end{pquote}
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

470
发表于 2017-5-4 14:55:35 |只看该作者
  1. \section{Expressions and Statement}

  2. An \textbf{expression} is a combination of values, variables, and operators. A value all by itself
  3. is considered an expression, and so is a variable, so the following are all legal expressions
  4. (assuming that the variable \texttt{x} has been assigned a value):

  5. \begin{pquote}
  6. \begin{verbatim}
  7. 17
  8. x
  9. x + 17
  10. \end{verbatim}
  11. \end{pquote}

  12. A \textbf{statement} is a unit of code that the Python interpreter can execute. We have seen two
  13. kinds of statement: print and assignment.

  14. Technically an expression is also a statement, but it is probably simpler to think of them
  15. as different things. The important difference is that an expression has a value; a statement
  16. does not.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

471
发表于 2017-5-4 14:55:50 |只看该作者
  1. \section{Interactive Mode and Script Mode}

  2. One of the benefits of working with an interpreted language is that you can test bits of
  3. code in interactive mode before you put them in a script. But there are differences
  4. between interactive mode and script mode that can be confusing.

  5. For example, if you are using Python as a calculator, you might type

  6. \begin{pquote}
  7. \begin{verbatim}
  8. >>> miles = 26.2
  9. >>> miles * 1.61
  10. 42.182
  11. \end{verbatim}
  12. \end{pquote}

  13. The first line assigns a value to \texttt{miles}, but it has no visible effect. The second line is an
  14. expression, so the interpreter evaluates it and displays the result. So we learn that a
  15. marathon is about 42 kilometers.

  16. But if you type the same code into a script and run it, you get no output at all. In script
  17. mode an expression, all by itself, has no visible effect. Python actually evaluates the
  18. expression, but it doesn't display the value unless you tell it to:

  19. \begin{pquote}
  20. \begin{verbatim}
  21. miles = 26.2
  22. print(miles * 1.61)
  23. \end{verbatim}
  24. \end{pquote}

  25. This behavior can be confusing at first.

  26. A script usually contains a sequence of statements. If there is more than one statement,
  27. the results appear one at a time as the statement execute.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

472
发表于 2017-5-4 14:56:02 |只看该作者
  1. For example, the script

  2. \begin{pquote}
  3. \begin{verbatim}
  4. print 1
  5. x = 2
  6. print x
  7. \end{verbatim}
  8. \end{pquote}
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

473
发表于 2017-5-4 14:56:15 |只看该作者
  1. \textit{Exercise 2-2}.

  2. Type the following statements in the Python interpreter to see what they do:

  3. \begin{pquote}
  4. \begin{verbatim}
  5. 5
  6. x = 5
  7. x + 1
  8. \end{verbatim}
  9. \end{pquote}

  10. Now put the same statements into a script and run it. What is the output? Modify the
  11. script by transforming each expression into a print statement and the run it again.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

474
发表于 2017-5-4 14:56:26 |只看该作者
  1. \section{Order of Operations}

  2. When more than one operator appears in an expression, the order of evaluation depends
  3. on the \textbf{rule of precedence}. For mathematical operators, Python follows mathematical
  4. convention. The acronym \textbf{PEMDAS} is a useful way to remember the rules:
  5. \begin{itemize}
  6.   \item \textbf{P}arentheses have the highest precedence and can be used to force an expression to
  7.   evaluate in the order you want. Since expressions in parentheses are evaluated first,
  8.   \texttt{2 * (3-1)} is 4, and \texttt{(1+1)**(5-2)} is 8. You can also use parentheses to make an
  9.   expression easier to read, as in \texttt{(minute * 100) / 60}, even if it doesn't change the
  10.   result.
  11.   \item \textbf{E}xponentiation has the next highest precedence, so \texttt{2**1+1} is 3, not 4, and
  12.   \texttt{3*1**3} is 3, not 27.
  13.   \item \textbf{M}ultiplication and \textbf{D}ivision have the same precedence, which is higher than
  14.   \textbf{A}ddition and \textbf{S}ubtraction, which also have the same precedence. So \texttt{2*3-1}
  15.   is 5, not 4, and \texttt{6+4/2} is 8, not 5.
  16.   \item Operators with the same precedence are evaluated for left to right (except exponentiation).
  17.   So in the expression \texttt{degree / 2 * pi}, the division happens first and
  18.   the result is multiplied by \texttt{pi}. To divide by $2\pi$, you can use parentheses or write
  19.   \texttt{degree / 2 / pi}.
  20. \end{itemize}

  21. I don't work very hard to remember rules of precedence for other operators. If I can't
  22. tell by looking at the expression, I use parentheses to make it obvious.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

475
发表于 2017-5-4 14:56:42 |只看该作者
  1. \section{String Operations}

  2. In general, you can't perform mathematical operations on strings, even if the strings
  3. look like numbers, so the following are illegal:

  4. \begin{pquote}
  5. \begin{verbatim}
  6. '2' - '1'   'egg' / 'easy'   'third' * 'a charm'
  7. \end{verbatim}
  8. \end{pquote}

  9. The \texttt{+} operator works with strings, but it might not do what you expect: it perform
  10. \textbf{concatenation}, which means joining the strings by linking them end-to-end. For
  11. example:

  12. \begin{pquote}
  13. \begin{verbatim}
  14. first = 'throat'
  15. second = 'warbler'
  16. print(first + second)
  17. \end{verbatim}
  18. \end{pquote}
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

476
发表于 2017-5-4 14:56:55 |只看该作者
  1. The output of this program is \texttt{throatwarbler}.

  2. The \texttt{*} operator also works on strings; it performs repetition. For example, \texttt{'Spam' * 3}
  3. is \texttt{'SpamSpamSpam'}. If one of the operands is a string, the other has to be an integer.

  4. This use of \texttt{+} and \texttt{*} makes sense by analogy with addition and multiplication. Just as
  5. \texttt{4 * 3} is equivalent to \texttt{4 + 4 + 4}, we expect \texttt{'Spam' * 3} to be the same as
  6. \texttt{'Spam' + 'Spam' + 'Spam'}, and it is. On the other hand, there is a significant way in which string concatenation
  7. and repetition are different from integer addition and multiplication. Can you think of
  8. a property that addition has that string concatenation does not?\footnote{Of course. $a + b = b + a$ is true when
  9. $a$ and $b$ are both integers, but this doesn't apply to a concatenation where $a$ and $b$ are both strings.}
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

477
发表于 2017-5-4 14:57:09 |只看该作者
  1. \section{Comments}

  2. As programs get bigger and more complicated, they get more difficult to read. Formal
  3. languages are dense, and it is often difficult to look at a piece of code and figure out what
  4. it is doing, or why.

  5. For this reason, it is a good idea to add notes to your programs to explain in natural
  6. language what the program is doing. These notes are called \textbf{comments}, and they start
  7. with the \texttt{\#} symbol:

  8. \begin{pquote}
  9. \begin{verbatim}
  10. # compute the percentage of the hour that has elapsed
  11. percentage = (minute * 100) / 60
  12. \end{verbatim}
  13. \end{pquote}

  14. Everything from the \texttt{\#} to the end of the line is ignored---it has no effect on the program.

  15. Comments are most useful when they document non-obvious feature of the code. It is
  16. reasonable to assume that the reader can figure out \textit{what} the code does; it is much more
  17. useful to explain \textit{why}.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

478
发表于 2017-5-4 14:57:23 |只看该作者
  1. This comment is redundant with the code and useless:

  2. \begin{pquote}
  3. \begin{verbatim}
  4. v = 5   # assign 5 to v
  5. \end{verbatim}
  6. \end{pquote}

  7. This comment contains useful information that is not in the code:

  8. \begin{pquote}
  9. \begin{verbatim}
  10. v = 5   # velocity in meters/second.
  11. \end{verbatim}
  12. \end{pquote}

  13. Good variable names can reduce the need for comments, but long names can make
  14. complex expressions hard to read, so there is a tradeoff.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

479
发表于 2017-5-4 14:57:36 |只看该作者
  1. \section{Debugging}

  2. At this point the syntax error you are most likely to make is an illegal variable name,
  3. like \texttt{class} and \texttt{yield}, which are keywords, or \texttt{odd~job} and \texttt{US\$}, which contain illegal
  4. characters.

  5. If you put a space in a variable name, Python thinks it is two operands without
  6. an operator:

  7. \begin{pquote}
  8. \begin{verbatim}
  9. >>> bad name = 5
  10. SyntaxError: invalid syntax
  11. \end{verbatim}
  12. \end{pquote}

  13. For syntax errors, the errors messages don't help much. The most common messages are
  14. \texttt{SyntaxError: invalid syntax} and \texttt{SyntaxError: invalid token}, neither of which
  15. is very informative.

  16. The runtime error you are most likely to make is a ``use before def;'' that is, trying to use
  17. a variable before you have assigned a value. This can happen if you spell a variable name
  18. wrong:

  19. \begin{pquote}
  20. \begin{verbatim}
  21. >>> principal = 327.68
  22. >>> interest = principle * rate
  23. NameError: name 'principle' is not defined
  24. \end{verbatim}
  25. \end{pquote}

  26. Variables names are case sensitive, so \texttt{LaTeX} is not the same as \texttt{latex}.

  27. At this point the most likely cause of a semantic error is the order of operations. For
  28. example, to evaluate $\frac{1}{2\pi}$, you might be tempted to write

  29. \begin{pquote}
  30. \begin{verbatim}
  31. >>> 1.0 / 2.0 * pi
  32. \end{verbatim}
  33. \end{pquote}

  34. But the division happens first, so you would get $\pi/2$ which is not the same thing! There
  35. is no way for Python to know what you meant to write, so in this case you don't get an
  36. error message; you just get the wrong answer.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

声望
282
寄托币
8473
注册时间
2016-10-23
精华
1
帖子
1327

新任版主 寄托兑换店纪念章 2016 US-applicant 寄托16周年纪念勋章

480
发表于 2017-5-4 14:57:51 |只看该作者
  1. \section{Glossary}

  2. \begin{description}
  3.   \item[Value:] One of the basic units of data, like a number or string, that a program manipulates.
  4.   \item[Type:] A category of values. The types we have seen so far are integers (type \texttt{int}),
  5.   floating-point numbers (type \texttt{float}), and strings (type \texttt{str}).
  6.   \item[Integer:] A type that represents whole numbers.
  7.   \item[Floating-point:] A type that represents numbers with fractional parts.
  8.   \item[String:] A type that represents sequences of characters.
  9.   \item[Variable:] A name that refers to a value.
  10.   \item[Statement:] A section of code that represents a command or action. So far, the statement we
  11.   have seen are assignments and print statements.
  12.   \item[Assignment:] A statement that assigns a value to a variable.
  13.   \item[State diagram:] A graphical representation of a set of variables and the values they refer to.
  14.   \item[Keyword:] A reserved word that is used by the compiler to parse a program; you cannot use
  15.   keywords like \texttt{if}, \texttt{def}, and \texttt{while} as variable names.
  16.   \item[Operator:] A special symbol that represents a simple computation like addition, multiplication,
  17.   or string concatenation.
  18.   \item[Operand:] One of the values on which an operator operates.
  19.   \item[Floor division:] The operation that divides two numbers hand chops off the fraction part.
  20.   \item[Expression:] A combination of variables, operators, and values that represents a single result
  21.   value.
  22.   \item[Evaluate:] To simplify an expression by performing the operations in order to yield a single
  23.   value.
  24.   \item[Rules of precedence:] The set of rules governing the order in which expressions involving multiple
  25.   operators and operands are evaluated.
  26.   \item[Concatenate:] To join two operands end-to-end.
  27.   \item[Comment:] Information in a program that is meant for other programmers (or anyone reading
  28.   the source code) and has no effect on the execution of the program.
  29. \end{description}
复制代码

使用道具 举报

RE: 放开我╭(╯^╰)╮我要水! [修改]

问答
Offer
投票
面经
最新
精华
转发
转发该帖子
放开我╭(╯^╰)╮我要水!
https://bbs.gter.net/thread-2047068-1-1.html
复制链接
发送
回顶部