一、习题收藏

 5、写函数,计算传入字符串中【数字】、【字母】、【空格】 以及 【其他】的个数,并返回结果。
# def func4(s):
#     dic = {
#         'num':0,'alpha':0,'space':0,'others':0
#     }
#     for i in s:
#         if i.isdigit():
#             dic['num'] += 1
#         elif i.isalpha():
#             dic['alpha'] +=1
#         elif i.isspace():
#             dic['space'] += 1
#         else:
#             dic['others'] += 1
#     return dic
# s = 'asdjk2330  sdjkfh#$^&'
# ret = func4(s)
# print(ret)
# 6、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容,并返回结果。
# def func5(s_l_t):
#     if s_l_t:
#         for i in s_l_t:
#             if not i and i != 0 and i != False:
#                 return True
#     else:
#         return True

# 7、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
#     dic = {"k1": "v1v1", "k2": [11,22,33,44]}
#     PS:字典中的value只能是字符串或列表
# def func6(dic):
#     for k in dic:
#         value = dic[k]
#         if len(value) > 2:
#             dic[k] = value[:2]
#     return dic
# def func6(dic):
#     for k in dic:
#         dic[k] = dic[k][:2]
#     return dic
# dic = {"k1": "v1v1", "k2": [11,22,33,44]}
# print(func6(dic))
# 8、写函数,接收两个数字参数,返回比较大的那个数字。
# a = 1
# b = 2
# def func7(a,b):
#     if a > b:
#         return a
#     else:
#         return b
# print(func7(1,1))
#
# #三元运算符
# c = 0
# if a>b:
#     c = a
# else:
#     c = b

#新知识:重要程度五颗星*****
# c = a if a>b else b  # 三元运算符 三元运算表达式
初始函数的作业题

相关文章:

  • 2021-11-13
  • 2021-10-16
  • 2021-12-24
  • 2022-02-08
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-12
  • 2022-02-23
  • 2021-07-04
  • 2021-12-14
  • 2022-12-23
  • 2021-06-30
  • 2021-10-09
相关资源
相似解决方案