到目前爲止,咱們一直所接觸的都是屬於面向過程編程,這樣的代碼會下降代碼的可讀性,所以引入了函數式編程,在後面咱們還會學到面向對象編程。python
函數本質:將N行代碼拿到別處,並給他起個名字,之後經過名字就能夠找到這段代碼並執行。編程
應用場景:代碼重複執行,代碼量特別多超過一屏,能夠選擇函數進行代碼的分割app
def 函數名(): # 函數內容 函數名() #執行函數 注:若是函數沒被調用,則內部代碼永不執行
v1 = [1,2,3,4] v2 = tuple(v1) v1 = (1,2,3,4) v2 = list(v1)
def func(a,b,c): pass func(1,2,3)
def func(a,b,c): pass func(k1 = 1,k2 = 2,k3 = 3)
def func(a,b = 3): pass func(11) # 能夠只寫一個值,則另一個默認就是3 func(2,3) # 也能夠寫兩個值,這樣就是把2賦值給a,把3賦值給b,b不在取默認值
*args :不支持關鍵字傳參,只能傳位置參數,能夠接收n個位置參數,而且將參數轉化爲元組函數式編程
調用函數無 *:函數
def func(*args): print(args) func(1,2,3...) # 不支持關鍵字傳參,只能傳位置參數,能夠接收n個位置參數,而且將參數轉化爲元組
調用函數有 *:ui
def func(*args): print(args) func(*(1,2,3,4)) func(*[1,2,3,4])
**kwargs :不支持位置傳參,只能關鍵字傳參,能夠接收n個關鍵字參數,而且轉化爲字典code
調用函數無**:對象
def func(**kwargs): print(kwargs) func(k1=1,k2=2,k3=3...) # 不支持位置傳參,只能關鍵字傳參,能夠接收n個關鍵字參數,而且轉化爲字典
調用函數有**:索引
def func(**kwargs): print(kwargs) func(**{'k1':'alex','k2':'eric'}) # kwargs={'k1':'v2','k2':'v2'}
def func(*args,**kwargs): print(*args,**kwargs) func(1,2,3,k1=1,k2=3,k3=5) func(*[1,2,3],k1=2,k5=9,k19=999) func(*[1,2,3],**{'k1':1,'k2':3}) func(111,222,*[1,2,3],k11='alex',**{'k1':1,'k2':3})
定義函數:ip
# 第一種 def func(a,b): pass func(1,2) # 第二種 def func(a,b=None): pass func(1) # 第三種 def func(*args,**kwargs): pass func(1,k1=1)
調用函數
def get_list_first_data(aaa): # aaa叫形式參數(形參) v = [11,22,33,44] print(v[aaa]) get_list_first_data(1) # 2/2/1調用函數時傳遞叫:實際參數(實參)
練習: # 1. 請寫一個函數,函數計算列表 info = [11,22,33,44,55] 中全部元素的和。 def value(x): var = 0 for i in x: var += i print(var) v1 = value([11,22,33,44,55])
# 2. 請寫一個函數,函數將兩個列表拼接起來。 def value(x,y): list1 = [] list1.extend(x) list1.extend(y) print(list1) value([1,2,3],['alex','eric'])
# 3. 計算一個列表的長度 def length(x): var = 0 for i in x: var += 1 print(var) length([1,2,3,4,5])
def func(arg): # .... return 9 # 返回值爲9 默認:return None val = func('adsfadsf')
# 練習 1.寫函數,計算一個列表中有多少個數字,打印: 列表中有%s個數字。 提示:type('x') == int 判斷是不是數字。 def num(list): var = 0 for i in list: if type(i) == int: var += 1 return var v1 = num(['sdf',1,2,3,'sdf']) print(v1)
# 2. 寫函數,計算一個列表中偶數索引位置的數據構形成另一個列表,並返回。 def double(list): var = [] for i in range(0,len(list)) : if i%2 == 0: var.append(list[i]) return var v1 = double([1,2,3,4,5,6]) print(v1)
# 3. 讀取文件,將文件的內容構形成指定格式的數據,並返回。 """ a.log文件 alex|123|18 eric|uiuf|19 ... 目標結構: a. ["alex|123|18","eric|uiuf|19"] 並返回。 b. [['alex','123','18'],['eric','uiuf','19']] c. [ {'name':'alex','pwd':'123','age':'18'}, {'name':'eric','pwd':'uiuf','age':'19'}, ] """ a: def var(): list = [] with open('a.log',mode='r',encoding='utf-8') as f: for line in f: list.append(line) return list v1 = var() print(v1) b: def var(): list = [] with open('a.log',mode='r',encoding='utf-8') as f: for line in f: line = line.strip() new_line = line.split("|") list.append(new_line) return list v1 = var() print(v1) c: def var(): list = [] with open('a.log',mode='r',encoding='utf-8') as f: for line in f: line = line.strip() new_line = line.split("|") a,b,c = new_line info = {} info['name'] = a info['pwd'] = b info['age'] = c list.append(info) return list v1 = var() print(v1)
Python中:
a = 1 def s1(): x1 = 666 print(x1) print(a) print(b) b = 2 print(a) s1() a = 88888 def s2(): print(a,b) s1() s2() #打印結果 1,666,1,2,8888,2,666,8888,2
總結:
練習題 !/usr/bin/env python -*- coding:utf-8 -*- x = 10 def func(): x = 9 print(x) def x1(): x = 999 print(x) func() x = 10 def func(): x = 9 print(x) def x1(): x = 999 print(x) x1() func() # 9,999 x = 10 def func(): x = 9 print(x) def x1(): x = 999 print(x) print(x) x1() func() #9,9,999 x = 10 def func(): x = 8 print(x) def x1(): x = 999 print(x) x1() print(x) func() #8,999,8 x = 10 def func(): x = 8 print(x) def x1(): print(x) x1() print(x) func() # 8,8,8 x = 10 def func(): x = 8 print(x) def x1(): print(x) x = 9 x1() x = 10 print(x) func() # 8,9,10 x = 10 def func(): x = 8 print(x) def x1(): print(x) x1() x = 9 x1() x = 10 print(x) func() # 8,8,9,10-
name = 'dali' def func(): name = 'alex' # 在本身做用域再建立一個這樣的值。 print(name) func() print(name) # ##################### name = [1,2,43] def func(): name.append(999) print(name) func() print(name) # ###################### 若是非要對全局的變量進行賦值 # 示例一 name = ["大慄",'alex'] def func(): global name name = '我' func() print(name) # 示例一 name = "大慄" def func(): name = 'alex' def inner(): global name name = 999 inner() print(name) func() print(name) # ##################### name = "大慄" def func(): name = 'alex' def inner(): global name name = 999 inner() print(name) func() print(name) # ##################### name = "大慄" def func(): name = 'alex' def inner(): nonlocal name # 找到上一級的name name = 999 inner() print(name) func() print(name)
之後全局變量都要大寫,局部能夠小寫
主要對於函數的參數、返回值、以及函數的做用域作了簡單的介紹和分享。