函数
def add(a, b):
"""
这个函数用来将两个数字相加
:param a: 第一个数字
:param b: 第二个数字
:return: 返回两个数字相加的和
"""
return a + b
help(add)
add(b=2, a=1)
def sub(a: int, b: int):
return a - b
a = 100
def demo():
a = 10
print(a)
demo()
print(a)
b = 100
def demo1():
global b
b = 10
demo1()
print(b)
c = 100
def demo2():
cc = 10
global b
b = 10
print(locals())
print(globals())
demo2()
print(c)
print("--------------------")
def demo3(x: int, y: int):
return x, y
resultTotal = demo3(2, 3)
resultX, resultY = demo3(2, 3)
def demo4(x, y=8):
print(x)
print(y)
demo4(5)
def sum_nums_3(a, *args, b=22, c=33, **kwargs):
print(a)
print(b)
print(c)
print(args)
print(kwargs)
sum_nums_3(100, 200, 300, 400, 500, 600, 700, b=1, c=2, mm=800, nn=900)
print("------lambda表达式-------------")
def cal(x: int, y: int, function):
return function(x, y)
add = lambda x, y: x + y
print(add(5, 4))
print(cal(4, 5, lambda x, y: x * y))
print(cal(4, 5, lambda x, y: x - y))
print("--------sort方法--------------")
lists = [1, 5, 6, 7, 9]
tuples = (1, 5, 6, 7, 9)
listDicts = [{"name": "胡勇", "age": 21, "height": 180},
{"name": "卢雯婷", "age": 18, "height": 158},
{"name": "喵喵", "age": 2, "height": 10},
]
def foo(element):
return element["age"]
tuples = sorted(tuples)
print(tuples)
listDicts.sort(key=foo, reverse=True)
listDicts.sort(key=lambda element: element["age"])
print(listDicts)
print("-----高阶函数---------------")
def test():
return 'hello world'
print(type(test))
def outer(x: int):
print("我是外部函数")
def inner():
print("我是内部函数")
if (x > 2):
inner()
outer(3)