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.

The L at the end of the digit string tells <strong>Python</strong> to create a long integer object withunlimited precision. In fact, as of <strong>Python</strong> 2.2, even the letter L is optional—<strong>Python</strong>automatically converts a normal integer up to a long integer whenever its value is toolarge to fit in a normal integer (technically, when it overflows normal integer precision,which is usually 32 bits). That is, you don’t need to code the L yourself, as<strong>Python</strong> will provide the extra precision if your program needs it:>>> 9999999999999999999999999999999999999 + 110000000000000000000000000000000000000LLong integers are a convenient built-in tool. For instance, you can use them to countthe national debt in pennies in <strong>Python</strong> directly (if you are so inclined, and haveenough memory on your computer). They are also why we were able to raise 2 tosuch large powers in the examples in Chapter 3:>>> 2L ** 2001606938044258990275541962092341162602522202993782792835301376L>>>>>> 2 ** 2001606938044258990275541962092341162602522202993782792835301376LBecause <strong>Python</strong> must do extra work to support their extended precision, long integermath is usually substantially slower than normal integer math (which usuallymaps directly to the hardware). However, if you need the precision, the fact that it’sbuilt-in for you to use will likely outweigh its performance penalty.Complex NumbersComplex numbers are a distinct core object type in <strong>Python</strong>. If you know what theyare, you know why they are useful; if not, consider this section optional reading.Complex numbers are represented as two floating-point numbers—the real andimaginary parts—and are coded by adding a j or J suffix to the imaginary part. Wecan also write complex numbers with a nonzero real part by adding the two partswith a +. For example, the complex number with a real part of 2 and an imaginarypart of -3 is written 2 + -3j. Here are some examples of complex math at work:>>> 1j * 1J(-1+0j)>>> 2 + 1j * 3(2+3j)>>> (2 + 1j) * 3(6+3j)Complex numbers also allow us to extract their parts as attributes, support all theusual mathematical expressions, and may be processed with tools in the standardcmath module (the complex version of the standard math module). Complex numberstypically find roles in engineering-oriented programs. Because they are advancedtools, check <strong>Python</strong>’s language reference manual for additional details.104 | Chapter 5: Numbers

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

Saved successfully!

Ooh no, something went wrong!