Python numbers

Python can handle normal long integers (the maximum length is determined based on the operating system, just like C), Python long integers (the maximum length is dependent on available memory), floating-point numbers (just like C doubles), octal and hexadecimal numbers, and complex numbers (numbers with an imaginary component).

Here are some examples of these numbers:

  • Integer: 12345, -32
  • Python integer: 999999999L (in Python 3.x, all integers are Python integers)
  • Float: 1.23, 4e5, 3e-4
  • Octal: 012, 0456
  • Hexadecimal: 0xf34, 0X12FA
  • Complex: 3+4j, 2J, 5.0+2.5j

Historically, integers were 16-bit numbers while longs were 32-bit. This could cause problems when using compiled languages, such as C, because trying to store a number that was too big for its data type could cause errors. The largest 16-bit number available is 65535, so trying to store 999999999 in a regular integer would fail.

As advances in computing created 32-bit, then 64-bit, processors and operating systems, the number of bits used has changed, but they are still defined by the OS and the particular programming language compiler used to make the program. In Python 3, all integers are the longest value they can be.

Python has the normal built-in numeric tools you'd expect: expression operators (*, >>, +, <, and so on), math functions (pow, abs, and so on), and utilities (rand, math, and so on). For heavy number-crunching, Python has the Numeric Python (NumPy) (http://www.numpy.org) extension which has such things as matrix data types. It's heavily used in science and mathematical settings, as its power and ease of use make it equivalent to Mathematica, Maple, and MatLab. It's included with the Anaconda Python distribution, and due to the number of other tools included with Anaconda, that's probably the easiest way to obtain NumPy.

Though this probably doesn't mean much to non-programmers, the expression operators found in C have been included in Python; however, some of them are slightly different. Logic operators are spelled out in Python rather than using symbols; for example, logical AND is represented by the word and in Python, not by &&, as in C; logical OR is represented by the word or in Python, not the double-pipe symbol ||; and logical NOT uses the word not instead of !. More information can be found in the Python documentation.

Operator level-of-precedence is the same as C, but using parentheses is highly encouraged to ensure the expression is evaluated correctly and enhances readability. Mixed types (float values combined with integer values) are converted up to the highest type before evaluation; that is, adding a float and an integer will cause the integer to be changed to a float value before the sum is evaluated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.240.119