12.07.2015 Views

Is Python a

Is Python a

Is Python a

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Numeric LiteralsAmong its basic types, <strong>Python</strong> provides the usual numeric types: it supports integerand floating-point numbers (with a fractional part), and all their associated syntaxand operations. Like the C language, <strong>Python</strong> also allows you to write integers usinghexadecimal and octal literals. But, unlike C, <strong>Python</strong> additionally offers a complexnumber type, as well as a long integer type with unlimited precision (it can grow tohave as many digits as your memory space allows). Table 5-1 shows what <strong>Python</strong>’snumeric types look like when written out in a program (that is, as literals).Table 5-1. Numeric literalsLiteralInterpretation1234, -24, 0 Normal integers (C longs)9999999999999999999LLong integers (unlimited size)1.23, 3.14e-10, 4E210, 4.0e+210 Floating-point numbers (C doubles)0177, 0x9ff, 0XFF Octal and hex literals for integers3+4j, 3.0+4.0j, 3JComplex number literalsIn general, <strong>Python</strong>’s numeric types are straightforward, but a few coding conceptsare worth highlighting here:Integer and floating-point literalsIntegers are written as strings of decimal digits. Floating-point numbers have anembedded decimal point, and/or an optional signed exponent introduced by ane or E. If you write a number with a decimal point or exponent, <strong>Python</strong> makes ita floating-point object, and uses floating-point (not integer) math when theobject is used in an expression. The rules for writing floating-point numbers arethe same in <strong>Python</strong> as in the C language.Numeric precision and long integersPlain <strong>Python</strong> integers (row 1 of Table 5-1) are implemented as C “longs” internally(i.e., at least 32 bits), and <strong>Python</strong> floating-point numbers are implementedas C “doubles”; <strong>Python</strong> numbers therefore get as much precision as the C compilerused to build the <strong>Python</strong> interpreter gives to longs and doubles. *Long integer literalsIf, however, an integer literal ends with an l or L, it becomes a <strong>Python</strong> long integer(not to be confused with a C long) and can grow as large as needed. In<strong>Python</strong> 2.2 and later, because integers are automatically converted to long integerswhen their values overflow 32 bits, you don’t need to type the letter Lyourself—<strong>Python</strong> automatically converts up to long integer when extra precisionis needed.* That is, the standard C<strong>Python</strong> implementation. In the Jython Java-based implementation, <strong>Python</strong> types arereally Java classes.94 | Chapter 5: Numbers

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

Saved successfully!

Ooh no, something went wrong!