7.3 numeric types and built-in method

About digital type, introduced before, briefly said that the figures include int type integer and floating-point-two float.

int int

Uses: are integers, so much for numbers, number, grade, age, etc.

Defined method:

18 is = Age   # is equivalent to int = Age (18 is) 
int ( ' 2,324,324 ' )   # If the string is a pure digital, can be defined by an integer 
Print (int ( ' 2332 ' ' 123 ' ))   # result 2,332,123 

Print (int [ ' 2332 ' , ' 123 ' ])   # error # listing can not be transferred 
Print (int ( ' 2332 ' , ' 123 ' ))   # error # which can only be a string of integer 
int ( ' 1.1 ' )   #Error # int turn only pure digital string of decimal point will not 
int ( ' djskljlrljo ' )   # Error # only integer string can be defined in

You can store a stored value or a plurality of values

Print (int ( ' 2332 ' ' 123 ' ' 28918 ' ))   # result 233 212 328 918 

# a value does not know the appropriate count examples, but only save int

Ordered or disordered (order: whenever there is an index of the data is ordered)

Because you can only store a value, so no one said order-disorder

Variable or immutable

1, the variable: when the value becomes, id address does not change.  # That is modified on the basis of the original value 
                        # variable == not the hash

 
2, immutable: when the value becomes, id address changes.  # I.e. reapply a new value into the space 
                        # immutable hash may ==   

For int, it is immutable

number = 998
print(type(number),id(number))
number += 1
print(type(number),id(number))

>>>>>>>>
#<class 'int'> 4488584848
#<class 'int'> 4489375216

float float

Use: Body weight, interest, salary, etc.

Defined method:

salary = 2.43  # 等价于 salary = float(2.43)
print(type(salary))

>>>>>>>>
<class 'float'>
a float = RES ( ' 1.22 ' )   # a float string can contain only floating-point, floating-point number is converted into 

Money = a float (135 )
 Print (Money, type (Money))   # can convert an integer to floating point type ( 0 and decimal point) 
# can not convert floating point to integer
>>>>>>>> 135.0 < class ' a float ' >

Hexadecimal conversion

Turn into other binary decimal

# 10 hexadecimal turn Binary 
 Print (bin (12 is))   # 0b1100 0B indicates that the following number is a binary number 

# 10 decimal turn octal 
 Print (OCT (12 is))   # 0o14 0o indicates that the following number is octal   
               # 14 => 1 * (8 * 1) + 4 * (8 * 0)) 
# 10 hex 16 hex transfected 
 Print (hex (12 is))   # 0xc 0x indicates that the following number is a hexadecimal number

Other binary to decimal

# Binary converted into decimal 
Print (int ( ' 101 ' , 2 )) 
 # 101 =>. 1 * (2 ** 2) + 0 * (2 **. 1) +. 1 * (2 ** 0) =. 4 5 + 0 + 1 = 

# octal converted to decimal 
Print (int ( ' 77 ' ,. 8 )) 
 # 77 =>. 7 * (1. 8 **) +. 7 * (0. 8 **) =. 7 = + 56 is 63 

# hexadecimal is converted into binary 10 
Print (int ( ' 21 is ' , 16 ))
 # 21 is => 2 * (16 **. 1) +. 1 * (16 ** 0) =. 1 + 32 = 33 is

 

Guess you like

Origin www.cnblogs.com/PowerTips/p/11128296.html