<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Chapter 1 The way of the program 編程之路 The goal of this book is to teach you to think like a computer scientist. This way of thinking combines some of the best features of mathematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions. > 本書的目的是叫你學會像計算機科學家一樣來思考。這種思考方式匯聚了數學、工程和自然科學的精華。計算機科學家像數學家一樣,使用規范的語言來闡述思想(尤其是一些計算);像工程師一樣設計、組裝系統,并且在多重選擇中尋找最優解;像自然科學家一樣觀察復雜系統的行為模式,建立猜想,測試預估的結果。 The single most important skill for a computer scientist is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills. That’s why this chapter is called, “The way of the program”. > 計算機科學家唯一最重要的技能就是『解決問題』。解決問題意味著要有能力把問題進行方程化,創造性地考慮解決思路,并且清晰又精確地表達出解決方案。而學習編程的過程,正是一個培養這種解決問題能力的絕佳機會。本章的標題是『編程之路』,原因就在此。 On one level, you will be learning to program, a useful skill by itself. On another level, you will use programming as a means to an end. As we go along, that end will become clearer. > 在一定層面上,大家將通過編程本身來學習編程這一重要的技巧。在另外一些層面上,大家也將把編程作為實現一種目的的途徑。這一目的會隨著我們逐漸學習而越發清楚。 ## 1.1 What is a program? 程序是什么? A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or something graphical, like processing an image or playing a video. > 程序是一個指令的序列,來告訴機器如何進行一組運算。這種運算也許是數學上的,比如求解一組等式或者求多項式的根;當然也可以是符號運算,比如在文檔中搜索和替換文字,或者一些圖形化過程,比如處理圖像或者播放一段視頻。 The details look different in different languages, but a few basic instructions appear in just about every language: * input: Get data from the keyboard, a file, the network, or some other device. * output: Display data on the screen, save it in a file, send it over the network, etc. * math: Perform basic mathematical operations like addition and multiplication. * conditional execution: Check for certain conditions and run the appropriate code. * repetition: Perform some action repeatedly, usually with some variation. > 不同編程語言的具體細節看著很不一樣,但幾乎所有編程語言都會有一些基礎指令: > * 輸入系統:從鍵盤、文件、網絡或者其他設備上獲得數據。 > * 輸出系統:將數據在屏幕中顯示,或者存到文件中、通過網絡發送等等。 > * 數學運算:進行基本的數學操作,比如加法或者乘法。 > * 條件判斷:檢查特定條件是否滿足來運行相應的代碼。 > * 重復判斷:重復進行一些操作,通常會有些變化。 Believe it or not, that’s pretty much all there is to it. Every program you’ve ever used, no matter how complicated, is made up of instructions that look pretty much like these. So you can think of programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions. > 大家可能不太相信,核心內容就這么多。你用過的所有程序,無論多么復雜,都是由一些這樣的指令組合而成的。因此大家可以把編程的過程理解成一個把龐大復雜任務進行拆分來解決的過程,分解到適合使用上述的基本指令來解決為止。 ## 1.2 Running Python 運行Python One of the challenges of getting started with Python is that you might have to install Python and related software on your computer. If you are familiar with your operating system, and especially if you are comfortable with the command-line interface, you will have no trouble installing Python. But for beginners, it can be painful to learn about system administration and programming at the same time. > 新手在剛接觸Python的時候遇到的困難之一就是必須在電腦上安裝Python和相關的一些軟件。如果你熟悉操作系統,并且還很習慣用命令行界面,那安裝Python對你來說就沒啥問題了。但對初學者來說,要求他們既要了解系統管理又要學習編程,就可能有些困難了。 To avoid that problem, I recommend that you start out running Python in a browser. Later, when you are comfortable with Python, I’ll make suggestions for installing Python on your computer. > 為了避免這種問題,我推薦大家可以在開始的時候用瀏覽器來體驗Python。熟悉了之后,再安裝Python到計算機上。 There are a number of web pages you can use to run Python. If you already have a favorite, go ahead and use it. Otherwise I recommend PythonAnywhere. I provide detailed instructions for getting started athttp://tinyurl.com/thinkpython2e. > 有很多站點提供在線運行Python的功能。如果你已經用過并且有一定經驗了,可以選擇你喜歡的。我推薦大家可以試試PythonAnywhere,對此的使用介紹可以在下面的鏈接中找到http://tinyurl.com/thinkpython2e。 There are two versions of Python, called Python 2 and Python 3. They are very similar, so if you learn one, it is easy to switch to the other. In fact, there are only a few differences you will encounter as a beginner. This book is written for Python 3, but I include some notes about Python 2. > Python現在有兩個主要的分之,即Python2和Python3。如果你學過其中的一個,你會發現他們還挺相似的,而且轉換起來也不算難。實際上對于初學者來說,他們只有很細微的差別而已。這本書是用Python3寫的,但也會對Python2進行注解。 The Python interpreter is a program that reads and executes Python code. Depending on your environment, you might start the interpreter by clicking on an icon, or by typing python on a command line. When it starts, you should see output like this: > Python的解釋器是一個讀取并執行Python代碼的程序。根據你的系統環境,你可以點擊圖標或者在命令行中輸入python來運行解釋器。它運行起來,你會看到類似這樣的輸出: ```python Python 3.4.0 (default, Jun 19 2015, 14:20:21) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> ``` The first three lines contain information about the interpreter and the operating system it’s running on, so it might be different for you. But you should check that the version number, which is 3.4.0 in this example, begins with 3, which indicates that you are running Python 3. If it begins with 2, you are running (you guessed it) Python 2. > 開頭的三行包含了關于解釋器和所在操作系統的信息,所以大家各自的情況可能有所不同。不過當你檢查版本的時候,比如例子中的是3.4.0,使用3開頭的,那就告訴你了,他運行的是Python3。你肯定也能猜到,如果開頭的是2那就是Python2咯。 The last line is a prompt that indicates that the interpreter is ready for you to enter code. If you type a line of code and hit Enter, the interpreter displays the result: > 最后一行那個是提示符,告訴你解釋器已經就緒了,你可以輸入代碼了。如果你輸入一行代碼然后回車鍵,解釋器就會顯示結果了,如下所示: ```python >>> 1 + 1 >>> 1 + 1 2 ``` Now you’re ready to get started. From here on, I assume that you know how to start the Python interpreter and run code. > 現在你已經做好開始學習Python的準備了。現在我估計你應該已經知道怎么來啟動Python解釋器和運行Python代碼了。 ## 1.3 The first program 第一個程序 Traditionally, the first program you write in a new language is called “Hello, World!” because all it does is display the words “Hello, World!”. In Python, it looks like this: > 傳統意義上,大家學一門新編程語言要寫的第一個程序都被叫做『Hello,World!』,因為這第一個程序就用來顯示這個詞組『Hello,World!』。在Python中,是這樣實現的: ```python >>> print('Hello, World!') >>> print('Hello, World!') ``` This is an example of a print statement, although it doesn’t’t actually print anything on paper. It displays a result on the screen. In this case, the result is the words Hello, World! The quotation marks in the program mark the beginning and end of the text to be displayed; they don’t appear in the result. > 這是一個打印語句的例子,雖然并沒有往紙張上面進行實際的『打印』。這個程序把結果顯示在屏幕上。結果就是輸出了這個詞組『Hello,World!』 The parentheses indicate that print is a function. We’ll get to functions in Chapter 3. > 括號表明了print是一個函數。關于函數我們到第三章再討論。 In Python 2, the print statement is slightly different; it is not a function, so it doesn’t use parentheses. > 在Python2中,打印的語句有一點點不一樣:print不是一個函數,所以就不用有括號了。 ```python >>> print 'Hello, World!' >>> print 'Hello, World!' ``` This distinction will make more sense soon, but that’s enough to get started. 這個區別以后會理解更深入,現在說這點就夠了。 ## 1.4 Arithmetic operators 運算符 After “Hello, World”, the next step is arithmetic. Python provides operators, which are special symbols that represent computations like addition and multiplication. > 在『Hello,World!』之后,下一步就是運算了。Python提供了運算符,就是一些用來表示例如加法、乘法等運算的符號了。 The operators +, -, and * perform addition, subtraction, and multiplication, as in the following examples: > 運算符+,-和*表示加法、減法和乘法,如下所示: ```python >>> 40 + 2 >>> 40 + 2 42 >>> 43 - 1 >>> 43 - 1 42 >>> 6 * 7 >>> 6 * 7 42 ``` The operator / performs division: > 運算符右斜杠/意味著除法: ```python >>> 84 / 2 >>> 84 / 2 42.0 ``` You might wonder why the result is 42.0 instead of 42. I’ll explain in the next section. > 你估計在納悶為啥結果是42.0而不是42,這個下一章節我再解釋。 Finally, the operator ** performs exponentiation; that is, it raises a number to a power: > 最后,再說個運算符**,它表示乘方,就是前一個數為底數,后一個數為指數的次冪運算: ```python >>> 6**2 + 6 >>> 6**2 + 6 42 ``` In some other languages, ^ is used for exponentiation, but in Python it is a bitwise operator called XOR. If you are not familiar with bitwise operators, the result will surprise you: > 在其他的一些編程語言中,^這個符號是乘方的意思,但在Python中這是一個位運算操作符叫做『異或』。要是你不熟悉位運算操作符,結果一定讓你很驚訝: ```python >>> 6 ^ 2 >>> 6 ^ 2 4 ``` I won’t cover bitwise operators in this book, but you can read about them at [Wiki](http://wiki.python.org/moin/BitwiseOperators). 我在本書中不會涉及到位運算,但你可以在下面這個鏈接里面讀一下來了解:[Wiki](http://wiki.python.org/moin/BitwiseOperators)。 ## 1.5 Values and types 值和類型 A value is one of the basic things a program works with, like a letter or a number. Some values we have seen so far are 2, 42.0, and 'Hello, World!'. > 值就是一個程序操作的基本對象之一,比如一個字母啊,或者數字。剛剛我們看到了一些值的例子了,比如2,42.0,還有那個字符串『Hello,World!』 These values belong to different types: 2 is an integer, 42.0 is a floating-point number, and 'Hello, World!' is a string, so-called because the letters it contains are strung together. > 這些值屬于不同的類型:2是一個整形值,42.0是浮點數,『Hello,World!』是字符串咯。之所以叫字符串就是因為有一串字符。(譯者注:這本書的作者真心掰開揉碎地講解每一個點啊,高中生甚至初中生都應該理解起來沒有什么問題,所以大家用這本書來學編程絕對是最佳選擇了。) If you are not sure what type a value has, the interpreter can tell you: > 如果你不確定一個值是什么類型呢,你可以讓解釋器來告訴你: ```python >>> type(2) >>> type(2) <class 'int'> >>> type(42.0) >>> type(42.0) <class 'float'> >>> type('Hello, World!') >>> type('Hello, World!') <class 'str'> ``` In these results, the word “class” is used in the sense of a category; a type is a category of values. > 在這些例子中,『class』這個字樣表明這是一類,一種類型就是對值的一種劃分。 Not surprisingly, integers belong to the type int, strings belong to str and floating-point numbers belong to float. > 很自然了,整形的就是int了,字符串就是str了,浮點數就是float了。 What about values like '2' and '42.0'? They look like numbers, but they are in quotation marks like strings. > 那'2' 和 '42.0'這種是啥呢?他們看著像是數字,但帶了單引號了。 ```python >>> type('2') >>> type('2') <class 'str'> >>> type('42.0') >>> type('42.0') <class 'str'> ``` They’re strings. > 真相就是——他們也是字符串了。 When you type a large integer, you might be tempted to use commas between groups of digits, as in 1,000,000. This is not a legal integer in Python, but it is legal: > 咱們現在輸入一個大的整數,在中間用逗號分隔試試看,比如1,000,000,并不是Python中合乎語法的整形,但也被接受了: ```python >>> 1,000,000 >>> 1,000,000 (1, 0, 0) ``` That’s not what we expected at all! Python interprets 1,000,000 as a comma-separated sequence of integers. We’ll learn more about this kind of sequence later. > 出乎意料吧,Python把逗號當做了分隔三個整形數字的分隔符了。我們以后再對這種序列進行討論。 ## 1.6 Formal and natural languages 公式語言和自然語言 Natural languages are the languages people speak, such as English, Spanish, and French. They were not designed by people (although people try to impose some order on them); they evolved naturally. > 自然語言就是人說的語言,比如英語、西班牙語、法語,當然包括中文了。他們往往都不是人主動去設計出來的(當然,人會試圖去分析語言的規律),自然而然地發生演進。 Formal languages are languages that are designed by people for specific applications. For example, the notation that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols. Chemists use a formal language to represent the chemical structure of molecules. And most importantly: Programming languages are formal languages that have been designed to express computations. > 公式語言是人們為了特定用途設計出來的。比如數學的符號就是一種公式語言,特別適合表達數字和符號只見的關系。化學家也用元素符號和化學方程式來表示分子的化學結構。要注意的是: 編程語言是一種用來表達運算的公式語言。 Formal languages tend to have strict syntax rules that govern the structure of statements. For example, in mathematics the statement 3 + 3 = 6 has correct syntax, but 3 + = 3 $ 6 does not. In chemistry H2O is a syntactically correct formula, but 2Zz is not. > 公式語言有嚴格的語法規則和對語句結構的要求。比如數學式3+3=6是正確的,而3+=3¥6就不是了。化學上H2O 是正確的化學式,而2Zz 就不是。 Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are the basic elements of the language, such as words, numbers, and chemical elements. One of the problems with 3 += 3 $ 6 is that $ is not a legal token in mathematics (at least as far as I know). Similarly, 2Zz is not legal because there is no element with the abbreviation Zz. > 語法規則體現在兩個方面,代號和結構。 代號是語言的基礎元素,比如單詞、數字以及化學元素。3 += 3 $ 6這個式子數學上無意義的一個原因就是因為 $ 并不是數學上的符號 (至少我所學的數學是沒有這個符號的)。類似地, 2Zz 也不對,因為沒有一種化學元素的縮寫是 Zz. The second type of syntax rule pertains to the way tokens are combined. The equation 3 += 3 is illegal because even though + and = are legal tokens, you can’t have one right after the other. Similarly, in a chemical formula the subscript comes after the element name, not before. > 第二個語法規則是代號必須有嚴格的組合結構。3 += 3這個式子數學上錯誤就因為雖然這些符號都是數學符號,但不能把加號等號放一起。類似地,化學方程式中要先寫元素名字后寫個數,而不是反著。 This is @ well-structured Engli$h sentence with invalid t*kens in it. This sentence all valid tokens has, but invalid structure with. > 這句英語的單詞和結構都有錯誤,大家還是能看懂的哈。(譯者注,作者故意這樣寫,來表明人類的自然語言容錯率高。) When you read a sentence in English or a statement in a formal language, you have to figure out the structure (although in a natural language you do this subconsciously). This process is called parsing. > 你讀一句英語或者公式語言中的語句時候,你必須搞清楚結構(雖然在自然語言中大家潛意識就能搞定了)。這就叫做解譯。 Although formal and natural languages have many features in common—tokens, structure, and syntax—there are some differences: * ambiguity: Natural languages are full of ambiguity, which people deal with by using contextual clues and other information. Formal languages are designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning, regardless of context. * redundancy: In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more concise. * literalness: Natural languages are full of idiom and metaphor. If I say, “The penny dropped”, there is probably no penny and nothing dropping (this idiom means that someone understood something after a period of confusion). Formal languages mean exactly what they say. > 雖然公式語言和自然語言有很多共同特征,比如代號、結構、語法這些元素,但差別還是顯著的,比如: > * 二義性ambiguity: 自然語言充滿二義性,也就是歧義了,人們有時候用上下文線索或者其他信息來幫助處理這種情況。公式語言被設計為盡量不具有二義性,這就意味著一個語句往往只有唯一的一種含義,與上下文無關。 > * 冗余性redundancy: 為了彌補歧義,減少誤解,自然語言有很多冗余,結果就是經常有廢話。公式語言要精簡的多。 > * 文字修辭literalness: 自然語言充滿習語和隱喻等。比如我說 “The penny dropped”, 可能并不是字面意思說硬幣掉了(這個俚語意思是過了一會終于弄明白了)。公式語言的意思嚴格精準。 Because we all grow up speaking natural languages, it is sometimes hard to adjust to formal languages. The difference between formal and natural language is like the difference between poetry and prose, but more so: * Poetry: Words are used for their sounds as well as for their meaning, and the whole poem together creates an effect or emotional response. Ambiguity is not only common but often deliberate. * Prose: The literal meaning of words is more important, and the structure contributes more meaning. Prose is more amenable to analysis than poetry but still often ambiguous. * Programs: The meaning of a computer program is unambiguous and literal, and can be understood entirely by analysis of the tokens and structure. > 咱們大家都是說著自然語言長大的,要調節到公式語言有時候挺難的。這兩者之間的差別有點像詩詞和散文,但差別更大: > * 詩詞Poetry: 單詞的運用要兼顧詞義和押韻,詩的整體要有一定的意境或者情感上的共鳴。雙關很常見,并且多是故意的。 > * 散文Prose: 文字意思更重要,結構也有重要作用。相比詩詞更好理解,但也有一定的雙關語歧義。 > * 程序Programs: 計算機程序的意義必須是無歧義和文采修飾的,能完全用代號和結構的方式進行解析。 Formal languages are more dense than natural languages, so it takes longer to read them. Also, the structure is important, so it is not always best to read from top to bottom, left to right. Instead, learn to parse the program in your head, identifying the tokens and interpreting the structure. Finally, the details matter. Small errors in spelling and punctuation, which you can get away with in natural languages, can make a big difference in a formal language. > 公式語言比自然語言要更加密集,讀起來也需要更長時間。公式語言的結構也非常重要,所以從頭到尾或者從左到右未必就是最佳方式。大家應該學著動腦來解譯程序,分辨代號,解析結構。最后要注意的就是在公式語言中,細節特別特別重要。拼寫和符號的小錯誤對于自然語言來說沒什么,但對公式語言來說就能帶來大問題。 ## 1.7 Debugging 調試 Programmers make mistakes. For whimsical reasons, programming errors are called bugs and the process of tracking them down is called debugging. > 程序員也會犯錯的。由于很奇妙的原因,程序的錯誤被叫做bug,調試的過程就叫debug了。(譯者注:一個傳言是最早的計算機中經常有蟲子進去導致短路之類的,清理蟲子就成了常規調試的操作,流傳至今。。。) Programming, and especially debugging, sometimes brings out strong emotions. If you are struggling with a difficult bug, you might feel angry, despondent, or embarrassed. > 編程,尤其是調試的過程,有時候會給人帶來強烈的挫敗感。面對特別復雜的狀況,你可能就感到憤怒、壓抑,或者特別難受。 There is evidence that people naturally respond to computers as if they were people. When they work well, we think of them as teammates, and when they are obstinate or rude, we respond to them the same way we respond to rude, obstinate people (Reeves and Nass, The Media Equation: How People Treat Computers, Television, and New Media Like Real People and Places). > 別擔心,這些都是正常人對計算機的正常反應。計算機工作正常了,我們會覺得他們像是隊友一樣;一旦工作出錯了,對我們很粗暴,我們對他們的反應就像是對待粗暴可恨的人一樣(參考Reeves和Nass,The Media Equation: How People Treat Computers, Television, and New Media Like Real People and Places)。 Preparing for these reactions might help you deal with them. One approach is to think of the computer, as an employee with certain strengths, like speed and precision, and particular weaknesses, like lack of empathy and inability to grasp the big picture. > 為這些反應做好心理準備,這樣你在遇到類似情況就更好應對了。我們也可以把計算機當做一個有一定優點但也有特定缺陷的員工,比如速度快精度高,但缺乏共鳴和應對大場面的能力。 Your job is to be a good manager: find ways to take advantage of the strengths and mitigate the weaknesses. And find ways to use your emotions to engage with the problem, without letting your reactions interfere with your ability to work effectively. > 你的工作就是做個好的經理人:盡量充分利用員工優勢并降低他們缺陷的作用。然后想辦法把你的情緒用在解決問題上,而不要讓過激的反應干擾工作效率。 Learning to debug can be frustrating, but it is a valuable skill that is useful for many activities beyond programming. At the end of each chapter there is a section, like this one, with my suggestions for debugging. I hope they help! > 調試的過程挺煩人的,但這個本領很有價值,而且在編程之外的其他領域都有用武之地。在每一章的末尾,都會有這樣的一段,我會給出一些關于調試方面的建議。希望能幫到大家! ## 1.8 Glossary 術語列表 problem solving: The process of formulating a problem, finding a solution, and expressing it. > 問題解決:將問題方程化,找到解決方案,并表達出來的過程。 high-level language: A programming language like Python that is designed to be easy for humans to read and write. > 高級語言:例如Python這樣的編程語言,設計初衷為易于被人閱讀和書寫。 l ow-level language: A programming language that is designed to be easy for a computer to run; also called “machine language” or “assembly language”. > 低級語言:設計初衷為易于被計算機運行的語言,比如機器語言和匯編語言。 portability: A property of a program that can run on more than one kind of computer. > 可移植性:程序能運行于多種平臺的特性。 interpreter: A program that reads another program and executes it > 解釋器:一邊讀取一邊執行代碼的程序。 prompt: Characters displayed by the interpreter to indicate that it is ready to take input from the user. > 提示符:解釋器顯示的,提醒用戶準備就緒,隨時可以輸入。 program: A set of instructions that specifies a computation. > 程序:進行一種特定運算的一系列指令。 print statement: An instruction that causes the Python interpreter to display a value on the screen. > 打印語句:讓Python解釋器輸出值到屏幕的指令。 operator: A special symbol that represents a simple computation like addition, multiplication, or string concatenation. > 運算符(操作符):一系列特殊的符號,表示一些簡單的運算,比如加減乘除或者字符串操作。 value: One of the basic units of data, like a number or string, that a program manipulates. > 值:數據的基本組成單元,比如數字或者字符串,是程序處理的對象。 type: A category of values. The types we have seen so far are integers (typeint), floating-point numbers (type float), and strings (type str). > 類型:對值的分類,大家剛剛接觸到的有整形int,浮點數float,以及字符串str。 integer: A type that represents whole numbers. 整形:就是整數咯。 floating-point: A type that represents numbers with fractional parts. > 浮點數:簡單說,就是有小數點的數了。 string: A type that represents sequences of characters. > 字符串:一串有序的字符了。 natural language: Any one of the languages that people speak that evolved naturally. > 自然語言:人們說的語言,自然地演化。 formal language: Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are formal languages. > 公式語言:人為設計的用于特定用途的語言,比如數學用途或者計算機編程用的;所有編程語言都是公式語言。 token: One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language. > 代號:程序結構中的一種基本元素,相當于自然語言中的單詞。 syntax: The rules that govern the structure of a program. > 語法:程序語言結構的規則。 parse: To examine a program and analyze the syntactic structure. > 解譯:理解程序并分析語法結構的過程。 bug: An error in a program. > Bug:程序的錯誤。 debugging: The process of finding and correcting bugs. > 調試(debug):搜索和改正程序錯誤的過程。 ## 1.9 Exercises 練習 ### Exercise 1 練習1 It is a good idea to read this book in front of a computer so you can try out the examples as you go. > 你讀這本書的同時最好手邊有臺電腦,這樣你就能把樣例在電腦上隨時運行來看看效果了。 Whenever you are experimenting with a new feature, you should try to make mistakes. For example, in the “Hello, world!” program, what happens if you leave out one of the quotation marks? What if you leave out both? What if you spell print wrong? > 無論你學任何一種新功能的時候,都可以試著犯點錯誤。比如就在這個『Hello,World!』程序,你可以試試去掉一個引號會怎么樣,都去掉會怎么樣,print這個單詞拼錯了會怎么樣等等。 This kind of experiment helps you remember what you read; it also helps when you are programming, because you get to know what the error messages mean. It is better to make mistakes now and on purpose than later and accidentally. > 這種嘗試能讓你對讀到的內容有更深刻的記憶;也有助于你編程,因為你在編程的時候也得知道調試信息的意思。所以最好現在就故意犯些錯誤來看看,比以后毫無準備地遇到要好多了。 1. In a print statement, what happens if you leave out one of the parentheses, or both? > 在print語句后面的括號去掉一個或者兩個,看看會怎么樣? 2. If you are trying to print a string, what happens if you leave out one of the quotation marks, or both? > Print字符串的時候如果你丟掉一個引號或者兩個引號試試看會如何? 3. You can use a minus sign to make a negative number like -2. What happens if you put a plus sign before a number? What about 2++2? > 輸入一個負數試試,比如-2。然后再試試在數字前面添加加號會怎么樣?比如2++2。 4. In math notation, leading zeros are ok, as in 02. What happens if you try this in Python? > 數學上計數用零開頭是可以得,比如02,在Python下面試試會怎樣? 5. What happens if you have two values with no operator between them? > 兩個值中間沒有運算符會怎么樣?
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看