写代码,接受n个数字,求这些数字的和
def sum_func(*args):
total = 0
for i in args:
total+=i
return total
print(sum_func(1,2,3,44,5,5,87,1,25))
读代码 打印出的a,b,c分别是多少
a = 10
b = 20
def test5(a,b):
print(a,b)
c = test5(b,a)
print(c)
print(a)
print(b)
# c = None ,10 a = 10 b = 20
读代码 打印出的a,b,c分别是多少
a = 10
b = 20
def test5(a,b):
a = 3
b = 5
print(a,b)
c = test5(b,a)
print(c)
print(a)
print(b)
# c = None a = 10 b = 20
写函数,检查获取传入的列表或者元祖的所有奇数索引对应的元素,返回回去
def func(l):
return l[1::2]
print(func([1,2,3,4,5,6,7,8]))
写函数,判断用户传入的对象(列表,字符串,元祖)是否长度大于5
def func(l):
# l = [True if len(l)> 5 else False]
# return l
return len(l)>5
print(func("sdsadhk"))
写函数,如果的长度大于2,仅保留两个长度内容并返回回去
def func(l):
# l = [l[0:2] if len(l)>2 else False]
# return l
return l[:2]
print(func("sdsadhk"))
写函数,计算传入字符串中的数字,字母,空格,以及其他的个数返回结果
def func(l):
dic = {"num":0,"alpha":0,"space":0,"other":0}
for i in l:
if i.isdigit():dic["num"]+=1
elif i.isalpha():dic["alpha"]+=1
elif i.isspace():dic["space"]+=1
else:dic["other"]+=1
return dic
print(func("shdah sdkah.sdha123 ..sihdao"))
写函数,检查用户传入的对象是否含有空内容,并返回结果
def func(x):
if type(x) is str and x :
for i in x :
if i == " ":return True
elif x and type(x) is list or type(x) is tuple:
for i in x:
if not i:return True
elif not x :return True
else : return True
print(func("shkahk shdalh"))
print(func([1,2," ",[],]))
print(func((1,2,[],)))
print(func([]))
写函数,检查传入字典的每个value的长度,长度大于2仅保留两个长度的内容,返回给调用者
def func(dic):
for k in dic:
if len(dic[k]) > 2:
dic[k] = dic[k][:2]
return dic
dic = {"k2":"shdahdkl","wc":[1,2,3,4,5,6]}
print(func(dic))
写函数,用户传入修改的文件名字,以及要修改的内容,
执行函数,完成整个文件的批量修改操作
def func(filename,old,new):
with open(filename,encoding="utf-8") as f, open("%s.bak" %filename,"w",encoding="utf-8") as f2:
for line in f:
if old in line:
line = line.replace(old,new)
f2.write(line)
import os
os.remove(filename)
os.rename("%s.bak" %filename,filename)