python基础语法四


函数的作用域:

 1 name = 'alex'
 2     
 3     def foo():
 4         name = 'linhaifei'
 5         def bar():
 6             name = "wupeiqi"
 7             def tt():
 8                 name='tt'
 9                 print(name)
10             return tt    
11         return bar
12         
13     foo()()()

#匿名函数

lambda x: x+1
	# x 形参 ; x+1 返回值
	
lambda x:x+1 #匿名函数赋值

  

name = 'alex' #name = 'alex'
函数:
def chang_name(x):
return name+'_sb' 匿名函数:
lambda x:name+'_sb' 函数式编程:函数式 = 编程语言定义的函数+ 数学意义上的函数
编程的方法论:
面向过程
函数式
面向对象

高阶函数:
满足两个特性任意一个即为高阶函数
1.函数的传入参数是一个函数名
2.函数的返回值是一个函数名
 1 def bar():
 2     print("from bar")
 3         
 4 def foo():
 5     print('from foo')
 6     return bar
 7 n = foo()
 8 n()
 9          
10  
11 #尾递归:
12 def cal(l):
13     print("assd")
14     return cal(l)

map 函数:

num_l = [1, 2, 3, 4, 5]
def map_test(func, array): #func = lambda x:x+1 array = [1, 2, 3, 4, 5]
	ret = []
	for i in array:
		res = func(i)
		ret.append(res)
	return ret

print(map_test(lambda x:x+1, num_l)
	
res = map(lambda x:x+1, num_l)
#for i in res:#迭代器,只能迭代一次!
#	print(i)
print(list(res))	

  
filter函数:

movie_people = ['sb_alex', 'sb_wupeiqi', 'linhaifeng', 'sb_yuanhao']

def sb_show(n):
	return n.endswith('sb')
# lambda n: n.endswith('sb')
	
def filter_test(func,array):
	ret = []
	for i in array:
		if not i.func(i):
			ret.append(i)
	return ret	
print(filter_test(sb_show, movie_people))
	
#最终:
	filter(lambda n:not n.endswith('sb'), movie_people)
	print(list(filter(lambda n:not n.endswith('sb'), movie_people)))

  reduce 函数:

num_l = [1, 2, 3, 4, 5]
#相加:
#res = 0
#for num in num_l:
#	res+=num
#print(res)
	
def multi(x, y):
	return x*y
	
def reduce_test(func, array, init=None):
	if init is None:
		res = array.pop(0)
	else:
		res = init
	for num in array:
		res=func(res, num)
	return res
		

#最终:
from function import reduce
num_l=[1, 2, 3, 100]
print(reduce(lambda x, y:x+y, num_l, 1)

  文件处理:

    f = open('文件名', encoding='utf-8')
    data = f.read()
    print(data)
    f.close()
    
    
    #r w a 
    默认打开的就是只读模式
    f = open('文件名','r', encoding='utf-8')
    data = f.read()
    print(f.readable())
    print(f.readline())
    print(data)
    f.close()
    
    #w 模式: 如果文件存在,文件会被清除,如果文件不存在,文件会被新建
    f = open('文件名','w', encoding='utf-8')
    f.write('111111\n')
    f.write('222222\n')
    #f.writeable()#是否可写
    #f.writelines(['555\n', '666\n'])
    f.close()
    #a 可追加
    f = open('文件名','a', encoding='utf-8')
    f.write('111111\n')
    
    
    
    f.close()
    
    
    
    
    即能读,也能写
    f = open('文件名','r+', encoding='utf-8')
    data = f.read()
    print(f.readable())
    print(f.readline())
    print(data)
    f.close()

优质内容筛选与推荐>>
1、[导入][链接] 400+ Differences
2、新居落成~~~
3、JAVA WEB开发环境与搭建
4、Pythoh网络编程4:创建UDP服务器和客户端
5、Apache 虚拟机 vhosts C:\WINDOWS\system32\drivers\etc\hosts


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn