1. 程式人生 > >14、內置函數、遞歸函數、匿名函數

14、內置函數、遞歸函數、匿名函數

instance 判斷 查看 utf 永遠 ins mage lob alex

一、內置函數

# 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、內置函數、遞歸函數、匿名函數