python函数练习题
python函数练习题所练习的不仅仅是函数如何使用,还包括基础知识的运用,尽管所要实现的算法在不使用函数的情况下也都能实现,而且很多算法在前面都已经学习实践过。但对初学者而言,十分有必要通过编写函数来理解掌握函数的定义,调用,传参,返回值等概念,进而提高自己的逻辑思考能力。
1. 求列表最大值
1.1 题目要求
def get_max(lst):
"""
返回列表lst的最大值
:param lst:
:return:
"""
pass
lst = [4, 2, 1, 6, 7, 9]
max_value = get_max(lst)
print(max_value)
实现函数get_max,函数最终返回列表lst的最大值,不要使用max函数
1.2 示例代码
def get_max(lst):
"""
返回列表lst的最大值
:param lst:
:return:
"""
if not lst:
return None
max_value = lst[0]
for item in lst:
if item > max_value:
max_value = item
return max_value
lst = [4, 2, 1, 6, 7, 9]
max_value = get_max(lst)
print(max_value)
2. 求学生最高分数科目和分数
2.1 题目要求
def get_max_socre(score_dic):
"""
返回学生考试成绩的最高分的科目和分数
:param score_dic:
:return:
"""
pass
dic = {
'语文': 90,
'数学': 97,
'英语': 98
}
course, score = get_max_socre(dic)
print(course, score)
实现函数get_max_socre, 执行程