• 函数


    #函数
    # 1.减少重复代码
    # # 2.方便修改,更易扩展,具有加减功能
    # # 3.保持代码的一致性

    #调用函数一定记得加括号

    #参数

    # def add(x,y):
    # print(x+y)
    # add(2,3)

    # def f(形参)
    # pass 函数里的是形参 调用的是实参
    # f(实参)




    # def logger(n):
    # # with open('日志记录', 'a') as f:
    # # f.write(' action end %s'%n)
    # #
    # #
    # # logger(1)


    #1.必须参数
    # f:(a.b) 调用
    #2.关键字参数
    # f:(name = '', age = ) 调用
    # 3.默认参数
    # f:(name = '', age = , sex = 'male') 调用 默认参数一定要跟在其他参数后面
    # 4.不定长参数
    # add(*args)无命名参数 add(**kwargs)有命名参数 不定长

    # 参数的位置关系
    # def 函数名(a,sex = 'male',*args,**kwargs) 从左至右的位置关系


    # 函数返回值
    # return
    # 1结束函数
    # 2返回值
    # 没有reture 返回None
    # def add(*args):
    # Sum = 0
    # for i in args:
    # Sum += i
    # return Sum
    # a = add(1,5)
    # print(a)


    # 作用域
    # if True:
    # x = 3
    # print(x)

    # def f():
    # a = 10
    # print(a)




    # 规则 L E G B
    # B = built-in 最外层
    # G = global 全局变量
    # E = enclosing 局部
    # L = local 局部

    # x = int(2.9) built-in
    #
    # g_count = 0 global
    #
    # def outer():
    # o_count = 1 enclosing
    #
    # def inner():
    # i_count = 2 local
    # print(i_count)
    # inner()
    #
    #
    # outer()



    # count = 10
    #
    # def outer():
    # global count 声明全局
    # print(count) #local variable 'count' referenced before assignment 在赋值之前被调用’count‘
    # count = 5
    #
    #
    # outer()

    # def outer():
    # count = 10
    # def inner():
    # nonlocal count 外层函数 local声明
    # count = 20
    # print(count)
    # inner()
    # print(count)
    #
    # outer()


    # 内部作用域 先声明就会覆盖外部作用域 不声明不能直接使用


    # def f(*args):
    # print(args)

    #f([1,2,3]) #([1, 2, 3],)
    # f(*[1,2,3]) (1, 2, 3)

    # def f(**kwargs):
    # print(kwargs)
    #
    # f(**{'name' : 'qian','age' : 32}) #{'name': 'qian', 'age': 32}


    #高阶函数

    # def f(n):
    # return n*n
    #
    # def foo(a,b,f):
    # #f(a)+f(b)
    # return f(a)+f(b)
    # print(foo(2,3,f))


    # 1. 函数名可以进行赋值 函数名是个变量
    # 2. 函数名可以作为函数参数,还可以作为函数的返回值


    # def f(n):
    #
    # fat = 1
    # for i in range(1,n+1):
    # fat = fat * i
    # print(i)
    # return fat
    #
    #
    # print(f(7))



    # def fact(n):
    #
    # if n == 10:
    # return 1
    # # 递归函数的特点
    # return n * fact(n+1) #1.自己调用自己,2.必须有结束条件,否则无限循环
    # # 但凡递归可以写的,循环都可以写
    # #递归的效率很多时候很低
    #
    # print(fact(5))


    # x = 1
    # y = 1
    #
    # while x < 30:
    # x = x + y #递归 循环
    # print(x)
    # y = x + y
    # print(y)



    # def fibo(n):
    # before = 0
    # after = 1
    # for i in range(n-1):
    # rat = before + after
    # before = after
    # after = rat
    # return rat
    #
    #
    # print(fibo(3))


    # def fibo(n):
    #
    #
    #
    # return fibo(n-1) + fibo(n-2)
    # print(fibo(6))
    #
    #内置函数

    # ads(x) 绝对值
    # all(iterable可迭代) agitue是letumle所有(iterable)如果iterable的所有元素都为真(或者如果iterable为空),则返回True。等价于:

    #filter()
    # str = ['a','b','c','d']
    # def fun1(s):
    # if s != 'a': 不等于 保留 等于 不保留
    # return s
    # ret = filter(fun1,str)
    #
    # print(list(ret)) #ret是可迭代器对象

    #map
    # str = ['a','b','c']
    # def fun2(s):
    # return s + 'qian'
    # ret = map(fun2,str)
    # print(list(ret)) #map object的迭代器
    # #str = ['a','b','c','d']


    #reduce

    # from functools import reduce
    #
    # def add1(x,y):
    #
    # return x + y
    #
    # print(reduce(add1,range(1,101))) 相当于阶乘 reduce返回结果就是一个值

    #lambda
    # def add(a,b):
    # return a + b
    # print(lambda a,b : a + b)








  • 相关阅读:
    IfcDescriptiveMeasure
    IfcBooleanOperator
    IfcKnotType
    IfcCurveOnSurface
    IfcCurvatureMeasure
    IfcBSplineCurveForm
    IfcDoseEquivalentMeasure
    netty系列之:使用UDP协议
    netty系列之:自定义编码和解码器要注意的问题
    netty系列之:内置的Frame detection
  • 原文地址:https://www.cnblogs.com/xuexihainan/p/12031869.html
Copyright © 2020-2023  润新知