1. 程式人生 > >python函數語言程式設計一

python函數語言程式設計一

一、pythonorand的使用

  • 1、使用方式

    result = a or b
    result = a and b
    複製程式碼
  • 2、or計算規則

    • a為真的時候就直接返回a的值,否則返回b的值
  • 3、and計算規則

    • a為真的時候返回b,當a為假的時候返回a
  • 4、pythonFalse的值

    • None
    • False
    • 0
    • ''/()/[]
    • {}

二、python中的三木運算

  • 1、格式

    result = 條件為真時返回的結果 if 條件 else 條件為假返回結果
    複製程式碼
  • 2、使用案例

    result = '我是真的' if True else '我是假的'
    複製程式碼

三、python中函式的定義方式

  • 1、常規函式定義方式

    def 函式名(引數):
    	pass
    複製程式碼
  • 2、lambda函式

    賦值接收的變數 = lambda 引數列表...: 函式返回值
    f = lambda x, y: x + y
    複製程式碼

四、函式傳參的幾種形式

  • 1、無參函式

    def foo():
    	pass
    複製程式碼
  • 2、固定引數(形參與實參一一對應)

    def foo(name, age, gender):
    	pass
    
    if __name__ == '__main__'
    : foo('張三', 20, '男') 複製程式碼
  • 3、可變引數(python中習慣使用關鍵詞*args來表示)

    在函式體中接收的是一個tuple元祖

    def foo(*args):
    	print(args)
    
    if __name__ == '__main__':
    	foo(1,2,3,4,5)
    複製程式碼
  • 4、關鍵字引數(python中習慣使用關鍵詞**kwargs來表示)

    關鍵字引數傳遞到函式中是以dict的方式展示

    def bar(**kwargs):
        print(type(kwargs))
        print(kwargs)
    
    if
    __name__ == '__main__': bar(name='張三', gender='男', age=20) # <class 'dict'> #{'name': '張三', 'gender': '男', 'age': 20} 複製程式碼
  • 5、固定引數、可變引數、關鍵字引數混合使用

    原理

    • 1.先會把實參不能少於形參固定引數的個數(少於會直接報錯)
    • 2.把實參優先給固定引數,多餘的給可變引數
    • 3.如果使用了關鍵詞傳遞的引數就是關鍵字引數中
    def test(name, age, *args, **kwargs):
        print(name, age)
        print(args)
        print(kwargs)
    
    if __name__ == '__main__':
        test(1, 2, 3, 4, 5, address='廣東', mobile = '110')
    複製程式碼
  • 6、預設引數

    def test(name, age=20):
        print(name, age)
    
    if __name__ == '__main__':
        test('哈哈')
    複製程式碼

五、函式的返回值

  • 1、如果沒指定函式返回就返回None
  • 2、函式使用return的返回值只有一個
  • 3、如果一個函式可以返回一個列表、元祖、字典
  • 4、函式可以使用yield返回多個值

六、python中常見的內建函式

  • 1、map函式(遍歷可迭代物件返回一個可迭代物件)

    • 使用格式

      map(函式, iterable...)
      複製程式碼
    • 使用案例

      list1 = [1, 2, 3, 4, 5]
      list2 = [6, 7, 8, 9, 10]
      
      result1 = map(lambda x: x ** 2, list1)
      print(list(result1))
      
      result2 = map(lambda x, y: x + y, list1, list2)
      print(list(result2))
      
      tuple1 = (1, 2, 3, 4)
      print(list(map(lambda x: x ** 2, tuple1)))
      複製程式碼
  • 2、filter函式(過濾資料)

    • 使用格式

      filter(函式, iterable...)
      複製程式碼
    • 使用案例

      list1 = [1, 2, 3, 4, 5]
      print(list(filter(lambda x: x % 2 == 0, list1)))
      print([x for x in list1 if x % 2 == 0])
      複製程式碼
    • 過濾資料推薦使用列表推導式

      list1 = [1, 2, 3, 4, 5]
      print([x for x in list1 if x % 2 == 0])
      複製程式碼
  • 3、reduce函式(連續計算)注意需要先導包

    • 1、定義格式

      from functools import reduce
      
      result = reduce(函式, iterable, 初始化值)
      複製程式碼
    • 2、使用案例

      from functools import reduce
      
      if __name__ == "__main__":
          list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
          result = reduce(lambda x, y: x + y, list1)
          print(result)
          result2 = reduce(lambda x, y: str(x) + str(y), list1, '==')
      複製程式碼
  • 4、sorted排序函式

    • 列表排序

      sorted([36, 6, -12, 9, -22])  列表排序
      複製程式碼
    • 字元排序

      sorted(['bob', 'about', 'Zoo', 'Credit'])  #字串排序,按照ASCII的大小排序
      複製程式碼
    • 逆向排序(相當於先升序排序後,然後在反向)

      sorted([36, 6, -12, 9, -22], reverse=True)
      複製程式碼
    • 新增函式排序使用關鍵引數key(注意這個地方的函式只能傳遞一個引數的函式)

      L = [{1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}]
      print(sorted(L, key=lambda x: len(x)))
      複製程式碼
    • key函式需要傳遞多個函式的時候

      from functools import cmp_to_key
      
      if __name__ == "__main__":
          L = [{1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}]
          print(sorted(L, key=cmp_to_key(lambda x, y: x[1] > y[1])))
      複製程式碼
    • 元祖、字典使用排序

      a = [('b', 2), ('a', 1), ('c', 0)]
      print(sorted(a, key=lambda x: x[0]))
      print(sorted(a, key=lambda x: x[1]))
      
      b = [{'city': '深圳', 'tem': 40}, {'city': '廣州', 'tem': 30}]
      print(sorted(b, key=lambda x: x['tem']))
      複製程式碼

七、更多關於作者文章請訪問