语法:指定一个描述性的列表名,如element;然后指定一个列表[ ],并在列表中定义一个表达式,如value**2,计算平方值;再写一个for循环,用于给表达式提供值for value in range(1,6)
# 1)for循环——打印一个列表
a =[]for i inrange(10):
a.append(i)print(a)# 2)用列表推导式
a =[x for x inrange(10)]print(a)
# 2、创建平方数列表
a =[x for x inrange(1,6)]print(a)
b =[i **2for i in a]print(b)
# 3、c是一个列表 ,列表中元素为元组
c =[(x, y)for x inrange(3)for y inrange(5)]print(c)
# 4、实现分组一个list的元素,如[1, 2, 3,...100]变成[[1, 2, 3], [4, 5, 6]...]
x =[i for i inrange(1,101)]
y =[x[i:i +3]for i inrange(0,100,3)]# 需要取步长,否则结果不正确i=0==>[0:3] i=3===>x[3:6]print(y)
二、高阶函数
1、函数作为另一个函数的参数、返回值(高阶函数)
1)函数既然和数字、字符串等一样,也是一种数据类型,它能像数字和字符串一样当做另一个函数的参数;
2)函数也能当做另一个函数的返回值
defget_sum(n):
result =0for i inrange(n +1):
result += i
return result
print(type(get_sum))# <class 'function'>
fn = get_sum
print(get_sum(100))print(fn(20))
# 2、函数当做另一个函数的返回值deftest():print('我是test函数')print('test函数有返回值,返回值是1')return1defdemo():print('我是demo函数')print('demo函数有返回值,返回值是test这个函数的执行结果')return test()# 加括号时才调用函数deffoo():print('我是foo函数里的代码')print('foo的返回值是test函数')return test
x = demo()print(x)# 1# foo函数的返回值也是一个函数
y = foo()print(y)# 不会调用test函数,只返回test函数<function test at 0x000001AB9243FD08># 可以调用这个函数
z = y()print(z)# 1,会调用test函数
foo()()# 1,会调用test函数
2、函数嵌套函数
1)global value # 用于全局变量;
2)nonlocal value # 使用nonlocal关键字修改外部函数的局部变量
defouter():
a =100# 局部变量
b ='hello'print('我是outer内,inner外的代码')definner():
a =200# global b 用于全局变量nonlocal b # 使用nonlocal关键字修改外部函数的局部变量
b ='good'print('inner内部变量a的值是%d'% a)# 200print('inner内部变量b的值是%s'% b)#print('我是inner内的代码')
inner()print('outer里变量a的值是%d'% a)# 100print('outer里变量b的值是%s'% b)# good(加了nonlocal b后,b的值由hello修改为good)
outer()# 只打印outer# inner() # inner函数是在函数内部定义的,在函数外部不能访问
# 1、多个函数均调用同一段代码import time
deftest():print('hehe')
time.sleep(3)print('hahahahaha')# time.time()拿到是时间戳,从1970-1-1 8:00:00 UTC+8开始,到现在的秒数# print(time.time())
start = time.time()
test()
end = time.time()print(end - start)defdemo():
y =0for x inrange(10000):
y += x
start = time.time()
demo()
end = time.time()print(end - start)
# 2、多个函数均调用同一段代码——将该段代码封装成一个函数进行调用import time
defcal_time(fn):
start = time.time()
fn()
end = time.time()print(end - start)deftest():print('hehe')
time.sleep(3)print('hahahahaha')
cal_time(test)
# 3、多个函数均调用同一段代码——使用装饰器import time
defcal_decorator(fn):defdo_action():
start = time.time()
fn()# x = fn()
end = time.time()print('{}函数耗时{}'.format(fn, end - start))# return xreturn do_action
@cal_decorator# 会调用cal_decorator这个函数,并把函数名传递给cal_decorator的参数(即把test传给fn)deftest():print('hehe')
time.sleep(3)print('hahahahaha')
test()#调用装饰器# cal_decorator(test)() #注释装饰器后,执行cal_decorator()函数@cal_decoratordefdemo():
y =0for x inrange(10000):
y += x
return y
demo()# 本质上调用的其实是do_action(),demo不再是之前的那个函数,而是装饰器内部的那个函数do_actionprint('demo=={}'.format(demo))# demo==<function cal_decorator.<locals>.do_action at 0x00000214092140D0>
result = demo()print(result)# 返回None,因为demo()本质上调用的其实是do_action()
# 1、将字符串首字母大写,使用装饰器defcapital_word(fn):defdo_action(x):return x.capitalize()return do_action
@capital_worddeftest(word):# return word.capitalize()return word
result = test('hell world')# 此时调的test()就是do_action(),就需要写do_action()里面的逻辑print(result)# Hello world
fns =[lambda x: x * i for i inrange(3)]for fn in fns:print(fn(5))# 打印3遍10
fns =[]for i inrange(3):
fns.append(lambda x: x * i)# lambda表达式是个函数# fns = [lambda x: x * i, lambda x: x * i, lambda x: x * i]print(fns[0](5))# 10print(fns[1](5))# 10print(fns[2](5))# 10
defdemo(x):return x * i
for i inrange(3):# fns.append(demo)print('hehe')print(i)# i=2print(demo(5))# 10