1.复习

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
name = 'alex' #name=‘lhf’
def change_name():
name='lhf'
# global name
# name = 'lhf'
# print(name)
# name='aaaa' #name='bbb'
def foo():
# name = 'wu'
nonlocal name
name='bbbb'
print(name)
print(name)
foo()
print(name) change_name()

2.匿名函数

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# def calc(x):
# return x+1 # res=calc(10)
# print(res)
# print(calc) # print(lambda x:x+1)
# func=lambda x:x+1
# print(func(10)) # name='alex' #name='alex_sb'
# def change_name(x):
# return name+'_sb'
#
# res=change_name(name)
# print(res) # func=lambda x:x+'_sb'
# res=func(name)
# print('匿名函数的运行结果',res) # func=lambda x,y,z:x+y+z
# print(func(1,2,3)) # name1='alex'
# name2='sbalex'
# name1='supersbalex' # def test(x,y,z):
# return x+1,y+1 #----->(x+1,y+1) # lambda x,y,z:(x+1,y+1,z+1)

3.作用域

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# def test1():
# print('in the test1')
# def test():
# print('in the test')
# return test1
#
# # print(test)
# res=test()
# # print(res)
# print(res()) #test1() #函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系
# name = 'alex'
# def foo():
# name='linhaifeng'
# def bar():
# # name='wupeiqi'
# print(name)
# return bar
# a=foo()
# print(a)
# a() #bar()

4.函数式编程

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
#高阶函数1。函数接收的参数是一个函数名 2#返回值中包含函数
# 把函数当作参数传给另外一个函数
# def foo(n): #n=bar
# print(n)
# #
# def bar(name):
# print('my name is %s' %name)
# #
# # foo(bar)
# # foo(bar())
# foo(bar('alex'))
#
#返回值中包含函数
# def bar():
# print('from bar')
# def foo():
# print('from foo')
# return bar
# n=foo()
# n()
# def hanle():
# print('from handle')
# return hanle
# h=hanle()
# h()
#
#
#
# def test1():
# print('from test1')
# def test2():
# print('from handle')
# return test1()

4.map函数

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# num_l=[1,2,10,5,3,7]
# num1_l=[1,2,10,5,3,7] # ret=[]
# for i in num_l:
# ret.append(i**2)
#
# print(ret) # def map_test(array):
# ret=[]
# for i in num_l:
# ret.append(i**2)
# return ret
#
# ret=map_test(num_l)
# rett=map_test(num1_l)
# print(ret)
# print(rett) num_l=[1,2,10,5,3,7]
#lambda x:x+1
def add_one(x):
return x+1 #lambda x:x-1
def reduce_one(x):
return x-1 #lambda x:x**2
def pf(x):
return x**2 def map_test(func,array):
ret=[]
for i in num_l:
res=func(i) #add_one(i)
ret.append(res)
return ret # print(map_test(add_one,num_l))
# print(map_test(lambda x:x+1,num_l)) # print(map_test(reduce_one,num_l))
# print(map_test(lambda x:x-1,num_l)) # print(map_test(pf,num_l))
# print(map_test(lambda x:x**2,num_l)) #终极版本
def map_test(func,array): #func=lambda x:x+1 arrary=[1,2,10,5,3,7]
ret=[]
for i in array:
res=func(i) #add_one(i)
ret.append(res)
return ret # print(map_test(lambda x:x+1,num_l))
res=map(lambda x:x+1,num_l)
print('内置函数map,处理结果',res)
# for i in res:
# print(i)
# print(list(res))
# print('传的是有名函数',list(map(reduce_one,num_l))) msg='linhaifeng'
print(list(map(lambda x:x.upper(),msg)))

5.filter函数

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao'] # def filter_test(array):
# ret=[]
# for p in array:
# if not p.startswith('sb'):
# ret.append(p)
# return ret
#
# res=filter_test(movie_people)
# print(res) # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
# def sb_show(n):
# return n.endswith('sb')
#
# def filter_test(func,array):
# ret=[]
# for p in array:
# if not func(p):
# ret.append(p)
# return ret
#
# res=filter_test(sb_show,movie_people)
# print(res) #终极版本
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
# def sb_show(n):
# return n.endswith('sb')
#--->lambda n:n.endswith('sb') def filter_test(func,array):
ret=[]
for p in array:
if not func(p):
ret.append(p)
return ret # res=filter_test(lambda n:n.endswith('sb'),movie_people)
# print(res) #filter函数
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
# print(list(filter(lambda n:not n.endswith('sb'),movie_people)))
res=filter(lambda n:not n.endswith('sb'),movie_people)
print(list(res)) print(list(filter(lambda n:not n.endswith('sb'),movie_people)))

6.reduce函数

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
from functools import reduce # num_l=[1,2,3,100]
#
# res=0
# for num in num_l:
# res+=num
#
# print(res) # num_l=[1,2,3,100]
# def reduce_test(array):
# res=0
# for num in array:
# res+=num
# return res
#
# print(reduce_test(num_l)) # num_l=[1,2,3,100]
#
# def multi(x,y):
# return x*y
# lambda x,y:x*y
#
# def reduce_test(func,array):
# res=array.pop(0)
# for num in array:
# res=func(res,num)
# return res
#
# print(reduce_test(lambda x,y:x*y,num_l)) # num_l=[1,2,3,100]
# 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
#
# print(reduce_test(lambda x,y:x*y,num_l,100)) #reduce函数
# from functools import reduce
# num_l=[1,2,3,100]
# print(reduce(lambda x,y:x+y,num_l,1))
# print(reduce(lambda x,y:x+y,num_l))

7.小结

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
#处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数及位置与原来一样
# map() #filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来 people=[
{'name':'alex','age':1000},
{'name':'wupei','age':10000},
{'name':'yuanhao','age':9000},
{'name':'linhaifeng','age':18},
]
# print(list(filter(lambda p:p['age']<=18,people)))
# print(list(filter(lambda p:p['age']<=18,people))) #reduce:处理一个序列,然后把序列进行合并操作
from functools import reduce
print(reduce(lambda x,y:x+y,range(100),100))
# print(reduce(lambda x,y:x+y,range(1,101)))

8.内置函数

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
# print(abs(-1))
# print(abs(1))
#
# print(all([1,2,'1']))
# print(all([1,2,'1','']))
# print(all('')) # print(any([0,'']))
# print(any([0,'',1])) # print(bin(3)) #空,None,0的布尔值为False,其余都为True
# print(bool(''))
# print(bool(None))
# print(bool(0)) name='你好'
# print(bytes(name,encoding='utf-8'))
# print(bytes(name,encoding='utf-8').decode('utf-8')) # print(bytes(name,encoding='gbk'))
# print(bytes(name,encoding='gbk').decode('gbk'))
#
# print(bytes(name,encoding='ascii'))#ascii不能编码中文
#
# print(chr(46))
#
# print(dir(dict))
#
# print(divmod(10,3)) # dic={'name':'alex'}
# dic_str=str(dic)
# print(dic_str) #可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型
# print(hash('12sdfdsaf3123123sdfasdfasdfasdfasdfasdfasdfasdfasfasfdasdf'))
# print(hash('12sdfdsaf31231asdfasdfsadfsadfasdfasdf23'))
#
name='alex'
# print(hash(name))
# print(hash(name))
#
#
# print('--->before',hash(name))
# name='sb'
# print('=-=>after',hash(name)) # print(help(all))
#
# print(bin(10))#10进制->2进制
# print(hex(12))#10进制->16进制
# print(oct(12))#10进制->8进制 name='哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陈'
# print(globals())
# print(__file__)
#
def test():
age=''
# print(globals())
print(locals())
#
# test()
#
l=[1,3,100,-1,2]
# print(max(l))
# print(min(l)) # print(isinstance(1,int))
# print(isinstance('abc',str))
print(isinstance([],list))
# print(isinstance({},dict))
print(isinstance({1,2},set))

最新文章

  1. 7 Must Read Python Books
  2. Linux下安装和配置JDK与Tomcat(入门版)
  3. 基于MVC4+EasyUI的Web开发框架形成之旅--界面控件的使用
  4. .html和.htm的区别
  5. 【转】IntelliJ IDEA内存优化最佳实践
  6. Visual Studio 2012 使用SvcUtil在MVC4客户端引用WCF服务
  7. Qt之QCustomPlot绘图(一)配置和第一个例子
  8. 动态设置 GridView 列宽
  9. python &lt;tab&gt;自动补全
  10. LINUX下printf输出字体的特效
  11. [LeetCode] Number Of Corner Rectangles 边角矩形的数量
  12. BZOJ5341[Ctsc2018]暴力写挂——边分治+虚树+树形DP
  13. mybatis查询修改同时操作
  14. Win10手记-为应用集成SQLite(一)
  15. Ubuntu(14.04LTS)学习札记
  16. Mercedes offline programming/coding tips and guides
  17. Android命令(更新……)
  18. Javascript-逻辑或(||)
  19. How to add a button in the seletions &quot;More&quot;
  20. c++可变参数(示例)

热门文章

  1. 常用JSTL标签
  2. PJzhang:工作之余一起来看剧
  3. Nginx系列篇一:linux中安装Nginx
  4. [BJOI2017]魔法咒语
  5. 洛谷 P4219 [BJOI2014]大融合
  6. 总结 - 常见的JavaScript兼容性问题
  7. python中的sort和sorted
  8. dangerouslySetHTML 和 style 属性
  9. Git之提交项目到远程github
  10. client系列、offset系列、scroll系列