Skip to Content
Learning Python, 2nd Edition
book

Learning Python, 2nd Edition

by Mark Lutz, David Ascher
December 2003
Beginner
620 pages
17h 3m
English
O'Reilly Media, Inc.
Content preview from Learning Python, 2nd Edition

Appendix B. Solutions to Exercises

See Section 3.13 for the exercises.

  1. Interaction. Assuming Python is configured properly, interaction should look something like the following. You can run this any way you like: in IDLE, from a shell prompt, and so on:

    % python
    ...copyright information lines...
    >>> "Hello World!"
    'Hello World!'
    >>>                     # Ctrl-D or Ctrl-Z to exit, or window close
  2. Programs. Your code (i.e., module) file module1.py and shell interactions should look like:

    print 'Hello module world!'
    
    % python module1.py
    Hello module world!

    Again, feel free to run this other ways—by clicking its icon, by IDLE’s Edit/RunScript menu option, and so on.

  3. Modules. The following interaction listing illustrates running a module file by importing it.

    % python
    >>> import module1
    Hello module world!
    >>>

    Remember that you need to reload the module to run again without stopping and restarting the interpreter. The questions about moving the file to a different directory and importing it again is a trick question: if Python generates a module1.pyc file in the original directory, it uses that when you import the module, even if the source code file (.py) has been moved to a directory not on Python’s search path. The .pyc file is written automatically if Python has access to the source file’s directory and contains the compiled byte-code version of a module. See Part V for more on modules.

  4. Scripts. Assuming your platform supports the #! trick, your solution will look like the following ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Python, 5th Edition

Learning Python, 5th Edition

Mark Lutz

Publisher Resources

ISBN: 0596002815Supplemental ContentErrata Page