zoukankan      html  css  js  c++  java
  • Python-9 函数

    #1 内建函数

      乘方:pow()

      >>> pow(2,3)
      8
      >>>

      取绝对值:abs()

      >>> abs(-1)
      1
      >>>

      四舍五入为最接近的整数:round()

      >>> round(2/3)
      1
      >>> round(3/2)
      2
      >>>

     #2 形参、实参

      def mythirdfunc(name1,name2):
          print(name1+'我爱你')#函数定义过程中的参数为形参,函数调用过程中则为实参
          return (name1+name2)#返回值(没有返回值时候 则返回None)

     #3 函数文档

      print(print.__doc__)#查看函数print的函数文档(此方法直接在shell中输入使用)
      help(print)#查看函数print的函数文档

     #4 关键字参数:调用函数时给实参指定形参名称

      mythirdfunc(name1='欢欢',name2='我爱你')

     #5 默认参数:定义函数时指定形参参数对应的默认值,若在调用时未给参数赋值则使用默认值

      def mythirdfunc(name1='欢欢',name2='我爱你'):
             return (name1+name2)#返回值

     #6 收集参数:调用函数时先收集所有非关键字参数为该参数的实参(使用元组--以,作为标志)

      def myfifthfunc(*name,name1=2):
          #*name--收集参数:调用函数时先收集所有非关键字参数为该参数的实参
          print(name,name1)

     #7 局部变量、全局变量

      函数内定义、修改--局部变量

      函数外定义、修改--全局变量

      函数内修改全局变量:global 变量名

      def mysixthfunc():
          print('调用函数6')
          def myseventhfunc():#内嵌函数--只在定义该函数的函数内有效(定义局部内有效)
              print('调用函数7')
          myseventhfunc()

    #8 闭包:自由变量+函数--调用了一个函数A,这个函数A返回了一个函数B。这个返回的函数B就叫做闭包。调用函数A的时候传递的参数就是自由变量。

      def myeighthfunc(x):
          def myninethfunc(y):
              return x*y
          return myninethfunc

      def myeighthfunc():
          x=[1]
          def myninethfunc():
              x[0]*=x[0]
              return x[0]#列表不是存放在栈里面 不受全局变量影响
          return myninethfunc()

      def myeighthfunc():
          x=1
          def myninethfunc():
              nonlocal x#nonlcal使用方法类似global--将变量定义为非局部变量
              x*=x
              return x
          return myninethfunc()

    #9 lambda、过滤器filter、映射map

      1)lambda函数:lambda 参数:返回值

      g=lambda x,y:x+y
      print(g(1,2))

      2)过滤器filter--只返回True结果

      help(filter)
      print(list(filter(None,[0,1,2,False,True])))
      print(list(filter(lambda x:x%2,range(0,10,1))))

      3)映射map--返回所有结果

      print(list(map(lambda x:x%2,range(0,10,1))))

    #10 递归分支思想)--函数调用自身+正确的函数终止条件

      1)迭代实现阶乘

      def myeleventhfunc(n):#迭代实现
          result = n
          for i in range(1,n,1):
              result *= i
          return result
      number = 5
      result = myeleventhfunc(number)
      print('%d 的阶乘是:%d'%(number,result))

      2)递归实现阶乘

      def mytwelvethfunc(n):#递归实现
          if n == 1:
              return 1
          else:
              return n*mytwelvethfunc(n-1)
      number = 6
      result = mytwelvethfunc(number)
      print('%d 的阶乘是:%d'%(number,result))

      3)迭代实现菲波那切数列

      def mythirteenthfunc(n):#迭代实现
          n1 = 1
          n2 = 1
          n3 = 1
          if n < 1:
              print('输入错误')
              return -1
          else:
              if n < 3:
                  return 1
              else:
                  for i in range(1,n-1,1):
                      n3 = n1+n2
                      n1 = n2
                      n2 = n3
                  return n3
      n = 3
      result = mythirteenthfunc(n)
      print('菲波那切数列中第%d个数为%d'%(n,result))

      4)递归实现菲波那切数列

      def myfourteenthfunc(n):#递归实现--分支思想

          if n < 1:
              print('输入错误')
              return -1
          if n == 1 or n ==2:
              return 1
          else:
              return myfourteenthfunc(n-1)+myfourteenthfunc(n-2)
      n = 4
      result = myfourteenthfunc(n)
      print('菲波那切数列中第%d个数为%d'%(n,result))

      5)递归实现汉诺塔

      def myfifteenthfunc(n,x1,x2,x3):
          if n == 1:
              print(x1,'--',x3)
          else:
              myfifteenthfunc(n-1,x1,x3,x2)
              print(x1,'--',x3)
              myfifteenthfunc(n-1,x2,x1,x3)
      n = 5
      myfifteenthfunc(n,'a','b','c')

  • 相关阅读:
    find the first t.py file through traversal C: and D:
    gevent simple server
    【python实例】判断是否是回文数
    【python实例】要求输出字符串中最少一个最多八个的所有字符串组合(连续)
    【python实例】统计字符串里大写字母,小写字母和非字母的个数
    【python基础】字符串方法汇总
    【python实例】判断质数:for-break-else
    【python实例】while&for 九九乘法表
    【python实例】水仙花数:每个位上的数值的三次方求和,刚好是这个三位数本身
    【python实例】判断输入年份是否是闰年
  • 原文地址:https://www.cnblogs.com/liuhuan2368935760/p/4660519.html
Copyright © 2011-2022 走看看