正在加载图片...
1.6.THE MAGIC OF PYTHON 7 Can you see what is happening here?When we use greet we can send different names to customize the result.We will discuss parameters in detail later on.For the time being,our functions will not use parameters, so the parentheses will be empty,but you still need to include them when defining and using functions. One problem with entering functions interactively at the Python prompt like this is that the definitions go away when we quit Python.If we want to use them again the next time,we have to type them all over again. Programs are usually created by typing definitions into a separate file called a module or script.This file is saved on a disk so that it can be used over and over again. A module file is just a text file,and you can create one using any program for editing text,like a notepad or word processor program(provided you save your program as a"plain text"file).A special type of program known as a programming environment simplifies the process.A programming environment is specifically designed to help programmers write programs and includes features such as automatic indenting,color high- lighting,and interactive development.The standard Python distribution includes a programming environment called Idle that you may use for working on the programs in this book. Let's illustrate the use of a module file by writing and running a complete program.Our program will illustrate a mathematical concept known as chaos.Here is the program as we would type it into Idle or some other editor and save in a module file: File:chaos.py A simple program illustrating chaotic behavior. def main(): print "This program illustrates a chaotic function" x input("Enter a number between 0 and 1:" for i in range(10): x=3.9*x*(1-x) print x main() This file should be saved with with the name chaos.py.The.py extension indicates that this is a Python module.You can see that this particular example contains lines to define a new function called main. (Programs are often placed in a function called main.The last line of the file is the command to invoke this function.Don't worry if you don't understand what main actually does;we will discuss it in the next section.The point here is that once we have a program in a module file,we can run it any time we want. This program can be run in a number of different ways that depend on the actual operating system and programming environment that you are using.If you are using a windowing system,you can run a Python program by (double-)clicking on the module file's icon.In a command-line situation,you might type a command like python chaos.py.If you are using Idle (or another programming environment)you can run a program by opening it in the editor and then selecting a command like import,run,or execute. One method that should always work is to start the Python interpreter and then import the file.Here is how that looks >>import chaos This program illustrates a chaotic function Enter a number between 0 and 1:.25 0.73125 0.76644140625 0.698135010439 0.82189581879 0.570894019197 0.955398748364 0.166186721954 0.540417912062 0.9686289303 0.118509010176 >>>1.6. THE MAGIC OF PYTHON 7 Can you see what is happening here? When we use greet we can send different names to customize the result. We will discuss parameters in detail later on. For the time being, our functions will not use parameters, so the parentheses will be empty, but you still need to include them when defining and using functions. One problem with entering functions interactively at the Python prompt like this is that the definitions go away when we quit Python. If we want to use them again the next time, we have to type them all over again. Programs are usually created by typing definitions into a separate file called a module or script. This file is saved on a disk so that it can be used over and over again. A module file is just a text file, and you can create one using any program for editing text, like a notepad or word processor program (provided you save your program as a “plain text” file). A special type of program known as a programming environment simplifies the process. A programming environment is specifically designed to help programmers write programs and includes features such as automatic indenting, color high￾lighting, and interactive development. The standard Python distribution includes a programming environment called Idle that you may use for working on the programs in this book. Let’s illustrate the use of a module file by writing and running a complete program. Our program will illustrate a mathematical concept known as chaos. Here is the program as we would type it into Idle or some other editor and save in a module file: # File: chaos.py # A simple program illustrating chaotic behavior. def main(): print "This program illustrates a chaotic function" x = input("Enter a number between 0 and 1: ") for i in range(10): x = 3.9 * x * (1 - x) print x main() This file should be saved with with the name chaos.py. The .py extension indicates that this is a Python module. You can see that this particular example contains lines to define a new function called main. (Programs are often placed in a function called main.) The last line of the file is the command to invoke this function. Don’t worry if you don’t understand what main actually does; we will discuss it in the next section. The point here is that once we have a program in a module file, we can run it any time we want. This program can be run in a number of different ways that depend on the actual operating system and programming environment that you are using. If you are using a windowing system, you can run a Python program by (double-)clicking on the module file’s icon. In a command-line situation, you might type a command like python chaos.py. If you are using Idle (or another programming environment) you can run a program by opening it in the editor and then selecting a command like import, run, or execute. One method that should always work is to start the Python interpreter and then import the file. Here is how that looks. >>> import chaos This program illustrates a chaotic function Enter a number between 0 and 1: .25 0.73125 0.76644140625 0.698135010439 0.82189581879 0.570894019197 0.955398748364 0.166186721954 0.540417912062 0.9686289303 0.118509010176 >>>
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有