【百度飞桨领航团零基础Python速成营】6次作业解析
作业一:Python编程基础
课程链接:https://aistudio.baidu.com/aistudio/course/introduce/7073
按要求补全下列代码
1. 输入两个整数,并打印出它们的和
In [13]
a = input('请输入第一个整数: ')
b = input('请输入第二个整数: ')
# 分别把 a、b 转换成整数
a = int(a)
print('a =' , a)
b = int(b)
print('b = ' ,b)
# 计算 a、b 的和,赋值给变量c
c=a+b
# 打印c
print(c)
请输入第一个整数:
请输入第二个整数: a = 2
b = 3
5
2. 输入两个整数,如果两个整数之和小于100,则输出 '小于100',否则输出 '不小于100'
In [17]
a = input('请输入第一个整数: ')
b = input('请输入第二个整数: ')
# 分别把 a、b 转换成整数
a = int(a)
b = int(b)
# 计算 a、b 的和,赋值给变量c
c=a+b
# 判断c是否小于100,按要求输出
if c<100:
print("c小于100")
if c>=100:
print("c不小于100")
请输入第一个整数:
请输入第二个整数: c不小于100
3. 输入两组姓名和年龄,然后存入一个字典,并输出
In [19]
name1 = input('请输入第一个姓名: ')
age1= input('请输入第一个年龄: ')
name2 = input('请输入第二个姓名: ')
age2 = input('请输入第二个年龄: ')
# 分别把age1和age2转成整数
age1 = int(age1)
age2 = int(age2)
# 构造字典dict_name
dict_name = name1,age1,name2,age2
# 打印字典
print(dict_name)
请输入第一个姓名:
请输入第一个年龄:
请输入第二个姓名:
请输入第二个年龄: ('小明', 5, '小红', 6)
4. 依次输入10组整数,然后求和,并输出
In [25]
sum_num = 0
for i in range(10):
# 用input输入数字并转化为整数
input('请输入数字')
i = int(i)
# sum_num 对输入的数字进行累加
sum_num +=i
print(sum_num)
请输入数字
请输入数字
请输入数字
请输入数字
请输入数字
请输入数字
请输入数字
请输入数字
请输入数字
请输入数字9
作业二:Python编程基础(二)
按要求完成下列代码
1. 选取列表的第2到第5项,并打印(从0开始计数,即取出c d e f)
In [3]
words = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
list=words
# 选取第2-5项,
list[2:6]
['c', 'd', 'e', 'f']
2. 使用列表生成式的方法,根据 list1 生成 list2
list1 = [1, 2, 3, 4, 5, 6, 7, 8]
# 根据list1生成list2
list2 = [100, 200, 300, 400, 500, 600, 700, 800]
In [2]
list1 = [1, 2, 3, 4, 5, 6, 7, 8]
# 列表推导式生成list2
list2 = [n*100 for n in list1]
print(list2)
[100, 200, 300, 400, 500, 600, 700, 800]
3. 把下列字符串按下划线('_')划分成若干个片段
string1 = 'this_is_a_sample'
# 生成按'_'划分的字符串列表,即下列内容
['this', 'is', 'a', 'sample']
In [1]
string1 = 'this_is_a_sample'
# 按'_'划分string1
string1.split('_')
['this', 'is', 'a', 'sample']
Lesson 3 大作业
作业内容
统计英语6级试题中所有单词的词频,并返回一个如下样式的字典
{'and':100,'abandon':5}
英语6级试题的文件路径./artical.txt
Tip: 读取文件的方法
def get_artical(artical_path):
with open(artical_path) as fr:
data = fr.read()
return data
get_artical('./artical.txt')
处理要求
- (a) '\n'是换行符 需要删除
- (b) 标点符号需要处理
['.', ',', '!', '?', ';', '\'', '\"', '/', '-', '(', ')']
- (c) 阿拉伯数字需要处理
['1','2','3','4','5','6','7','8','9','0']
- (d) 注意大小写 一些单词由于在句首,首字母大写了。需要把所有的单词转成小写
'String'.lower()
- (e) 高分项
通过自己查找资料学习正则表达式,并在代码中使用(re模块)
可参考资料:https://docs.python.org/3.7/library/re.html
import re
# 请根据处理要求下面区域完成代码的编写。
def get_artical(artical_path):
with open(artical_path) as fr:
data = fr.read()
return data
# get_artical()为自定义函数,可用于读取指定位置的试题内容。
raw_artical = get_artical('./artical.txt')
# 转小写 然后 去掉换行,标点符号,数字
raw_artical = re.sub(r'[\d\n\.\-,!\?")(:;\/]+', ' ', raw_artical.lower())
# 得到所有单词
raw_artical_list = re.split(r'\s+', raw_artical.strip())
# # 获取所有单词word数量
words = {}
for word in set(raw_artical_list):
words[word] = raw_artical_list.count(word)
print(words)
# get_artical()为自定义函数,可用于读取指定位置的试题内容。
#get_artical('./artical.txt')
{'should': 6, 'throughout': 2, 'balance': 1, 'state': 1, 'asked': 2, 'include': 1, 'adults': 1, 'this': 9, 'proposed': 3, 'extend': 1, 'changes': 3, 'coordinator': 1, 'rising': 2, 'mutual': 1, 'retreating': 1, 'survive': 1, 'immediate': 4, 'exotic': 1, 'also': 2, 'upward': 1, 'hard': 1, 'actions': 1, 'scientific': 2, 'daunting': 1, 'power': 1, 'strongly': 1, 'holiday': 1, 'assume': 1, 'refined': 1, 'discussions': 2, 'beach': 1, 'single': 1, 'serve': 2, 'expect': 1, 'wiped': 1, "penguins'": 1, 'objects': 1, 'someone': 1, 'comes': 1, 'halting': 1, 'society': 4, 'intellectual': 1, 'once': 1, 'yet': 1, 'life': 3, 'destroy': 1, 'way': 6, 'think': 5, 'early': 1, 'tracts': 1, 'guilt': 1, 'lodging': 1, 'prepared': 1, 'organisation': 1, 'admire': 1, 'prepare': 1, 'right': 1, 'tract': 1, 'out': 7, 'highlights': 1, 'when': 7, 'neighbours': 2, "one's": 4, 'fire': 1, 'hope': 1, 'powerful': 2, 'show': 1, 'aid': 1, 'impact': 5, 'go': 1, 'aspirations': 1, 'poor': 1, 'treks': 1, "idols'": 1, 'part': 2, 'extinct': 1, 'immense': 1, 'arrive': 1, 'relations': 1, 'science': 2, 'answered': 1, 'resolving': 1, 'different': 2, 'kinds': 1, 'c': 20, "world's": 2, 'tools': 1, 'further': 3, 'action': 1, 'propagation': 1, 'opting': 1, 'mix': 1, 'however': 2, 'board': 1, 'luge': 1, 'while': 1, 'potentially': 2, 'run': 1, 'industrial': 3, 'largest': 2, 'ensuring': 1, '