8. Numbers in python, 8. python numbers

Source: Internet
Author: User
Tags bitwise operators integer numbers

8. Numbers in python, 8. python numbers

The numbers in python include integer int, long, float, and complex. The difference is:

Int (integer)

It is also called a signed integer, which only has a positive or negative integer without a decimal point.
The integer and the long integer have a certain number of digits limit:
On a 32-bit machine, the number of digits of an integer is 32 bits and the value range is-2 ** 31 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647
In a 64-bit system, the number of digits of an integer is 64-bit, and the value range is-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807   Once the number of digits is exceeded, it is automatically converted to a long integer (Versions later than python2.2)
Long (long integer)
Theoretically, the length is infinite, but the data is stored in the memory after all, so the actual length limit depends on the memory size.
Float (float type)
Digits with decimal places are called floating point types.
Complex (plural) is the plural in mathematics. Its definition is the same as that in Mathematics (z = a + bi, where a and B are real numbers, and I are virtual number units, for example, 1 + 5j)

I. Integer

The numbers in python include integer int, long, float, and complex. We will analyze them one by one. 1. integer Help on class int in module _ builtin __: class int (object) | int (x = 0)-> int or long | int (x, base = 10) -> int or long | Convert a number or string to an integer, or return 0 if no arguments | are given. if x is floating point, the conversion truncates towards zero. | If x is outside the integer range, the function returns a long instead. | If x is not a number or if base is given, then x mus T be a string or | Unicode object representing an integer literal in the given base. the | literal can be preceded by '+' or '-' and be surrounded by whitespace. | The base defaults to 10. valid bases are 0 and 2-36. base 0 means to | interpret the base from the string as an integer literal. | >>> int ('0b100', base = 0) | 4 | Methods defined here: | _ abs __(...) | x. _ abs _ () <=> abs (x) | _ Dd __(...) | x. _ add _ (y) <=> x + y | _ and __(...) | x. _ and _ (y) <=> x & y | _ cmp __(...) | x. _ cmp _ (y) <=> cmp (x, y) | _ coerce __(...) | x. _ coerce _ (y) <=> coerce (x, y) | _ div __(...) | x. _ div _ (y) <=> x/y | _ divmod __(...) | x. _ divmod _ (y) <=> divmod (x, y) | _ float __(...) | x. _ float _ () <=> float (x) | _ floordiv __(...) | x. _ floordiv _ (y) <=> x // y | _ format __(...) | _ _ Getattribute __(...) | x. _ getattribute _ ('name') <=> x. name | _ getnewargs __(...) | _ hash __(...) | x. _ hash _ () <=> hash (x) | _ hex __(...) | x. _ hex _ () <=> hex (x) | _ index __(...) ''' is used for slicing, but the parameter is not accepted. The return value is the entire number. Therefore, slicing does not make sense for digits ''' | x [y: z] <=> x [y. _ index _ (): z. _ index _ ()] | _ int __(...) | x. _ int _ () <=> int (x) | _ invert __(...) | x. _ invert _ () <=> ~ X | _ long __(...) | x. _ long _ () <=> long (x) | _ lshift __(...) | x. _ lshift _ (y) <==> x <y | _ mod __(...) | x. _ mod _ (y) <=> x % y | _ mul __(...) | x. _ mul _ (y) <=> x * y | _ neg __(...) | x. _ neg _ () <=>-x | _ nonzero __(...) | x. _ nonzero _ () <=> x! = 0 | _ oct __(...) | x. _ oct _ () <=> oct (x) | _ or __(...) | x. _ or _ (y) <=> x | y | _ pos __(...) | x. _ pos _ () <=> + x | _ pow __(...) | x. _ pow _ (y [, z]) <=> pow (x, y [, z]) | _ radd __(...) | x. _ radd _ (y) <=> y + x | _ rand __(...) | x. _ rand _ (y) <==> y & x | _ rdiv __(...) | x. _ rdiv _ (y) <=> y/x | _ rdivmod __(...) | x. _ rdivmod _ (y) <=> divmod (y, x) | _ repr __(...) | x. _ repr _ () <=> repr (x) | _ rfloordiv __(...) | x. _ rfloordiv _ (y) <=> y // x | _ rlshift __(...) | x. _ rlshift _ (y) <==> y <x | _ rmod __(...) | x. _ rmod _ (y) <=> y % x | _ rmul __(...) | x. _ rmul _ (y) <=> y * x | _ ror __(...) | x. _ ror _ (y) <=> y | x | _ rpow __(...) | y. _ rpow _ (x [, z]) <=> pow (x, y [, z]) | _ rrshift __(...) | x. _ rrshift _ (y) <==> y> x | _ rshift __(...) | x. _ rshift _ (y) <==> x> y | _ rsub __(...) | x. _ rsub _ (y) <=> y-x | _ rtruediv __(...) | x. _ rtruediv _ (y) <=> y/x | _ rxor __(...) | x. _ rxor _ (y) <=> y ^ x | _ str __(...) | x. _ str _ () <=> str (x) | _ sub __(...) | x. _ sub _ (y) <=> x-y | _ truediv __(...) | x. _ truediv _ (y) <=> x/y | _ trunc __(...) | Truncating an Integral returns itself. | _ xor __(...) | x. _ xor _ (y) <=> x ^ y | bit_length (...) | int. bit_length ()-> int | ''' the Number to be modified is returned. The Number must be in binary format. ''' | Number of bits necessary to represent self in binary. | >>> bin (37) # obtain its binary representation | '0b100101 '# 0b indicates that it is a binary representation, the last 100101 is the real binary code. | >>> (37 ). bit_length () | 6 # the binary representation of 37 is 100101, a total of 6 digits, so 6 is returned | conjugate (...) | Returns self, the complex conjugate of any int. | ---------------------------------------------------------------------- | Data descriptors defined here: | denominator | the denominator of a rational number in lowest terms | imag | the imaginary part of a complex number | numerator | the numerator of a rational number in lowest terms | real | the real part of a complex number | ---------------------------------------------------------------------- | Data and other attributes defined here: | _ new _ = <built-in method _ new _ of type object> | T. _ new _ (S ,...) -> a new object with type S, a subtype of TInt

 

We can see that there are three built-in methods:

1. Common method (the description has been commented out in the Code)

2. It is equivalent to some built-in functions (the built-in functions will be introduced later)

3. methods related to operators

Here we will introduce the python operators in detail:

1. Arithmetic Operators

  Assume that the variable a = 10 and the variable B = 20:

 

2. Comparison Operators

  Assume that the variable a = 10 and the variable B = 20:

 

3. Value assignment operator

  Assume that the variable a = 10 and the variable B = 20

4. bitwise operators

Bitwise operators regard numbers as binary values for calculation. The bitwise Algorithm in Python is as follows:

  Variables a = 60, B = 13 in the following table

5. logical operators

  Assume that the variables a = 10, B = 20

 

6. member operators

In addition to the preceding operators, Python also supports member operators. The test instance contains a series of members, including strings, lists, and tuples.

 

7. Identity Operators

Identity operators are used to compare the storage units of two objects.

 

Here we will talk about the memory pool (buffer pool) in python. python uses the memory pool to manage small integer numbers and small strings.
What does it mean?

When we perform the following assignment operations

a = 123b = 123

 

Theoretically, we need to create two values in the memory respectively and assign them to the variables. However, this is a waste of time. It is clearly the same number, but it takes up two memories.

Therefore, to save memory, python introduces a memory pool ~ 257, excluding 257) If you want to create multiple times, you can only create one time, and all the subsequent references will point to the same place. In this case, the identity operator will appear:

The string is separated by 256 ascll codes.

If you are interested, refer to: stamp here

 

8. Operator priority

In addition, we can use parentheses () like mathematics to specify that an operation is performed first.

For more information, click here.

  It's easy to copy and paste all the way.

 

Ii. Long Integer

It is basically the same as an integer, so we will not repeat it here. You can use the help () function.
  Don't cry for help

 

 

Iii. Floating Point

I will not list them all here. I just want to talk about different ones.

 |  __setformat__(...) |      float.__setformat__(typestr, fmt) -> None |       |      You probably don't want to use this function.  It exists mainly to be |      used in Python's test suite. |       |      typestr must be 'double' or 'float'.  fmt must be one of 'unknown', |      'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be |      one of the latter two if it appears to match the underlying C reality. |       |      Override the automatic determination of C-level floating point type. |      This affects how floats are converted to and from binary strings.

 

An internal method used for internal testing. The official saying is that You probably don't want to use this function (You may not want to use this function), but we actually cannot use it, I don't know how to use it. Generally, I can ignore it.

 

 |  __trunc__(...) |      Return the Integral closest to x between 0 and x.

Returns the nearest x point from 0 and x, which seems to be related to the point operation and has never been used.

 

 

 |  as_integer_ratio(...) |      float.as_integer_ratio() -> (int, int) |       |      Return a pair of integers, whose ratio is exactly equal to the original |      float and with a positive denominator. |      Raise OverflowError on infinities and a ValueError on NaNs. |       |      >>> (10.0).as_integer_ratio() |      (10, 1) |      >>> (0.0).as_integer_ratio() |      (0, 1) |      >>> (-.25).as_integer_ratio() |      (-1, 4)

Returns a ancestor composed of two numbers, and the division of the two numbers is equal to the original floating point number. In fact, it returns the simplest score, with the numerator in front and the denominator in the back.

 

 

 |  conjugate(...) |      Return self, the complex conjugate of any float.

Returns the complex number of the condensed elements.

 

 |  fromhex(...) |      float.fromhex(string) -> float |       |      Create a floating-point number from a hexadecimal string. |      >>> float.fromhex('0x1.ffffp10') |      2047.984375 |      >>> float.fromhex('-0x1p-1074') |      -4.9406564584124654e-324

Use a hexadecimal string to create a floating point number.

 

 

 |  hex(...) |      float.hex() -> string |       |      Return a hexadecimal representation of a floating-point number. |      >>> (-0.1).hex() |      '-0x1.999999999999ap-4' |      >>> 3.14159.hex() |      '0x1.921f9f01b866ep+1'

Returns the hexadecimal string of a floating point number.

 

 

 |  is_integer(...) |      Return True if the float is an integer.

Determines whether a floating point number is an integer.

Actually, it depends on whether the decimal point is 0.

 

 

Iv. Plural

  These operations are related to built-in functions.

 |  conjugate(...) |      complex.conjugate() -> complex |       |      Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.

Returns the total number of original complex numbers.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.