
Python练习
文章平均质量分 89
Python
MingoHuang
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
python核心编程2 第5章 练习
5-2 运算符(a) 写一个函数,计算并返回两个数的乘积(b) 写一段代码调用这个函数,并显示它的结果def product(x, y): return x * yif __name__ == '__main__': x = int(input('输入数字X: ')) y = int(input('输入数字Y: ')) print(multiply(x...原创 2019-02-20 16:54:05 · 312 阅读 · 0 评论 -
python核心编程2 第15章 练习
15-1.识别下列字符串 :“bat ”、“bit ”、“but ”、“hat ”、“hit” 或 “hut ”import refrom random import choicestrtuple = ('bat', 'bit', 'but', 'hat', 'hit', 'hut')patt = '[bh][aiu]t'm = re.search(patt, choice(st...原创 2019-03-15 16:27:17 · 410 阅读 · 0 评论 -
python核心编程2 第13章 练习
13-3.对类进行定制。写一个类,用来将浮点型值转换为金额。class MoneyFmt(object): def __init__(self, value=0.0, flag='-'): self.value = float(value) self.flag = flag def dollarize(self): ""...原创 2019-03-12 16:25:13 · 318 阅读 · 0 评论 -
python核心编程2 第12章 练习
12–5. 使用 __import__().(a) 使用 __import__ 把一个模块导入到你的名称空间。 你最后使用了什么样的语法?(b) 和上边相同, 使用 __import__() 从指定模块导入特定的名字。module = __import__('sys', fromlist=['path'])path = module.pathprint(module.modules...原创 2019-03-04 09:37:55 · 141 阅读 · 0 评论 -
python核心编程2 第11章 练习
11-2 函数。结合你对练习5-2的解,以便你创建一个带一对相同数字并同时返回它们之和以及产物的结合函数。multiply = lambda x, y: x * yif __name__ == '__main__': x = int(input('x: ')) y = int(input('y: ')) print(multiply(x, y))11-3 ...原创 2019-03-01 11:12:24 · 336 阅读 · 0 评论 -
python核心编程2 第10章 练习
10-6.改进的open()。为内建的open()函数创建一个封装。使得成功打开文件后,返回文件句柄:若打开失败则返回给调用者None,而不是生成一个异常。这样你打开文件就不需要额外的异常处理语句。def openfile(file): try: f = open(file) except IOError: return None re...原创 2019-02-25 16:16:57 · 271 阅读 · 0 评论 -
python核心编程2 第9章 练习
9–1. 文件过滤. 显示一个文件的所有行, 忽略以井号( # )开头的行. 这个字符被用做Python , Perl, Tcl, 等大多脚本文件的注释符号.附加题: 处理不是第一个字符开头的注释.filename = input("输入文件名:")with open(filename) as f: for i in f: if i.startswith('#'):...原创 2019-02-20 19:34:27 · 1128 阅读 · 0 评论 -
python核心编程2 第8章 练习
8–2. 循环. 编写一个程序, 让用户输入三个数字: (f)rom, (t)o, 和 (i)ncrement . 以 i为步长, 从 f 计数到 t , 包括 f 和 t . 例如, 如果输入的是 f == 2, t == 26, i == 4 , 程序将输出 2, 6, 10, 14, 18, 22, 26.f = int(input("From: "))t = int(input...原创 2019-02-20 19:24:13 · 435 阅读 · 0 评论 -
python核心编程2 第7章 练习
7-4. 建立字典。给定两个长度相同的列表,比如说,列表[1, 2, 3,...]和['abc', 'def','ghi',...],用这两个列表里的所有数据组成一个字典,像这样:{1:'abc', 2: 'def', 3: 'ghi',...}listdict = dict(zip([1,2,3], ['abc','def','ghi']))print(listdict)7–5....原创 2019-02-20 17:34:01 · 237 阅读 · 0 评论 -
python核心编程2 第6章 练习
6-2. 字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别 Python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 keyword.kelist)来辅助import stringimport keywordalphas = string.ascii_letters + '_'nums = string.di...原创 2019-02-20 17:25:07 · 315 阅读 · 0 评论 -
python核心编程2 第14章 练习
14-3.执行环境。创建运行其他Python脚本的脚本。if __name__ == '__main__': with open('test.py') as f: exec(f.read())14-4. os.system()。调用os.system()运行程序。附加题:将你的解决方案移植到subprocess.call()。import osfrom s...原创 2019-03-14 11:15:50 · 273 阅读 · 0 评论