python基础练习6


学习内容:

  1. 实现函数get_max_score, 执行程序,最终输出: 英语 98
  2. 实现函数is_palindrome,判断参数string是否为回文,所谓回文,就是左右对称的字符串
  3. 定义一个判断列表中是否有相同项的函数。

代码实现:

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()函数可以去除列表中的重复元素,对于判断列表中是否有相同项很简单

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值