Exercises
Okay: time to start doing a little coding on your own. This session is fairly simple, but a few of these questions hint at topics to come in later chapters. Remember, check Appendix C, for the answers; they sometimes contain supplemental information not discussed in the chapters. In other words, you should peek, even if you can manage to get all the answers on your own.
Interaction. Start the Python command line, and type the expression:
"Hello World!"(including the quotes). The string should be echoed back to you. The purpose of this exercise is to get your environment configured to run Python. You may need to add the path to thepythonexecutable to yourPATHenvironment variable. Set it in your .cshrc or .kshrc file to make Python permanently available on Unix systems; use a setup.bat or autoexec.bat file on Windows.Programs. With the text editor of your choice, write a simple module file—a file containing the single statement:
print'Hellomoduleworld!'. Store this statement in a file named module1.py. Now, run this file by passing it to the Python interpreter program on the system shell’s command line.Modules. Next, start the Python command line and import the module you wrote in the prior exercise. Does your
PYTHONPATHsetting include the directory where the file is stored? Try moving the file to a different directory and importing it again; what happens? (Hint: is there still a file named module1.pyc in the original directory?)Scripts. If your platform supports ...