python數據容器的通用方法
#記住排序后容器類型會變成list容器列表
list=[1,3,5,4,6,7]
newList=sorted(list,reverse=True)
print(newList)
[7, 6, 5, 4, 3, 1]list=[1,3,5,4,6,7]
newList=sorted(list,reverse=False)
print(newList)
[1, 3, 4, 5, 6, 7]
字典排序的是字典的key
字符串大小比較
print('--------------------')
print('abd'>'abc')print('key2'>'key1')True
print('--------------------')
print('ab'>'a')True
print('abd11'>'abc22')
True
函數多返回值
def get_user():return 1,"John"num,name=get_user()print(num,name)1 John
函數的多種傳參形式
這個傳參的形式重點記
def get_user(name,age):print(f"the name is {name}, age is {age}")#這樣的寫法可以不考慮順序前后,否則必須按照形參的順序一一賦值(未知參數)
get_user(name="tom",age=200)the name is tom, age is 200
未知參數
缺省函數(重點)
#這個有默認參數的值,要放到形參的最后位置否則比報錯
# 例如 def get_user(age=90,name):直接報錯
def get_user(name,age=90):print(f"the name is {name}, age is {age}")get_user(name="Jane")the name is Jane, age is 90
不定長參數(重點):
def get_location(*args):print(args)
# 該形參的默認數據容器類型為元組
get_location("beijing","shanghai","guangzhou")('beijing', 'shanghai', 'guangzhou')
def get_my_dick(**kwargs):print(kwargs)get_my_dick(name="tom",age=18){'name': 'tom', 'age': 18}
函數作為參數傳遞(重點):
def compute(a,b):#內部邏輯已經寫好了 只是數據的不確定性return a+bdef test_add(compute):#這邊我們提供的只是數據,這里數據是確定的,邏輯內部不需要知道,我調用我需要的業務邏輯就行z=compute(1,2)print(z)test_add(compute)3
匿名函數:
def test_add(compute):z=compute(1,2)print(z)# 只能支持一行代碼,然后的話將這串傳遞給compute
test_add(lambda x,y:x+y)