Python学习---读过《深入Python3》有感2
2011-11-16 23:03
246 查看
不用多说,附上代码:
__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:])
相关文章推荐
- Python学习---读过《深入Python3》有感
- 5.python的路径相关操作——《深入python3》学习
- 5.python的路径相关操作——《深入python3》学习
- python学习笔记-(11)python中的文件相关
- 【Python学习日记】面向过程 和 面向对象 编程
- 用Python 学习数据结构与算法 六、最大子序列
- python 学习
- Python学习笔记(二)
- python学习第一课
- numpy.zeros 使用方法 python学习笔记1
- 11、TX2学习笔记--OpenCV3.2+OpenCV_contrib3.2+CUDA8.0+python3.5安装
- python学习:异常处理、with用法以及utf8中文显示
- Python+NLTK自然语言处理学习(一…
- python学习笔记-(15)python中的循环对象
- python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置
- python学习-类理解
- Python学习 第二天
- Python学习第十三天——装饰器和继承
- Python学习入门基础教程(learning Python)--2.3 Python自定义函数传参函数设计
- python 学习笔记5------序列