next up previous contents
Next: Numeric Operators Up: Types of Numeric Data Previous: Types of Numeric Data   Contents

Hexadecimal and Octal Constants

As an alternative to decimal constants, python allows you to enter numbers as either octal (base 8) or hexadecimal (base 16) constants. Octal constants are recognized by python because they start with a leading zero (0); in particular a number like 012 is interpreted as an octal number (with a decimal value of 10), not as a decimal number with a leading zero, and a number like 09 will generate a SyntaxError exception, since 9 is not a valid octal digit.

Hexadecimal constants start with a leading zero, followed by the letter ``x'' (either upper or lower case). In addition to the decimal digits, these constants can contain the letters a, b, c, d, e, or f (again, either upper or lower case), representing the extra hexadecimal digits beyond 9.

Note that these constants are numeric values, not string values, and thus should not be surrounded by quotes. Trying to use a string representation of an octal or hexadecimal constants raises a TypeError exception.

Arithmetic performed on octal or hexadecimal numbers will be displayed in decimal form by the python interpreter. The functions hex and oct convert their arguments into a string representation of the corresponding hexadecimal or octal value, respectively.


next up previous contents
Next: Numeric Operators Up: Types of Numeric Data Previous: Types of Numeric Data   Contents
Phil Spector 2003-11-12