1、递归求和
i= 1
sum = 0
def he ( i, sum ) :
sum += i
if i< 10 :
i+= 1
return he( i, sum )
print ( sum )
he( i, sum )
2、集合去重
list = [ 'a' , 'd' , 's' , 'f' , 'a' , 'a' , 'f' ]
set = set ( list )
print ( set )
3、装饰器记录程序运行时间
def count_time ( func) :
def int_time ( * args, ** kwargs) :
start_time = datetime. datetime. now( )
func( )
over_time = datetime. datetime. now( )
total_time = ( over_time- start_time) . total_seconds( )
print ( '程序共计%s秒' % total_time)
return int_time
@count_time
def main ( ) :
print ( '>>>>开始计算函数运行时间' )
for i in range ( 1 , 1000 ) :
for j in range ( i) :
print ( j)
4、map函数配合匿名函数
x= [ 1 , 2 , 3 ]
y= map ( lambda x: x+ 1 , x)
print ( list ( y) )
print ( "hello" if True else "world" )
5、zip的使用
list = [ 1 , 2 , 3 , 4 , 4 , 4 , 5 , 5 , 5 , 5 ]
print ( max ( set ( list ) , key= list . count) )
使用两个相关的序列构建一个字典
t1= ( 1 , 2 , 3 )
t2= ( 10 , 20 , 30 )
print ( dict ( zip ( t1, t2) ) )
6、
斐波那契函数
def fibonacci ( n) :
a, b, counter = 0 , 1 , 0
while True :
if counter > n:
return
yield a
a, b = b, a + b
counter += 1
fib = fibonacci( 10 )
print ( type ( fib) )
for i in fib:
print ( i, end= " " )