学习内容:
-
实现函数get_max_score, 执行程序,最终输出: 英语 98
-
实现函数is_palindrome,判断参数string是否为回文,所谓回文,就是左右对称的字符串
-
定义一个判断列表中是否有相同项的函数。
代码实现:
1.
def get_max_socre(score_dic):
max_score = 0
for name,info in score_dic.items():
if score_dic[name]>max_score:
max_score =score_dic[name]
return name,max_score
dic = {
'语文': 90,
'数学': 97,
'英语': 98
}
course, score = get_max_socre(dic)
print(course, score)
2.
def is_palindrome(string):
if string == string[::-1]:
return "是回文"
else:
return "不是回文"
print(is_palindrome('abcddcba'))
print(is_palindrome('pythonohtyp'))
print(is_palindrome('bookkob'))
3.
def same_list(list):
if len(list)==len(set(list)):
return "无相同项"
else:
return "有相同项"
ls1 =[1,2,3,4,5,6,7,89]
print(same_list(ls1))
总结:
set()函数可以去除列表中的重复元素,对于判断列表中是否有相同项很简单