Skip to Main Content
Learning Python
book

Learning Python

by Mark Lutz, David Ascher
April 1999
Beginner content levelBeginner
384 pages
11h 15m
English
O'Reilly Media, Inc.
Content preview from Learning Python

Appendix C. Solutions to Exercises

This appendix contains solutions to the exercises that appear at the end of each chapter.

  1. Interaction. Assuming your Python is configured properly, you should participate in an interaction that looks something like this:

    % python
    copyright information lines...
    >>> "Hello World!"
    'Hello World!'
    >>>                       # <Ctrl-D or Ctrl-Z to exit>
  2. Programs. Here’s what your code (i.e., module) file and shell interactions should look like:

    % cat module1.py
    print 'Hello module world!'
    
    % python module1.py
    Hello module world!
  3. Modules. The following interaction listing illustrates running a module file by importing it. Remember that you need to reload it to run again without stopping and restarting the interpreter. The bit 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 bytecode version of a module. We look at how this works in more detail in Chapter 5.

    % python
    >>> import module1
    Hello module world!
    >>>
  4. Scripts. Assuming your platform supports the #! trick, your solution will look like the following (though your #! line may need to list another path on your machine):

    % cat module1.py #!/usr/local/bin/python (or ...
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

Learning Python

Fabrizio Romano
Getting Started with Python

Getting Started with Python

Fabrizio Romano, Benjamin Baka, Dusty Phillips

Publisher Resources

ISBN: 1565924649Supplemental ContentCatalog PageErrata