Python's numeric type

Source: Internet
Author: User

Problem:
1. What are the Python values?
2. What is the accuracy of each type?
3. How is the conversion of the type implemented?
5. How do I use these data types? What to pay attention to?
6. What is an immutable type?

Numeric Type:
Integral type: (immutable type)
Standard integer: Range of values [-sys.maxint-1, Sys.maxint]
Long integer: Can represent an infinite number of integers whose value is only relevant to the (virtual) memory size supported by your machine
Values outside the standard integer range will be converted to long integers.
For example:
1. Define the standard integral type
>>> Import Sys
>>> A = Sys.maxint
>>> A
2147483647
2. Add 1 to the value of a
>>> a=sys.maxint+1
>>> A
2147483648L

Boolean type: (Immutable type)
Value range: True and False distribution of 1 and 0 of the integer type
Case-sensitive for bool values
For example:
>>>a=true
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' true ' is not defined

Floating point type:
Double in C, 8 bytes (64 bits) for each floating-point value
Precision:
>>> Sys.float_info
Sys.float_info (max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.
2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsi
Lon=2.220446049250313e-16, radix=2, Rounds=1)

Plural:
Some of the concepts in Python about complex numbers:
1. Imaginary numbers cannot exist alone, and they are always combined with a real part with a value of 0.0 to form a complex number
2. Complex numbers consist of real and imaginary parts
3. Syntax for plural: real+imagej
4. Real and imaginary parts are floating-point numbers
5. The imaginary part must have a suffix J or j

Type conversions:
When we are doing an operation, if the two operands are of the same data type, then there is no need for the type conversion.
Python checks whether one operand can be converted to an operand of another type only if the two types are inconsistent.

Automatic type conversion:
No coding is required to resolve conversion issues.
Basic rules:
To convert an integer to a floating point type, simply add ". 0" after the integer type.
To convert a non-complex number to a complex number, simply add "0j" after the non-complex number

Coercion type conversion: coerce ()
Coerce () Python built-in functions for enforcing type conversions
Example:
1. Integer and floating-point conversions
>>> Coerce (1,1.1)
(1.0, 1.1)
2. Long integer and floating-point conversions
>>> Coerce (123l,0.1)
(123.0, 0.1)
3. Conversion of integers to long integers
>>> Coerce (1,2L)
(1L, 2L)
4. Conversion of integers to complex numbers
>>> Coerce (1,1+1J)
((1+0J), (1+1j))

650) this.width=650; "Title=" Drawing 1 "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" drawing 1 "src=" http://img1.51cto.com/attachment/201408/10/8926377_ 1407659276y8fm.jpg "" 640 "height=" 414 "/>


Immutable types:
Create a variable and assign a value this procedure allocates memory for the variable name and the variable value, and once the value of the variable is
It cannot be changed after it is created.
For example:
1. Create variable A, variable B, and assign a value of 1
>>> a=1;b=1
2. View the reference address of the value of a B variable
>>> ID (a); ID (b)
5153592
5153592

Explanation of the ID function:
Return the identity of an object. This was guaranteed to be unique among
Simultaneously existing objects. (Hint:it ' s The object's memory address.)











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.