正在加载图片...
6 CHAPTER 1.COMPUTERS AND PROGRAMS >>print "Hello,World" Hello,World >>print 2+3 5 >>>print"2+3=",2+3 2+3=5 Here I have tried out three examples using the Python print statement.The first statement asks Python to display the literal phrase Hello,World.Python responds on the next line by printing the phrase.The second print statement asks Python to print the sum of 2 and 3.The third print combines these two ideas. Python prints the part in quotes"2+3="followed by the result of adding 2+3,which is 5. This kind of interaction is a great way to try out new things in Python.Snippets of interactive sessions are sprinkled throughout this book.When you see the Python prompt >>>in an example,that should tip you off that an interactive session is being illustrated.It's a good idea to fire up Python and try the examples for yourself. Usually we want to move beyond snippets and execute an entire sequence of statements.Python lets us put a sequence of statements together to create a brand-new command called a fiction.Here is an example of creating a new function called hello. >>def hello(): print "Hello" print "Computers are Fun" >>> The first line tells Python that we are defining a new function called hello.The following lines are indented to show that they are part of the hello function.The blank line (obtained by hitting the <Enter>key twice)lets Python know that the definition is finished,and the interpreter responds with another prompt. Notice that the definition did not cause anything to happen.We have told Python what should happen when the hello function is used as a command;we haven't actually asked Python to perform it yet. A function is invoked by typing its name.Here's what happens when we use our hello command. >>>he11o() Hello Computers are Fun >>> Do you see what this does?The two print statements from the hello function are executed in sequence. You may be wondering about the parentheses in the definition and use of hello.Commands can have changeable parts called parameters that are placed within the parentheses.Let's look at an example of a customized greeting using a parameter.First the definition: >>def greet(person): print "Hello",person print "How are you?" Now we can use our customized greeting. >>greet ("John") Hello John How are you? >>greet ("Emily") Hello Emily How are you? >>>6 CHAPTER 1. COMPUTERS AND PROGRAMS >>> print "Hello, World" Hello, World >>> print 2 + 3 5 >>> print "2 + 3 =", 2 + 3 2 + 3 = 5 Here I have tried out three examples using the Python print statement. The first statement asks Python to display the literal phrase Hello, World. Python responds on the next line by printing the phrase. The second print statement asks Python to print the sum of 2 and 3. The third print combines these two ideas. Python prints the part in quotes “2 + 3 =” followed by the result of adding 2 + 3, which is 5. This kind of interaction is a great way to try out new things in Python. Snippets of interactive sessions are sprinkled throughout this book. When you see the Python prompt ￾✁￾✁￾ in an example, that should tip you off that an interactive session is being illustrated. It’s a good idea to fire up Python and try the examples for yourself. Usually we want to move beyond snippets and execute an entire sequence of statements. Python lets us put a sequence of statements together to create a brand-new command called a function. Here is an example of creating a new function called hello. >>> def hello(): print "Hello" print "Computers are Fun" >>> The first line tells Python that we are defining a new function called hello. The following lines are indented to show that they are part of the hello function. The blank line (obtained by hitting the <Enter> key twice) lets Python know that the definition is finished, and the interpreter responds with another prompt. Notice that the definition did not cause anything to happen. We have told Python what should happen when the hello function is used as a command; we haven’t actually asked Python to perform it yet. A function is invoked by typing its name. Here’s what happens when we use our hello command. >>> hello() Hello Computers are Fun >>> Do you see what this does? The two print statements from the hello function are executed in sequence. You may be wondering about the parentheses in the definition and use of hello. Commands can have changeable parts called parameters that are placed within the parentheses. Let’s look at an example of a customized greeting using a parameter. First the definition: >>> def greet(person): print "Hello", person print "How are you?" Now we can use our customized greeting. >>> greet("John") Hello John How are you? >>> greet("Emily") Hello Emily How are you? >>>
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有