12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

B RAINB UILDERPart I ExercisesIt’s time to start doing a little coding on your own. This first exercise session is fairlysimple, but a few of these questions hint at topics to come in later chapters. Be sureto check “Part I, Getting Started” in the solutions appendix (Appendix B) for theanswers; the exercises and their solutions sometimes contain supplemental informationnot discussed in the main text of the part, so you should take a peek at the solutionseven if you manage to answer all the questions on your own.1. Interaction. Using a system command line, IDLE, or another method, start the<strong>Python</strong> interactive command line (>>> prompt), and type the expression "HelloWorld!" (including the quotes). The string should be echoed back to you. Thepurpose of this exercise is to get your environment configured to run <strong>Python</strong>. Insome scenarios, you may need to first run a cd shell command, type the full pathto the <strong>Python</strong> executable, or add its path to your PATH environment variable. Ifdesired, you can set PATH in your .cshrc or .kshrc file to make <strong>Python</strong> permanentlyavailable on Unix systems; on Windows, use a setup.bat, autoexec.bat, orthe environment variable GUI. See Appendix A for help with environment variablesettings.2. Programs. With the text editor of your choice, write a simple module file containingthe single statement print 'Hello module world!' and store it as module1.py.Now, run this file by using any launch option you like: running it in IDLE, clickingon its file icon, passing it to the <strong>Python</strong> interpreter program on the systemshell’s command line (e.g., python module1.py), and so on. In fact, experiment byrunning your file with as many of the launch techniques discussed in this chapteras you can. Which technique seems easiest? (There is no right answer to thisone.)3. Modules. Next, start the <strong>Python</strong> interactive command line (>>> prompt) andimport the module you wrote in exercise 2. Try moving the file to a differentdirectory and importing it again from its original directory (i.e., run <strong>Python</strong> inthe original directory when you import). What happens? (Hint: is there still amodule1.pyc byte code file in the original directory?)4. Scripts. If your platform supports it, add the #! line to the top of your module1.pymodule file, give the file executable privileges, and run it directly as an executable.What does the first line need to contain? #! usually only has meaning onUnix, Linux, and Unix-like platforms such as Mac OS X; if you’re working onWindows, instead try running your file by listing just its name in a DOS consolewindow without the word “python” before it (this works on recent versions ofWindows), or via the Start ➝ Run...dialog box.Part I Exercises | 61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!