The way of the program learn to parse the program in your head,identifying the tokens and interpreting the structure.Finally,the details matter.Little things like spelling errors and bad punctuation,which you can get away with in natural languages,can make a big difference in a formal language. 1.5 The first program Traditionally,the first program written in a new language is called"Hello,World!" because all it does is display the words,"Hello,World!"In Python,it looks like this: print "Hello,World!" This is an example of a print statement,which doesn't actually print anything on paper.It displays a value 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 value; they don't appear in the result. Some people judge the quality of a programming language by the simplicity of the "Hello,World!"program.By this standard,Python does about as well as possible. 1.6 Glossary problem solving:The process of formulating a problem,finding a solution,and expressing the solution. high-level language:A programming language like Python that is designed to be easy for humans to read and write. low-level language:A programming language that is designed to be easy for a computer to execute;also called "machine language"or "assembly lan- guage.” portability:A property of a program that can run on more than one kind of computer. interpret:To execute a program in a high-level language by translating it one line at a time. compile:To translate a program written in a high-level language into a low-level language all at once,in preparation for later execution
8 The way of the program learn to parse the program in your head, identifying the tokens and interpreting the structure. Finally, the details matter. Little things like spelling errors and bad punctuation, which you can get away with in natural languages, can make a big difference in a formal language. 1.5 The first program Traditionally, the first program written in a new language is called “Hello, World!” because all it does is display the words, “Hello, World!” In Python, it looks like this: print "Hello, World!" This is an example of a print statement, which doesn’t actually print anything on paper. It displays a value 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 value; they don’t appear in the result. Some people judge the quality of a programming language by the simplicity of the “Hello, World!” program. By this standard, Python does about as well as possible. 1.6 Glossary problem solving: The process of formulating a problem, finding a solution, and expressing the solution. high-level language: A programming language like Python that is designed to be easy for humans to read and write. low-level language: A programming language that is designed to be easy for a computer to execute; also called “machine language” or “assembly language.” portability: A property of a program that can run on more than one kind of computer. interpret: To execute a program in a high-level language by translating it one line at a time. compile: To translate a program written in a high-level language into a low-level language all at once, in preparation for later execution
1.6 Glossary 9 source code:A program in a high-level language before being compiled. object code:The output of the compiler after it translates the program. executable:Another name for object code that is ready to be executed. script:A program stored in a file (usually one that will be interpreted). program:A set of instructions that specifies a computation. algorithm:A general process for solving a category of problems. bug:An error in a program. debugging:The process of finding and removing any of the three kinds of pro- gramming errors. syntax:The structure of a program. syntax error:An error in a program that makes it impossible to parse (and therefore impossible to interpret). runtime error:An error that does not occur until the program has started to execute but that prevents the program from continuing. exception:Another name for a runtime error. semantic error:An error in a program that makes it do something other than what the programmer intended. semantics:The meaning of a program. 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 spe- cific purposes,such as representing mathematical ideas or computer pro- grams;all programming languages are formal languages. token:One of the basic elements of the syntactic structure of a program,analo- gous to a word in a natural language. parse:To examine a program and analyze the syntactic structure. print statement:An instruction that causes the Python interpreter to display a value on the screen
1.6 Glossary 9 source code: A program in a high-level language before being compiled. object code: The output of the compiler after it translates the program. executable: Another name for object code that is ready to be executed. script: A program stored in a file (usually one that will be interpreted). program: A set of instructions that specifies a computation. algorithm: A general process for solving a category of problems. bug: An error in a program. debugging: The process of finding and removing any of the three kinds of programming errors. syntax: The structure of a program. syntax error: An error in a program that makes it impossible to parse (and therefore impossible to interpret). runtime error: An error that does not occur until the program has started to execute but that prevents the program from continuing. exception: Another name for a runtime error. semantic error: An error in a program that makes it do something other than what the programmer intended. semantics: The meaning of a program. 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. parse: To examine a program and analyze the syntactic structure. print statement: An instruction that causes the Python interpreter to display a value on the screen
10 The way of the program
10 The way of the program
Chapter 2 Variables,expressions and statements 2.1 Values and types A value is one of the fundamental things-like a letter or a number-that a program manipulates.The values we have seen so far are 2 (the result when we added 11),and 'Hello,World!'. These values belong to different types:2 is an integer,and 'Hello,World!' is a string,so-called because it contains a "string"of letters.You (and the interpreter)can identify strings because they are enclosed in quotation marks. The print statement also works for integers. >>print 4 4 If you are not sure what type a value has,the interpreter can tell you. >>type('Hello,World!') <type 'str'> >>type(17) <type 'int'> Not surprisingly,strings belong to the type str and integers belong to the type int.Less obviously,numbers with a decimal point belong to a type called float, because these numbers are represented in a format called floating-point
Chapter 2 Variables, expressions and statements 2.1 Values and types A value is one of the fundamental things—like a letter or a number—that a program manipulates. The values we have seen so far are 2 (the result when we added 1 + 1), and ’Hello, World!’. These values belong to different types: 2 is an integer, and ’Hello, World!’ is a string, so-called because it contains a “string” of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks. The print statement also works for integers. >>> print 4 4 If you are not sure what type a value has, the interpreter can tell you. >>> type(’Hello, World!’) <type ’str’> >>> type(17) <type ’int’> Not surprisingly, strings belong to the type str and integers belong to the type int. Less obviously, numbers with a decimal point belong to a type called float, because these numbers are represented in a format called floating-point
12 Variables,expressions and statements >>type(3.2) <type 'float'> What about values like '17'and '3.2'?They look like numbers,but they are in quotation marks like strings. >>type('17') <type 'str'> >>type(3.2) <type 'str'> They're strings. When you type a large integer,you might be tempted to use commas between groups of three digits,as in 1,000,000.This is not a legal integer in Python,but it is a legal expression: >>print1,000,000 100 Well,that's not what we expected at all!Python interprets 1,000,000 as a comma-separated list of three integers,which it prints consecutively.This is the first example we have seen of a semantic error:the code runs without producing an error message,but it doesn't do the "right"thing. 2.2 Variables One of the most powerful features of a programming language is the ability to manipulate variables.A variable is a name that refers to a value. The assignment statement creates new variables and gives them values: >>message ="What's up,Doc?" >>>n=17 >>pi=3.14159 This example makes three assignments.The first assigns the string "What's up, Doc?"to a new variable named message.The second gives the integer 17 to n, and the third gives the floating-point number 3.14159 to pi. Notice that the first statement uses double quotes to enclose the string.In general, single and double quotes do the same thing,but if the string contains a single quote (or an apostrophe,which is the same character),you have to use double quotes to enclose it
12 Variables, expressions and statements >>> type(3.2) <type ’float’> What about values like ’17’ and ’3.2’? They look like numbers, but they are in quotation marks like strings. >>> type(’17’) <type ’str’> >>> type(’3.2’) <type ’str’> They’re strings. When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000. This is not a legal integer in Python, but it is a legal expression: >>> print 1,000,000 1 0 0 Well, that’s not what we expected at all! Python interprets 1,000,000 as a comma-separated list of three integers, which it prints consecutively. This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn’t do the “right” thing. 2.2 Variables One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value. The assignment statement creates new variables and gives them values: >>> message = "What’s up, Doc?" >>> n = 17 >>> pi = 3.14159 This example makes three assignments. The first assigns the string "What’s up, Doc?" to a new variable named message. The second gives the integer 17 to n, and the third gives the floating-point number 3.14159 to pi. Notice that the first statement uses double quotes to enclose the string. In general, single and double quotes do the same thing, but if the string contains a single quote (or an apostrophe, which is the same character), you have to use double quotes to enclose it