Python学习(九)——一个小程序的学习

简介

本文主要通过最近学习的一个python小程序,来逐句学习python的语法规则。

Python代码

#!/use/bin/python

SUFFIXES={1000:['KB','MB','GB','TB','PB','EB','ZB','YB'],1024:['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']}

def approximate_size(size,a_kilobyte_is_1024_bytes=True):
    '''Convert a size to human-readable form

    arguments:
        size
        a_kilobyte_is_1024_byte
    
    '''
    if size < 0:
        raise ValueError('number must be non-negative!')
    multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
    for suffix in SUFFIXES[multiple]:
        size /= multiple
        if size < multiple:
            return '{0:.1f} {1}'.format(size,suffix)
    raise ValueError('number too large')

if __name__ == '__main__':
    print(approximate_size(1000000000000,False))
    print(approximate_size(1000000000000))



你可能感兴趣的:(Python学习(九)——一个小程序的学习)