首页 > 代码库 > 14、内置函数、递归函数、匿名函数

14、内置函数、递归函数、匿名函数

一、内置函数

# print(abs(-1))
# print(all([1,2,3]))
# print(all([0,2,3]))

# print(all(‘‘))

# print(any([0,None,‘‘,1]))
# print(any([0,None,‘‘]))
# print(any([]))

# print(bin(3))
# print(hex(17))
# print(oct(9))

#0 None 空===》bool值为False,其余都为True

# def func():
# pass
# print(callable(func))


# print(chr(68))
# print(ord(‘D‘))


# res=complex(1+2j)
# print(res.real)
# print(res.imag)


#工厂函数
# dict
# int
# str
# set
# list

# l=[] #l=list([])
# list([1,2,3])
# list([‘a‘])
# list([‘b‘])


# l=[]
# print(dir(l)) #查看一个对象下面的属性
#
# print(help(l))

# print(divmod(1000,30))

# x=range(10)
# enumerate([1,2,3]).__next__()


# cmd=‘print("你瞅啥")‘

# dic="{‘a‘:1,‘b‘:2}"
# d=eval(dic)
# print(type(d),d[‘a‘])

# with open(‘user.db‘,‘w‘,encoding=‘utf-8‘) as f:
# user_dic={‘name‘:‘egon‘,‘password‘:‘123‘}
# f.write(str(user_dic))

# with open(‘user.db‘,‘r‘,encoding=‘utf-8‘) as f:
# dic=f.read()
# print(dic,type(dic))
# dic=eval(dic)
# print(dic[‘name‘])



s={1,2} #s=set({1,2}) 定义可变集合

# s.add(3)
# print(s)

# s=frozenset({1,2}) # 定义不可变集合


#哈希:
# 1. 只要校验的内容一致,那hash得到结果永远一样
# 2. 不可逆
# 3. 只要采用的哈希算法一样,那无论被校验的内容有多长,hash的到的结果长度都一样
# print(hash(‘asdfasdfsadf‘))
# print(hash(‘asdfasdfsadf‘))

# x=1
# y=x
# print(id(x),id(y))
#
# print(x is y) #判断的是身份

# a=1
# b=1
# print(a == b) #判断的是值



# print(max([1,2,3,4]))

# print(pow(10,2,3))

l=[‘a‘,4,2,3]
# for i in reversed(l):
# print(i)
#
# print(list(reversed(l)))

# i=reversed(l)
# for x in i:
# print(x)
# print(list(i))



# print(round(3.141542653589127134,4))



# l=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]
# print(l[1:4:2])
# s=slice(1,4,2)
# print(l[s])


# vars() #等于locals()


# s=‘helloo‘
# l=[1,2,3,4,5]
#
# z=zip(s,l)
# print(z)
# for i in z:
# print(i)


import time


m=__import__(‘time‘) #以字符串的形式导入模块

# m.sleep(3000)


#面向对象里面讲
super

isinstance
issubclass


classmethod
staticmethod
property

delattr
hasattr
getattr

技术分享

二、递归函数

#递归调用:在函数调用过程中,直接或间接地调用了函数本身,这就是函数的递归调用


#
# def f1():
# print(‘from f1‘)
# f1()
#
# f1()



# def f1():
# print(‘f1‘)
# f2()
#
# def f2():
# f1()
#
# f1()

# import sys
# print(sys.getrecursionlimit())
# print(sys.setrecursionlimit(1000000))
#
# print(sys.getrecursionlimit())



# age(5)=age(4)+2
# age(4)=age(3)+2
# age(3)=age(2)+2
# age(2)=age(1)+2
# age(1)=18
#
#
#
#
# age(n)=age(n-1)+2 n>1
# age(n)=18 n=1

# def age(n):
# if n == 1:
# return 18
# return age(n-1)+2
#
# print(age(5))


#二分法

l = [1, 2, 10,33,53,71,73,75,77,85,101,201,202,999,11111]

def search(find_num,seq):
if len(seq) == 0:
print(‘not exists‘)
return
mid_index=len(seq)//2
mid_num=seq[mid_index]
print(seq,mid_num)
if find_num > mid_num:
#in the right
seq=seq[mid_index+1:]
search(find_num,seq)
elif find_num < mid_num:
#in the left
seq=seq[:mid_index]
search(find_num,seq)
else:
print(‘find it‘)

# search(77,l)
# search(72,l)
# search(-100000,l)

三、匿名函数

# def func(x,y):
# return x+y
#
# func(1,2)


#匿名函数
# f=lambda x,y:x+y
# print(f)
#
# print(f(1,2))
#
# lambda x,y:x+y


#max,min,zip,sorted的用法
salaries={
‘egon‘:3000,
‘alex‘:100000000,
‘wupeiqi‘:10000,
‘yuanhao‘:2000
}

# print(max(salaries))
# res=zip(salaries.values(),salaries.keys())
#
# # print(list(res))
# print(max(res))

# def func(k):
# return salaries[k]

# print(max(salaries,key=func))
# print(max(salaries,key=lambda k:salaries[k]))
# print(min(salaries,key=lambda k:salaries[k]))

# print(sorted(salaries)) #默认的排序结果是从小到到
# print(sorted(salaries,key=lambda x:salaries[x])) #默认的排序结果是从小到到
# print(sorted(salaries,key=lambda x:salaries[x],reverse=True)) #默认的排序结果是从小到到
#




# x=1000
# def f1():
# # global x
# x=0
#
# f1()
# print(x)




‘‘‘
4. 内置函数
map
reduce
filter
‘‘‘


# def func(f):
# return f
#
# res=func(max)
# print(res)


# l=[‘alex‘,‘wupeiqi‘,‘yuanhao‘]
#
#
# res=map(lambda x:x+‘_SB‘,l)
#
# print(res)
#
# print(list(res))
#
# nums=(2,4,9,10)
#
# res1=map(lambda x:x**2,nums)
#
# print(list(res1))

# from functools import reduce
#
# l=[1,2,3,4,5]
# print(reduce(lambda x,y:x+y,l,10))


# l=[‘alex_SB‘,‘wupeiqi_SB‘,‘yuanhao_SB‘,‘egon‘]
#
# res=filter(lambda x:x.endswith(‘SB‘),l)
# print(res)
# print(list(res))












14、内置函数、递归函数、匿名函数