Posts

15. Floating Point Arithmetic: Issues and Limitations

Image
  15. Floating Point Arithmetic: Issues and Limitations   15. Floating Point Arithmetic: Issues and Limitations Floating-point numbers are represented in computer hardware as base 2 (binary) fractions. For example, the decimal fraction 0.125 has value 1/10 + 2/100 + 5/1000, and in the same way the binary fraction 0.001 has value 0/2 + 0/4 + 1/8. These two fractions have identical values, the only real difference being that the first is written in base 10 fractional notation, and the second in base 2. Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine. The problem is easier to understand at first in base 10. Consider the fraction 1/3. You can approximate that as a base 10 fraction: 0.3 or, better, 0.33 or, better, 0.333 and so on. No matter how many digits you’...

14. Interactive Input Editing and History Substitution

Image
14. Interactive Input Editing and History Substitution   14. Interactive Input Editing and History Substitution Some versions of the Python interpreter support editing of the current input line and history substitution, similar to facilities found in the Korn shell and the GNU Bash shell. This is implemented using the GNU Readline library, which supports various styles of editing. This library has its own documentation which we won’t duplicate here. 14.1. Tab Completion and History Editing Completion of variable and module names is automatically enabled at interpreter startup so that the Tab key invokes the completion function; it looks at Python statement names, the current local variables, and the available module names. For dotted expressions such as string.a , it will evaluate the expression up to the final '.' and then suggest completions from the attributes of the resulting object. Note that this may execute application-defined code if an object with a __getattr__...

13. What Now?

Image
  13. What Now?   13. What Now? 13. What Now? Reading this tutorial has probably reinforced your interest in using Python — you should be eager to apply Python to solving your real-world problems. Where should you go to learn more? This tutorial is part of Python’s documentation set. Some other documents in the set are: The Python Standard Library : You should browse through this manual, which gives complete (though terse) reference material about types, functions, and the modules in the standard library. The standard Python distribution includes a lot of additional code. There are modules to read Unix mailboxes, retrieve documents via HTTP, generate random numbers, parse command-line options, write CGI programs, compress data, and many other tasks. Skimming through the Library Reference will give you an idea of what’s available. Installing Python Modules explains how to install additional modules written by other Python users. The Python Language Reference : A detailed e...

12. Virtual Environments and Packages

Image
  12. Virtual Environments and Packages 12. Virtual Environments and Packages 12.1. Introduction Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface. This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run. The solution for this problem is to create a virtual environment , a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. Different applications can then...

11. Brief Tour of the Standard Library — Part II

Image
  11. Brief Tour of the Standard Library — Part II     11. Brief Tour of the Standard Library — Part II This second tour covers more advanced modules that support professional programming needs. These modules rarely occur in small scripts. 11.1. Output Formatting The reprlib module provides a version of repr() customized for abbreviated displays of large or deeply nested containers: >>> import reprlib >>> reprlib . repr ( set ( 'supercalifragilisticexpialidocious' )) "{'a', 'c', 'd', 'e', 'f', 'g', ...}" The pprint module offers more sophisticated control over printing both built-in and user defined objects in a way that is readable by the interpreter. When the result is longer than one line, the “pretty printer” adds line breaks and indentation to more clearly reveal data structure: >>> import pprint >>> t = [[[[ 'black' , 'cyan' ], 'white' , [ ...