Python学习---读过《深入Python3》有感2

不用多说,附上代码:

__author__ = 'minggxu9'
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 file size to human-readable form.

    Keyword arguments:
    size -- file size in bytes
    a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
                                if False, use multiples of 1000

    Returns: string

    '''
    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')


query = 'user=pilgrim&database=master&password=PapayaWhip'
a_list = query.split('&')
print(a_list)
a_list_of_lists = [v.split('=') for v in a_list]
print(a_list_of_lists)

a_dict = dict(a_list_of_lists)
print(a_dict)

s = '''Finished files are the re-
 sult of years of scientif-
 ic study combined with the
experience of years.'''
print(s)
print(len(s))
print(s.splitlines())
print(s.lower())
print(s.lower().count('f')   )

#格式化字符串,格式化说明符
print('{0:.1f} {1}'.format(698.24, 'GB'))


#定义函数
def goOther(a_kilobyte_is_1024_bytes=True):
    if  a_kilobyte_is_1024_bytes:
     return '{0:.1f} {1}'.format(4444.14, 'gb')

print( goOther())

si_suffixes=['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
print('1000{0[0]} = 1{0[1]}'.format(si_suffixes))
'''
    这一句看上去有些复杂,其实不是这样的。{0}代表传递给format()方法的第一个参数,
    即si_suffixes。注意si_suffixes是一个列表。所以{0[0]}指代si_suffixes的第一个元素,即'KB'。
    同时,{0[1]}指代该列表的第二个元素,即:'MB'。大括号以外的内容 — 包括1000,等号,
    还有空格等 — 则按原样输出。语句最后返回字符串为'1000KB = 1MB'。
'''

username = 'mark'
password = 'PapayaWhip' 
print("{0}'s password is {1}".format(username, password))

#字符串的分片
a_string = 'My alphabet starts where your alphabet ends.'
print(a_string[3:11])

print( a_string[3:-3])
print( a_string[0:2] )
print(a_string[:18] )
print(a_string[18:])


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深入 Python 3 的内容涵盖了 Python 3 及其与 Python 2 的区别。Python 3 提供了一个脚本叫做 2to3。学习它。喜欢它。使用它。用 2to3 移植代码到 Python 3 是一个有关 2to3 工具能够自动整理的所有东西的参考手册。很多这些东西都是语法的变更,因此了解 Python 3 里面许多的语法变更是一个好的起点。(print 现在是一个函数,`x` 不能使用,等等。) 在 HTTP Web 服务这章,httplib2 模块通过 HTTP 获取头信息和数据。HTTP头信息返回的是字符串,而 HTTP 正文则返回的是字节。 在序列化 Python 对象这章,你将了解到为什么 Python 3 里面的 pickle 模块定义了一个和 Python 2 向后不兼容的新的数据类型。(提示:这就是因为字节和字符串的原因。) 同样 JSON也根本不支持字节类型。我将向你展示如何解决这个问题。 在案例分析:移植 chardet 到 Python 3这章,到处都是一大堆一大堆关于字节和字符串的东西。 即使你不关心 Unicode (但实际上你会的),你也会想阅一下 Python 3 里面的字符串格式,这和 Python 2 里面的完全不一样。 Python 的专家们聪明的把 ElementTree 变成了标准库的一部分 ,然后现在它构成了新的 XML 章节的基础。解析 XML 的那些老的方式仍然可用,但是你应该避免使用它们,因为他们很糟糕! 除此之外,还有个关于 Python 的新东西 — 不是语言上的,而是社区中的 — 像 Python 包装索引(PyPI) 的出现。Python 提供了实 用工具类用来将你的代码打包成标准格式,并分发那些包到PyPI 中。阅 打包 Python 库了解详细信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值