1.列举布尔值为false的值
0 False ‘’ [] {} None
2.根据范围获取3和7整除所有数的和,并返回调用者:
符合条件的数字个数以及符合条件的数字的总和 如 def func(start,end):
方法1:递归 def func(start,end,a=0,b=0): if start == end: return a,b if start % 3 == 0 and start % 7 ==0: a+=1 b+=start ret = func(start+1,end,a,b) return ret 方法2:for循环 def func(start,end,a=0,b=0): for i in range(start,end): if i %3==0 and i%7 ==0: a+=1 b+=i return a,b
3.使用set集合获取两个列表l1 = [11,22,33],l2 = [22,33,44]中相同的元素
l1 = [11,22,33] l2 = [22,33,44] s1 = set(l1) s2 = set(l2) v=s1&s2 print(v)
4.定义一个函数统计字符串中大写字母、小写字母、数字的个数,并以字典为结果返回给调用者
int=[] upstr=[] lowstr=[] def number(a, count_int = 0,count_upstr = 0,count_lowstr = 0): for i in a: if i.isdigit(): int.append(i) count_int += 1 elif i.isupper(): upstr.append(i) count_upstr += 1 elif i.islower(): lowstr.append(i) count_lowstr += 1 return int,upstr,lowstr,count_int,count_upstr,count_lowstr a=number('shfSNFALSH1513') print(a)
5.
def func(arg): arg.append(55) li = [11,22,33,44] func(li) 此次是对li进行操作,li改变了 print(li) li = func(li) 此次是让li等于函数func的返回值,因为func么有返回值,所以返回None print(li)
运行结果:
[11, 22, 33, 44, 55]
None