寄托天下
楼主: vegetable03

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

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:50:46 |显示全部楼层
  1. \section{Syntax Errors}

  2. Python can only execute a program if the syntax is correct; otherwise, the interpreter
  3. displays an error message. \textbf{Syntax} refers to the structure of a program and the rules
  4. about that structure. For example, parentheses have to come in matching pairs, so
  5. \texttt{(1 + 2)} is legal, but \texttt{8)} is a \textbf{syntax error}.

  6. In English readers can tolerate most syntax errors, which is why we can read the poetry
  7. of e.e. cummings without spewing error messages. Python is not so forgiving. If there
  8. is a single syntax error anywhere in your program, Python will display an error message
  9. and quit, and you will not be able to run your program. During the first few weeks of
  10. your programming career, you will probably spend a lot of time tracking down syntax
  11. errors. As you gain experience, you will make fewer errors and find them faster.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:51:03 |显示全部楼层
  1. \section{Runtime Errors}

  2. The second type of error is a runtime error, so called because the error dos not appear
  3. until after the program has started running. These errors are also called \textbf{exceptions}
  4. because they usually indicate that something exceptional (and bad) has happened.

  5. Runtime errors are rare in simple programs you will see in the first few chapters, so
  6. it might be a while before you encounter one.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:51:15 |显示全部楼层
  1. \section{Semantic Errors}

  2. The third type of error is the \textbf{semantic error}. If there is a semantic error in your program.
  3. it will run successfully in the sense that the computer will not generate any error messages,
  4. but it will not do the right thing. It will do something else. Specially, it will do
  5. what you told it to do.

  6. The problem is that the program you wrote is not the program you wanted to write. The
  7. meaning of the program (its semantics) is wrong. Identifying semantic errors can be
  8. tricky because it requires you to work backward by looking at the output of the program
  9. and trying to figure out what it is doing.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:51:28 |显示全部楼层
  1. \section{Experimental Debugging}

  2. One of the most important skills you will acquire is debugging. Although it can be
  3. frustrating, debugging is one of the most intellectually rich, challenging, and interesting
  4. part of programming.

  5. In some way, debugging is like detective work. You are confronted with clues, and you
  6. have to infer the process and events that led to the results you see.

  7. Debugging is also like an experimental science. Once you have an idea about what is
  8. going wrong, you modify your program and try again. If your hypothesis was correct,
  9. then you can predict the result of the modification, and you take a step closer to a
  10. working program. If your hypothesis was wrong, you have to come up with a new one.
  11. As Sherlock Holmes pointed out, ``When you have eliminated the impossible, whatever
  12. remains, however improbable, must be the truth.'' (A. Conan Doyle, \textit{The Sign of Four})

  13. For some people, programming and debugging are the same thing. That is, programming
  14. is the process of gradually debugging a program until it does what you want. The
  15. idea is that you should start with a program that does \textit{something} and make small
  16. modifications, debugging them as you go, so that you always have a working program.

  17. For example, Linux is an operating system that contains thousands of lines of code, but
  18. it started out as a simple program Linus Torvalds used to explore the Intel 8-386 chip.
  19. According to Larry Greenfield, ``One of Linus's earlier projects was a program that would
  20. switch between printing AAAA and BBBB. This later evolved to Linux.'' (\textit{The Linux
  21. User's Guide} Beta Version 1).

  22. Later chapters will make more suggestions about debugging and other programming
  23. practice.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:51:42 |显示全部楼层
  1. \section{Formal and Natural Languages}

  2. \textbf{Natural languages} are the languages people speak, such as English, Spanish, and French.
  3. They were not designed by people (although people try to impose some order on them);
  4. they evolved naturally.

  5. \textbf{Formal languages} are languages that are designed by people for specific applications.
  6. For example, the notation that mathematicians use is a formal language that is particularly
  7. good at denoting relationship among numbers and symbols. Chemists use a
  8. formal language to represent the chemical structure of molecules. And most
  9. importantly:

  10. \begin{quote}\bfseries \small
  11. Programming languages are formal languages that have been designed to express
  12. computations.
  13. \end{quote}

  14. Formal languages tend to have strict rules about syntax. For example, $3+3=6$ is a
  15. syntactically correct mathematical statement, but $3+ =3\$6$ is not. \chemfig{H_{2}O} is a syntactically
  16. correct chemical formula, but \chemfig{_{2}Z_{z}} is not.

  17. Syntax rules come in two flavors, pertaining to \textbf{tokens} and structure. Tokens are the
  18. basic elements of the language, such as words, numbers and chemical elements. One of
  19. the problems with $3+ = 3\$6$ is that \$ is not  legal token in mathematics (at least as far
  20. as I know). Similarly, \chemfig{_{2}Z_{z}} is not legal because there is no element with the abbreviation
  21. \chemfig{Z_{z}}

  22. The second type of syntax error pertains to the structure of a statement; that is, the way
  23. the tokens are arranged. The statement $3+ =3$ is illegal because even though $+$ and $=$
  24. are legal tokens, you can't have one right after the other. Similarly, in a chemical formula
  25. the subscript comes after the element name, not before.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:52:06 |显示全部楼层
  1. \textit{Exercise 1-1}

  2. Write a well-structured English sentence with invalid tokens in it. Then write another
  3. sentence with all valid tokens but with invalid structure.

  4. When you read a sentence in English or a statement in a formal language, you have to
  5. figure out what the structure of the sentence is (although in a natural language you do
  6. this subconsciously). This process is called \textbf{parsing}.

  7. For example, when you hear the sentence, ``The penny dropped,'' you understand that
  8. ``the penny'' is the subject and ``dropped'' is the predicate. Once you have parsed a sentence,
  9. you can figure out what it means, or the semantics of the sentence. Assuming that
  10. you know what a penny is and what it means to drop, you will understand the general
  11. implication of this sentence.

  12. Although formal and natural languages have many features in common---tokens, structure,
  13. syntax, and semantics---there are some differences:
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:52:22 |显示全部楼层
  1. \begin{description}
  2.   \item[Ambiguity:]  Natural languages are full of ambiguity, which people deal with by using contextual
  3.   clues and other information. Formal languages are designed to be nearly or completely
  4.   unambiguous, which means that any statement has exactly one meaning, regardless of context.
  5.   \item[Redundancy:] In order to make up for ambiguity and reduce misunderstanding, natural languages
  6.   employ lots of redundancy. As a result, they are often verbose. Formal languages
  7.   are less redundant and more concise.
  8.   \item[Literalness: ] Natural languages are full of idiom and metaphor. If I say, ``The penny dropped,''
  9.   there is probably no penny and nothing dropping (this idiom means that someone
  10.   realized something after a period of confusion). Formal languages mean exactly
  11.   what they say.
  12. \end{description}

  13. People who grow up speaking a natural language (everyone) often have a hard time
  14. adjusting to formal languages. In some ways, the difference between formal and natural
  15. language is like the difference between poetry and prose, but more so:

  16. \begin{description}
  17.   \item[Poetry:] Words are used for their sounds as well as for their meaning, and the whole poem
  18.   together creates an effect or emotional response. Ambiguity is not only common
  19.   but often deliberate.
  20.   \item[Prose:] The literal meaning of words is more important, and the structure contributes more
  21.   meaning. Prose is more amenable to analysis than poetry but still often ambiguous.
  22.   \item[Programs:] The meaning of a computer program is unambiguous and literal, and can be
  23.   understood entirely by analysis of the tokens and structure.
  24. \end{description}

  25. Here are some suggestions for reading programs (and other formal languages). First,
  26. remember that formal languages are much more dense than natural languages, so it
  27. takes longer to read them. Also, the structure is very important, so it is usually not a
  28. good idea to read fro stop to bottom, left and right. Instead, learn to parse the program
  29. in your head, identifying the tokens and interpreting the structure. Finally, the details
  30. matter. Small errors in spelling and punctuation, which you can get away with in natural
  31. languages, can make a big difference in a formal language.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:52:33 |显示全部楼层
  1. \section{First Program}

  2. Traditionally, the first program you write in a new language is called ``Hello, World!'
  3. because all it does is display the words ``Hello, World!''. In Python, it looks like this:

  4. \begin{pquote}
  5. \texttt{print 'Hello! World!'}
  6. \end{pquote}

  7. This is an example of a \textbf{print statement}, which doesn't actually print anything on paper.
  8. It displays a value on the screen. In this case, the result is the words

  9. \begin{pquote}
  10. \texttt{Hello! World!}
  11. \end{pquote}

  12. The quotation marks in the program mark the beginning and end of the text to be
  13. displayed; they don't appear in the result.

  14. In Python 3, the syntax for printing is slightly different:

  15. \begin{pquote}
  16. \texttt{print('Hello! World!')}
  17. \end{pquote}

  18. The parentheses indicate that \texttt{print} is a function. We'll get to functions in Chapter 3.

  19. For the rest of this book, I'll use the print statement. If you are using Python 3, you will
  20. have to translate. But other than that, there are very few differences we have to  worry
  21. about.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:52:49 |显示全部楼层
  1. \section{Debugging}

  2. It is a good idea to read this book in front of a computer so you can try out the example
  3. as you go. You can run most of the examples in interactive mode, but if you put the code
  4. in a script, it is easier to try out variations.

  5. Whenever you are experimenting with a new feature, you should try to make mistakes.
  6. For example, in the ``Hello! World!'' program, what happens if you leave out one of the
  7. quotation marks? What if you leave out both? What if you spell \texttt{print} wrong?

  8. This kind of experiment helps you remember what you read; it also helps with debugging,
  9. because you get to know what the error messages mean. It is better to maker mistakes
  10. now and on purpose that later and accidentally.

  11. Programming, and especially debugging, sometimes brings out strong emotions. If you
  12. are struggling with a difficult bug, you might feel angry, despondent or embarrassed.

  13. There is evidence that people naturally respond to computers as if they were people.
  14. When they work well, we think of them as teammates, and when they are obstinate or
  15. rude, we respond to them the same way we respond to rude, obstinate people (Reeves
  16. and Nass, \textit{The Media Equation: How People Treat Computers, Televisions, and New Media
  17. Like Real People and Places}).

  18. Preparing for these reactions might help you deal with them. One approach is to think
  19. of the computer as an employee with certain strengths, like speed and precision, and
  20. particular weaknesses, like lack of empathy and inability to grasp the big picture.

  21. Your job is to be a good manger: find ways to take advantage of the strengths and
  22. mitigate the weakness. And find ways to use your emotions to engage with the problem,
  23. without letting your reactions interfere with your ability to work effectively.

  24. Learning to debug can be frustrating, but it is a valuable skill that is useful for many
  25. activities beyond programming. At the end of each chapter here is a debugging section,
  26. like this one with my thoughts about debugging. I hope they help!
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:53:06 |显示全部楼层
  1. \chapter{Variables, Expressions, and Statements}

  2. \section{Values and Types}

  3. A \textbf{value} is one of the basic things a program works with, like a letter or a number. The
  4. values we have seen so far are \texttt{1}, \texttt{2}, and \texttt{'Hello! World!'}.

  5. These values belong to different \textbf{types}: \texttt{2} is an integer, and \texttt{'Hello! World!'} is a
  6. \textbf{string}, so-called because it contains a ``string'' of letters. You (and the interpreter) can
  7. identify string because they are enclosed in quotation marks.

  8. If you are not sure what type a value has, the interpreter can tell you.

  9. \begin{pquote}
  10. \begin{verbatim}
  11. >>> type('Hello! World!')
  12. <type 'str'>
  13. >>> type(17)
  14. <type 'int'>
  15. \end{verbatim}
  16. \end{pquote}
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:53:19 |显示全部楼层
  1. Not surprisingly, strings belong to the type \texttt{str} and integers belong to the type \texttt{int}. Less
  2. obviously, numbers with a decimal point belong to a type called \texttt{float}, because these
  3. numbers are represented in a format called \textbf{floating-point}.

  4. \begin{pquote}
  5. \begin{verbatim}
  6. >>> type(3.2)
  7. <type 'float'>
  8. \end{verbatim}
  9. \end{pquote}

  10. What about values like \texttt{'17'} and \texttt{'3.2'}? They look like numbers, but they are in quotation
  11. marks like strings.

  12. \begin{pquote}
  13. \begin{verbatim}
  14. >>> type('17')
  15. <type 'str'>
  16. >>> type('3.2')
  17. <type 'str'>
  18. \end{verbatim}
  19. \end{pquote}

  20. They're strings.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:53:33 |显示全部楼层
  1. When you type a large integer, you might be tempted to use commas between groups
  2. of three digits, as in \texttt{1,000,000}. This is not a legal integer in Python, but it is legal:

  3. \begin{pquote}
  4. \begin{verbatim}
  5. >>> 1,000,000
  6. (1,0,0)
  7. \end{verbatim}
  8. \end{pquote}

  9. Well, that's not what we expected at all! Python interprets \texttt{1,000,000} as a comma-separated
  10. sequence of integers. This is the first example we have seen of a semantic error:
  11. the code runs without producing an error message, but it doesn't do the ``right'' thing.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:53:46 |显示全部楼层
  1. \section{Variables}

  2. One of the most powerful features of a programming language is the ability to manipulate
  3. \textbf{variables}. A variable is a name that refers to a value.

  4. An \textbf{assignment statement} creates new variables and gives them values:

  5. \begin{pquote}
  6. \begin{verbatim}
  7. >>> message = 'And now for something completely different'
  8. >>> n = 17
  9. >>> pi = 3.1415926535897932
  10. \end{verbatim}
  11. \end{pquote}

  12. This example makes three assignments. The first assigns a strings to a new variable named
  13. \texttt{message}; the second gives the integer \texttt{17} to \texttt{n}; the third assigns the (approximate) value
  14. of $\pi$ to \texttt{pi}.

  15. A common way to represent variables on paper is to write the name with n arrow
  16. pointing to the variable's value. This kind of figure is called a \textbf{state diagram} because it
  17. shows what state each of the variables is in (think of it as the variable's state of mind).
  18. Figure \ref{statedia} shows the result of the previous example.
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:53:59 |显示全部楼层
  1. \begin{figure}
  2. \label{statedia}
  3. \begin{center}
  4. \includegraphics[width=\textwidth]{thinkpython003.png}
  5. \caption{State diagram}
  6. \end{center}
  7. \end{figure}

  8. The type of a variable is the type of the value it refers to.

  9. \begin{pquote}
  10. \begin{verbatim}
  11. >>> type(message)
  12. <type 'str'>
  13. >>> type(n)
  14. <type 'int'>
  15. >>> type(pi)
  16. <type 'float'>
  17. \end{verbatim}
  18. \end{pquote}
复制代码

使用道具 举报

Rank: 7Rank: 7Rank: 7

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

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

发表于 2017-5-4 14:54:10 |显示全部楼层
  1. \textit{Exercise 2-1}

  2. If you type an integer with a leading zero, you might get a confusing error:

  3. \begin{pquote}
  4. \begin{verbatim}
  5. >>> zipcode = 02492
  6. SyntaxError: invalid token
  7. \end{verbatim}
  8. \end{pquote}

  9. Other numbers seem to work, but the results are bizarre:

  10. \begin{pquote}
  11. \begin{verbatim}
  12. >>> zipcode = 02132
  13. >>> zipcode
  14. 1114
  15. \end{verbatim}
  16. \end{pquote}

  17. Can you figure out what is going on? Hint: display the values \texttt{01}, \texttt{010}, \texttt{0100} and
  18. \texttt{01000}.
复制代码

使用道具 举报

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

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