正在加载图片...
8 CHAPTER 1.COMPUTERS AND PROGRAMS Typing the first line import chaos tells the Python interpreter to load the chaos module from the file chaos.py into main memory.Notice that I did not include the py extension on the import line:Python assumes the module will have a py extension. As Python imports the module file,each line executes.It's just as if we had typed them one-by-one at the interactive Python prompt.The def in the module causes Python to create the main function.When Python encounters the last line of the module,the ma in function is invoked,thus running our program.The running program asks the user to enter a number between 0 and 1(in this case,I typed"25")and then prints out a series of 10 numbers. When you first import a module file in this way,Python creates a companion file with a.pyc extension. In this example,Python creates another file on the disk called chaos.pyc.This is an intermediate file used by the Python interpreter.Technically,Python uses a hybrid compiling/interpreting process.The Python source in the module file is compiled into more primitive instructions called byte code.This byte code (the pyc)file is then interpreted.Having a.pyc file available makes importing a module faster the second time around.However,you may delete the byte code files if you wish to save disk space;Python will automatically re-create them as needed. A module only needs to be imported into a session once.After the module has been loaded,we can run the program again by asking Python to execute the main command.We do this by using a special dot notation. Typing chaos.main (tells Python to invoke the main function in the chaos module.Continuing with our example,here is how it looks when we rerun the program with26 as the input. >>chaos.main() Enter a number between 0 and 1:.26 0.75036 0.73054749456 0.767706625733 0.6954993339 0.825942040734 0.560670965721 0.960644232282 0.147446875935 0.490254549376 0.974629602149 >>> 1.7 Inside a Python Program The output from the chaos program may not look very exciting,but it illustrates a very interesting phe- nomenon known to physicists and mathematicians.Let's take a look at this program line by line and see what it does.Don't worry about understanding every detail right away;we will be returning to all of these ideas in the next chapter. The first two lines of the program start with the character: File:chaos.py A simple program illustrating chaotic behavior. These lines are called comments.They are intended for human readers of the program and are ignored by Python.The Python interpreter always skips any text from the pound sign(#)through the end of a line. The next line of the program begins the definition of a function called main. def main(): Strictly speaking,it would not be necessary to create a main function.Since the lines of a module are executed as they are loaded,we could have written our program without this definition.That is,the module could have looked like this:8 CHAPTER 1. COMPUTERS AND PROGRAMS Typing the first line import chaos tells the Python interpreter to load the chaos module from the file chaos.py into main memory. Notice that I did not include the .py extension on the import line; Python assumes the module will have a .py extension. As Python imports the module file, each line executes. It’s just as if we had typed them one-by-one at the interactive Python prompt. The def in the module causes Python to create the main function. When Python encounters the last line of the module, the main function is invoked, thus running our program. The running program asks the user to enter a number between 0 and 1 (in this case, I typed “.25”) and then prints out a series of 10 numbers. When you first import a module file in this way, Python creates a companion file with a .pyc extension. In this example, Python creates another file on the disk called chaos.pyc. This is an intermediate file used by the Python interpreter. Technically, Python uses a hybrid compiling/interpreting process. The Python source in the module file is compiled into more primitive instructions called byte code. This byte code (the .pyc) file is then interpreted. Having a .pyc file available makes importing a module faster the second time around. However, you may delete the byte code files if you wish to save disk space; Python will automatically re-create them as needed. A module only needsto be imported into a session once. After the module has been loaded, we can run the program again by asking Python to execute the main command. We do this by using a special dot notation. Typing chaos.main() tells Python to invoke the main function in the chaos module. Continuing with our example, here is how it looks when we rerun the program with ￾ 26 as the input. >>> chaos.main() Enter a number between 0 and 1: .26 0.75036 0.73054749456 0.767706625733 0.6954993339 0.825942040734 0.560670965721 0.960644232282 0.147446875935 0.490254549376 0.974629602149 >>> 1.7 Inside a Python Program The output from the chaos program may not look very exciting, but it illustrates a very interesting phe￾nomenon known to physicists and mathematicians. Let’s take a look at this program line by line and see what it does. Don’t worry about understanding every detail right away; we will be returning to all of these ideas in the next chapter. The first two lines of the program start with the # character: # File: chaos.py # A simple program illustrating chaotic behavior. These lines are called comments. They are intended for human readers of the program and are ignored by Python. The Python interpreter always skips any text from the pound sign (#) through the end of a line. The next line of the program begins the definition of a function called main. def main(): Strictly speaking, it would not be necessary to create a main function. Since the lines of a module are executed as they are loaded, we could have written our program without this definition. That is, the module could have looked like this:
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有