Python basis (six) function

.function

It is a function of the operation of the package

2.1 Basic Structure Function

#函数的定义
def 函数名():
    #函数提
    pass

#函数的执行
函数名()

2.2 Parameter acquaintance

         #形参
def hanshu(aaa):        #参数相当于变量来接受
    v=[11,22,33]
    print(v[aaa])

hanshu(1)   #实参

2.3 Return value

  1. The return value indicates a return:

    1. End of the function.

      l1 = [1, 2, 3]
      def new_len():
          print(111)
          print(222)
          if 1 == 1:
              return
          print(333)
          print(444)
      new_len()
    2. Function does not return or write only a return, executive function get is None.

      # l1 = [1, 2, 3]
      # def new_len():
      #     count = 0
      #     for i in l1:
      #         count += 1
      #     return
      # print(new_len())
    3. Followed by a single function return value, the function performed by this value is obtained (type value is not changed).

    def func():
        print(111)
        # return 100
        # return [1, 2, 3]
        return {'name': '太白'}
    ret = func()
    print(ret, type(ret))
    1. Followed by a plurality of function return value, the function performed by a tuple is obtained.

      # def func():
      #     print(111)
      #     return 1, '23期', [22, 33]
      # ret = func()
      # print(ret, type(ret))  # (1, '23期', [22, 33])
      
      # def func():
      #     print(111)
      #     # return 1, '23期', [22, 33]
      # a,b,c = func()
      # print(a,b,c)
      
      
      def func():
          print(111)
          # return 1+1+2
          return 2 > 1
      ret = func()
      print(ret)

2.4 The foregoing summary

def f1():
    pass        #第一种
f1()

def f2(a1):
    pass        #第二种
f2(123)

def f3():
    return 1    #第三种
v1 = f3()

def f4(a1,a2):
    return  a1+a2   #第四种
v1=f4( )

Guess you like

Origin www.cnblogs.com/llwwhh/p/11090372.html