Variables and formatted output

Python-based memory management scheme value. Execution of an assignment statement is: First calculate the value of the expression on the right of the equal sign out, and then look for a location to put the value in memory, and finally create a variable to point to this memory address. Python is not directly stored in the variable value, but the value stored in the memory address of or reference to, and this is the type of a variable can change at any time reasons.

Variable is used to reference the value in the program may change.

1, variable

In [2]: name1='apple'

In [3]: name2='apple'

In [4]: id(name1)
Out[4]: 1438115923984

In [5]: id(name2)
OUT [ 5]: 1438115923984 NAME1 and name2 memory address is the same as that of the Python memory optimization
In [6]: name1="i love you"

In [7]: name2="i love you"

In [8]: id(name1)
Out[8]: 1438129390000

In [9]: id(name2)
OUT [ . 9]: 1438129393456 name1 and name2 case of no memory address is different long strings of memory optimization mechanism
In [21]: name1=1000

In [22]: name2=1000

In [23]: id(name1)
Out[23]: 1438129513968

In [24]: id(name2)
OUT [ 24]: 1,438,129,513,616 numbers [-5,257 between) have a memory optimization (only one out of address space)

In [25]: name1=name2=1000

In [26]: id(name1)
Out[26]: 1438129512784

In [27]: id(name2)
OUT [ 27]: when 1438129512784 continuous assignment is the equivalent of two pointers to the same memory address, I think a key hung two rings

2, an output format or a character string format output form

input () is a function of the output string format default

def format(*args, **kwargs): # real signature unknown
"""
Return value.__format__(format_spec)

format_spec defaults to the empty string
"""
pass


format (item, format-specifier) ​​item a number or string, and the format specifier (format-specifier) ​​format specified entry item. This function returns a string.

E.g. 10.2f 10 wherein the width of the finger domain      .2 refers to the overall accuracy of the conversion code means f is a format character.

In [32]: format(12.1512345,'10.2f')  
OUT [ 32]: '      12.15 '                          decimal point is a default right alignment, rounded

In [33]: format(23.2456453,'10.2f')
Out[33]: '     23.25'

The In [ 34 is]: the format (12.3, " 10.2f " ) up enough bits 0
Out[34]: '     12.30'

The In [ 35]: the format (23.23456, ' .3f ' ) is not specified width is automatically set based on the content of the specified precision for .n
Out[35]: '23.235'

In [36]: format(12345678.901,'10.1f')
Out[36]: '12345678.9'

The In [ 37 []: the format (12,345,678.901, ' 10.2f ' ) exceeds the width, ensure that the content priority, increasing the width
Out[37]: '12345678.90'
The In [39]: the format (585.58558, ' 10.3e ' ) scientific notation
Out[39]: ' 5.856e+02'

The In [ 40]: the format (0.0585858, ' 10.2% ' ) percentage
Out[40]: '     5.86%'

In [41]: format(0.5989898,'.3%')
Out[41]: '59.899%'

The In [ 42 is]: the format (0.0585858, ' <10.2% ' ) < left justified format prior to comparing the position of the character
Out[42]: '5.86%     '
The In [43 is]: the format (12 is, ' D ' ) a decimal integer format
Out[43]: '12'

The In [ 44 is]: the format (12 is, ' X ' ) Hex
Out[44]: 'c'

The In [ 45]: the format (12 is, ' O ' ) octal
Out[45]: '14'

The In [ 47]: the format (12 is, " B " ) Binary
Out[47]: '1100'
In [48]: format('we will regain the lost after we lost it','<45s')
Out[48]: 'we will regain the lost after we lost it     '             指定45为宽度
           
The In [ 49]: the format ( ' WE WE After Will Regain Lost Lost The IT ' , ' 45s ' ) right justified numeric default, and the default character string is left justified
Out[49]: 'we will regain the lost after we lost it     '

In [50]: format('we will regain the lost after we lost it','>45s')
Out[50]: '     we will regain the lost after we lost it'

%

In [53]: from math import pi

The In [ 54 is]: ' PI I% ' % PI I and d are decimal
Out[54]: 'pi3'

In [55]: '%.5f'%pi
Out[55]: '3.14159'


In [58]: '%10.3f'%pi
Out[58]: '     3.142'

 

Guess you like

Origin www.cnblogs.com/bchy/p/11688915.html