Python built-in type (4) -- value, python built-in

Python has three numeric types: integer, floating point numbers, and plural ). In addition, Boolean is the subtype of an integer.

Value Type description

An integer is represented by 1-N numeric characters. The integer type name is`int`

All integers are of the type.`int`

The floating point is composed of an integer and a decimal part.`.`

Connection. The floating point type name is`float`

All floating point numbers are of the type.`float`

The complex number consists of the real part and the virtual part, where the virtual part follows the letter`j`

, The number of plural type names`complex`

, All plural values are types`complex`

.

Value declaration

In python, there are two ways to declare a value: literal declaration and numerical constructor initialization.

Literal

Similar to other languages, python supports literal value declaration.

For integers, a string of numbers indicates a positive integer, and a negative number is added before the number.`-`

Number,

`# Positive number >>>> 19 19 # negative number >>>>-7-7 #0 >>> 00`

The literal value of the floating point number. The negative value is also the one added above.`-`

Number,

`# Positive floating point number> 19.119.1 # negative floating point number>-7.2-7.2 #0.0 >>> 0.00.0`

The literal expression of the plural. The imaginary part uses letters.`j`

(Or`J`

) Indicates

`>>> 1 + 2j1 + 2j`

Integers are represented in decimal notation by default, but the literal value can also be directly declared in binary notation, octal notation, or hexadecimal notation. They are added to integers respectively.`0b`

,`0o`

,`0x`

.

`#2 hexadecimal >>> a = 0b10 >>> a2 #8 hexadecimal >>> B = 0o10 >>> b8 #16 hexadecimal >>> c = 0x10 >>> c16`

Constructor Declaration

`int`

A function is a python built-in function and also an integer type.`int`

It initializes the input parameters and converts them to an integer. The initialization results vary according to the input parameters. For details, see Python built-in function 33 -- int.

`#1. if no parameter is input, the result 0 >>> int () 0 #2 is displayed. when an integer is input, the return value is itself. When a floating point number is input, the value is rounded down to >>>> int (3) 3 >>> int (3.6) 3 #3. when a string is input, it is converted to 10 by default. The string can contain positive and negative symbols. >>> Int ('+ 36') 36 >>> int ('-36 ')-36 #4. Specify the conversion hexadecimal format when passing in the string. >>> Int ('01', 2) 1 >>> int ('02 ', 3) 2 >>> int ('07', 8) 7 >>> int ('0f', 16) 15`

`float`

A function is a python built-in function and also a floating point type.`float`

It initializes the input parameters and converts them to a floating point number. The initialization results vary according to the input parameters. For details, see Python built-in function 22 -- float.

`#1. if no parameter is input, the result is 0.0 >>> float () 0.0 #2. input an integer or floating point number >>>> float (3) 3.0 >>> float (3.6) 3.6 #3. when a string is input, it can contain positive and negative characters. >>> Float ('+ 000000') 3.6 >>> float ('-000000')-3.6 #4. several special strings> float ('infinity ') # Infinity inf> float ('inf') # inFinIty inf> float ('infinity ') # case insensitive inf >>> float ('+ inFinIty') # positive inFinIty inf >>> float ('-inFinIty ') # negative infinity-inf> float ('nan ') # No nan Value`

`complex`

A function is a python built-in function and also a complex number.`complex`

The constructor is used to initialize the input parameters and convert them into a plural number. The initialization results vary according to the input parameters. For details, see Python built-in function 13 -- complex.

`#1. if no parameter is input, the result is 0j >>> complex () 0j #2. two parameters are passed in, indicating the real part and the virtual part >>> complex (3, 4) (3 + 4j) #3. when passing in a string >>> complex ('3 + 4j ') 3.6 >>> complex ('-100') (3 + 4j)`

Supported operation Operations common operation operations

Shaping supports the following operations, sorted in ascending order of priority

`x + y`

Add
`x - y`

Subtraction
`x * y`

Multiply
`x / y`

Division
`x // y`

Floor Division
`x % y`

Returns the remainder.
`-x`

Take negative
`+x`

Positive value, value unchanged
`abs(x)`

Take absolute value
`int(x)`

Convert to integer
`float(x)`

Convert to floating point
`complex(re,im)`

Convert to plural
`c.conjugate()`

Take the combination of the plural
`divmod(x， y)`

Returns the result of the floor division and the result of the remainder.
`pow(x,y)`

Y Power of x
`x ** y`

Y Power of x

Where`x // y`

Only floating-point numbers and integers are supported, and plural numbers are not supported. The division result is rounded down.

`>>> 1 // 20 >>>-1 // 2-1 >>>> 1 // (-2)-1 >>>> (-1) // (-2) 0 # One of the floating point results is also a floating point >>>> 5 // 31 >>> 5 // 3.01.0`

Rounding a floating point

Python provides the built-in function float for rounding floating point numbers. Note that if float does not input 1.1 parameters, it is truncated to an integer. It is different from entering 0 to retain 0 decimal places.

`>>> Round (3.456, 2) 3.46 >>> round (3.456, 1) 3.5 >>> round (3.456, 0) 3.0 >>> round (3.456) # If no parameter is input, the result is an integer of 3.`

Several truncation methods for converting a floating point to an integer

In`math`

In the module, there are several other methods to truncate floating point numbers into decimal places. They are`trunc`

Truncation,`floor`

Round down,`ceil`

Rounded up. As mentioned above`round`

Function. If no 2nd parameters are input, the function can also be rounded up.

`# Import mathimport math >>>> a = 3.556 >>> B =-3.556 # directly truncation >>> math. trunc (a) 3> math. trunc (B)-3 # Round down> math. floor (a) 3 >>> math. floor (B)-4 # rounded up> math. ceil (a) 4> math. ceil (B)-3 # rounding to integer> round (a) 4 >>> round (B)-4`

Floating Point

`is_integer()`

Method

`is_integer()`

Is an instance method of floating number,

If the floating point instance is an integer, return`True`

Otherwise, return`False`

.

`>>> f1 = 3.0>>> f1.is_integer()True>>> f2 = 3.14>>> f2.is_integer()False`