python-函数进阶、容器通用方法、字符串比大小(笔记)
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\')Trueprint(\'--------------------\')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)# 只能支持一行代码,然后的话将这串传递给computetest_add(lambda x,y:x+y)