Python Number, pythonnumber
1. Python number
The Python Number data type is used to store numeric values.
The data type cannot be changed, which means that if the value of the Number data type is changed, the memory space will be re-allocated.
Create a number data value, change the data value, and check that the memory address has changed:
>>> num = 123>>> id(num)8743872>>> num = 456>>> id(num)13991590095640
Use the del statement to delete number objects. You can delete multiple objects separated by commas:
>>> num = 123>>> num1 =888>>> del num,num1
2. python supports four different data types:
- INTEGER (int)-it is usually called an integer or integer and is a positive or negative integer.
- Long integer-an integer of an infinite size. The integer is represented by L in upper or lower case.
- Floating point real values-floating points are composed of integer and decimal parts.
- The complex number (complex numbers)-the complex number consists of the real number and the virtual number. It can be expressed by a + bj or complex (a, B). The a and B parts of the complex number are float.
Value range of long integer: In python2.7, the value range of long integer is-2 **-1 to 2 ** 63 power python3 without the long type, use int to indicate a long integer In [1]: 2 ** 63-1Out [1]: 9223372042554775807lin [2]: lo1 = 9223372042554775807in [3]: type (lo1) Out [3]: intIn [4]: lo2 = 9223372042554775808in [5]: type (lo2) Out [5]: longIn [6]: log8 =-2 ** 62In [7]: type (log8) Out [8]: intIn [9]: log8 =-2 ** 63-1In [10]: type (log8) Out [11]: long create a plural: >>> complex1 = 1.2 + 3.4j >>> type (complex1) <class 'compute' >>>> complex2 = complex (0.3, 3.2) >>> print (complex1, complex2) (1.2 + 3.4j) (0.3 + 3.2j)
3. python number type conversion
Built-in functions can be used to convert data types. These functions return a new object, indicating the converted value.
>>> Nu1 = 89 >>> nu2 = float (nu1) # converted floating point type >>> type (nu2) <class 'float' >>>> nu3 = complex (nu2) # To the plural >>> type (nu3) <class 'compute' >>> print (nu3) (89 + 0j) >>> nu4 = int (nu2) # convert integer >>> type (nu4) <class 'int' >>>> nu5 = str (nu4) # convert character >>> type (nu5) <class 'str'>
Str (x) converts object x to string repr (x) convert object x to expression string eval (str) to calculate a valid Python expression in the string, and returns an object tuple (s) to convert the sequence s into a tuples list (s) to convert the sequence s into a list chr (x) convert an integer to a character unichr (x) and convert an integer to a Unicode Character ord (x). convert a character to its integer hex (x) converts an integer to a hexadecimal string oct (x) and an integer to an octal string.
4. python digital built-in functions, digital processing module math
>>> Import math # digital processing module >>> help (math. ceil) # view help> dir (math) # print all methods ['_ doc _', '_ file __', '_ loader _', '_ name _', '_ package _', 'acs', 'acoss', 'asin', 'asinh ', 'ata', 'atan2', 'atanh', 'ceil ', 'copysign', 'cos', 'cosh', 'degrees', 'E', 'erf ', 'erfc', 'exp', 'expm1', 'fabs ', 'factorial', 'floor ', 'fmod', 'frexp', 'fsum ', 'Gamma ', 'hybrid', 'isfinite ', 'isinf', 'isnan ', 'ldexp', 'lgamma ', 'log', 'log10', 'log1p', 'log2 ', 'modf', 'Pi ', 'pow', 'radians', 'sin', 'sinh', 'sqrt ', 'Tan', 'tanh ', 'trunc '] >>> nu1 = 12.34 >>> math. ceil (nu1) # Get an integer 13 >>> math. exp (nu1) # returns the nu1 power of e. e is the defined constant 228661.9520568098 >>> math. fabs (nu1) # returns the absolute value 12.34 >>> math. floor (nu1) # returns the rounded down integer of a number 12 >>> math. modf (nu1) # returns the fractional part and integer part (0.33999999999999986, 12.0) >>> math. sqrt (nu1) # returns the square root of 3.5128336140500593 >>> math. e # The constant e2.718281828459045 defined by the module> math. pi # module-defined constant pi3.141592653589793 built-in function: >>> abs (11.2) # returns absolute value 11.2 >>>> max () # maximum value 24 >>> min) # minimum value 12 >>> pow (2, 4) #2 ** 4 power 16 >>> round (1.245, 3) # returns a rounding value, 3: the number of decimal places defined as 1.245 >>> round (1.245) # The default value is 01.
5. python random Number Module random
>>> Import random # import module >>> random. random () # obtain a random number between 0 and 1, 0.1781419039493949> random. random () 0.914421842727102 >>> random. uniform (19.774883012515218) # generate a floating point number between 10 and 20 >>> random. uniform (11.654111952867027) >>> random. randint () # generate an integer in the specified range 18 >>> random. randint (10, 20) 11 >>> random. randrange (1,100, 8) # obtain a random number from the specified range in ascending order of the specified base number 33> random. randrange (1,100, 8) 17 >>> random. randrange (1,100, 8) 33 >>> random. choice ([1, 2, 3, 4, 5]) # obtain random elements from sequence elements, which can only be ordered type 2> random. choice ([1, 2, 3, 4, 5]) 1 >>> random. choice ([1, 2, 3, 4, 5]) 2 >>> random. choice ('abc') 'd> random. choice ('abc') 'A'> random. choice ('abcd') 'C'> a = [1, 2, 3, 4, 5] # disrupt a list element> random. shuffle (a) >>> a [1, 2, 3, 5, 4] >>> random. shuffle (a) >>> a [2, 5, 4, 3, 1] >>> random. sample (a, 2) # randomly obtain N elements from the specified sequence and generate a new object [5, 2] >>> random. sample (a, 2) [5, 4] >>> random. sample (a, 3) [3, 4, 1] >>> random. sample (a, 5) [5, 1, 4, 2, 3]
2017-09-2412: 27: 23