#lambda 作为匿名函数,是不需要函数名的→需要一个函数,又不想动脑筋取想名字,这就是匿名函数#lambda的主体是一个表达式,而不是一个代码块#lambda只有一行,仅仅能在lambda表达式中封装有限的逻辑进去deffun(a,b,c):return a+b+c
print(fun(1,2,3))
f =lambda a,b,c:a+b+c
print(f(1,2,3))
f =lambda num:len(num)print(f([1,2,3,4,5,6,'l']))
列表排序函数两种写法
deff(list):list.sort
returnlistprint(f([3,1,2,55]))deff(*var):
st =list(var)
st_re =sorted(st,reverse=True)return(st_re)print(f(1,2,6,4,2,3,6))
字符串各类型元素计数
st ='abcndjd de'len(st)
a =0
b =0
c =0
d =0for n inrange(0,len(st)-1):if st[n].isalpha():
a +=1elif st[n].isspace():
b +=1elif st[n].isdigit():
c +=1else:
d +=1print(a,b,c,d)
# time模块import time
for i inrange(5):print('hello')
time.sleep(1)#休眠print(time.ctime())#当前时间print(type(time.ctime()))print(time.localtime())#当地时间print(type(time.localtime()))print(time.strftime('%y-%m-%d %H:%M:%S',time.localtime()))#按指定格式输出时间print(type(time.strftime('%y-%m-%d %H:%M:%S',time.localtime())))
result:
hello
hello
hello
hello
hello
Thu Oct 3108:11:072019<class'str'>
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=31, tm_hour=8, tm_min=11, tm_sec=7, tm_wday=3, tm_yday=304, tm_isdst=0)<class'time.struct_time'>19-10-3108:11:07<class'str'>
random模块
# random 模块import random
x = random.random()#输出0到1的随机数
y = random.random()print(x,y*100)
m = random.randint(0,20)#输出指定范围内1个整数print(m)
lst =list(range(20))
s = random.choice(lst)#列表中选择一个数print(s)
sli = random.sample(lst,5)#列表中选择5个数print(sli)
random.shuffle(lst)#列表打乱print(lst)