今日內容python
參數app
做用域函數
函數嵌套code
知識點回顧utf-8
函數基本結果作用域
def func(name,age,email): # 函數體(保持縮進一致) a = 123 print(a) return 1111#函數中,出現return後,後面的語句再也不執行。 b = 456 print(b) result = func(1,2,3) # 函數默認返回值:None
參數input
def func(n1,n2): print(n1,n2) func(1,2) func(1,[11,22,3]) func({'k1':'k'},[11,22,3]) # 嚴格按照順序傳參數:位置方式傳參。 # 實際參數能夠是任意類型。
返回值it
函數內部執行過程當中遇到return,就終止。
def func1():
return "完成" # 函數每次執行到此,就返回;因此下面代碼永遠不執行。
for i in range(10):
print(i)
func1()email
def func2(): for i in range(10): print(i) return "完成" print(666) func2()
return 能夠返回任意類型
def func():
return (1,2,3)變量
v = func() print(v) # 特殊:返回元組 def func(): return 5,8,"alex" v = func() print(v)
昨日做業題
def func(data_list): val = data_list[1::2] return list(val) v1 = func([1,2,3,4,5,5]) print(v1) v2 = func((1,2,3,4,5,5)) print(v2) # 其餘類型能夠經過強制轉換爲列表 # v1 = (1,2,3,4) # v1 = {1,2,3,4} # v1 = {'k1':'v1','k2':'v2'} v1 = "asdfasdfasdf" v2 = list(v1) print(v2) def func(arg): if len(arg) > 5: return True else: return False data = func([1111,22,3,42,12]) data = func((1111,22,3,42,12) def func(a1,a2): if a1 > a2: return a1 else: return a2 v1 = func(1,2) v2 = func(11,2) def func(a1,a2): return a1 if a1 > a2 else a2 v1 = func(1,2) v2 = func(11,2) def func(name,gender,age,edu): # template = "%s*%s*%s*%s" %(name,gender,age,edu,) # return template data_list = [name,gender,age,edu] return "*".join(data_list) n1 = input('>') n2 = input('>') n3 = input('>') n4 = input('>') result = func(n1,n2,n3,n4) print(result) def func(max_range): result = [1,1] while True: val = result[-1] + result[-2] if val > max_range: break result.append(val) return result v = func(100) print(v) def func(name): with open('data.txt',mode='r',encoding='utf-8') as obj: # 方式一 content = obj.read() flag = False row_list = content.split('\n') # ['1|alex|123123','2|eric|rwerwe','3|wupeiqi|pppp'] for row in row_list: v= row.split('|') if v[1] == name: flag = True break return flag func('alex') func('eric') # ############################################ def func(name): with open('data.txt',mode='r',encoding='utf-8') as obj: # 方式一 content = obj.read() row_list = content.split('\n') # ['1|alex|123123','2|eric|rwerwe','3|wupeiqi|pppp'] for row in row_list: v= row.split('|') if v[1] == name: return True v1 = func('alex') if v1: print('存在') else: print('不存在')
今日內容詳細
參數
任意類型
def func(a1,a2,a3):
print(a1,a2,a3)
位置傳參(調用函數並傳入參數)【執行】
def func(a1,a2):
print(a1,a2)
func(1,3)
關鍵字傳參【執行】
def func(a1, a2):
print(a1, a2)
func(a2=99,a1=2) # 關鍵字傳參數和位置傳參能夠混合使用(位置傳入的參數 > 關鍵字參數在後 = 總參數個數) def func1(a1, a2, a3): print(a1, a2, a3) # func(1, 2, a3=9) # func(1, a2=2, a3=9) # func(a1=1, a2=2, a3=9) # func(a1=1, 2,3) # 錯誤
默認參數【定義】
def func(a1,a2,a3=9,a4=10):
print(a1,a2,a3,a4)
func(11,22) func(11,22,10) func(11,22,10,100) func(11,22,10,a4=100) func(11,22,a3=10,a4=100) func(11,a2=22,a3=10,a4=100) func(a1=11,a2=22,a3=10,a4=100)
調用函數無
def func(args):
print(args)
func(1,2,3,4) (1, 2, 3, 4)
調用函數有
def func(args):
print(args)
func(*(1,2,3,4)) func(*[1,2,3,4]) (1, 2, 3, 4) (1, 2, 3, 4)
只能用位置傳參
def func(*args):
print(args)
# func(1) # func(1,2) func(1,2) # args=(1, 2) func((11,22,33,44,55)) # args=((11,22,33,44,55),),不帶星號,至關於把噹噹成一個總體,看成元組的一個元素。 func(*(11,22,33,44,55)) # args=(11,22,33,44,55) (1,) (1, 2) (1, 2) ((11, 22, 33, 44, 55),) (11, 22, 33, 44, 55)
調用函數無
def func(kwargs):
print(kwargs)
func(k1=1,k2="alex") {'k1': 1, 'k2': 'alex'}
綜合應用:無敵 + 無敵 => 真無敵
def func(*args,**kwargs):
print(args,kwargs)
# func(1,2,3,4,5,k1=2,k5=9,k19=999) 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}) (1, 2, 3, 4, 5) {'k1': 2, 'k5': 9, 'k19': 999} (1, 2, 3) {'k1': 2, 'k5': 9, 'k19': 999} (1, 2, 3) {'k1': 1, 'k2': 3} (111, 222, 1, 2, 3) {'k11': 'alex', 'k1': 1, 'k2': 3}參數相關重點:
定義函數
def func1(a1,a2):
pass
def func2(a1,a2=None):
pass
def func3(*args,**kwargs):
pass
調用函數
位置參數 > 關鍵字參數
做用域
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 88888 2 666 88888 2
做用域中查找數據規則:優先在本身的做用域找數據,本身沒有就去 "父級" -> "父級" -> 直到全局,所有麼有就報錯。注意:父級做用域中的值究竟是什麼?
x = 10
def func():
x = 9
print(x)
func()
練習題:
#!/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() # x = 10 # def func(): # x = 9 # print(x) # def x1(): # x = 999 # print(x) # print(x) # x1() # # func() # x = 10 # def func(): # x = 8 # print(x) # def x1(): # x = 999 # print(x) # x1() # print(x) # # func() # x = 10 # def func(): # x = 8 # print(x) # def x1(): # print(x) # x1() # print(x) # # func() # x = 10 # def func(): # x = 8 # print(x) # def x1(): # print(x) # x = 9 # x1() # x = 10 # print(x) # # func() # # x = 10 # def func(): # x = 8 # print(x) # def x1(): # print(x) # # x1() # x = 9 # x1() # x = 10 # print(x) # # func()
子做用域中只能 找到父級中的值 ,默認沒法從新爲父級的變量進行賦值。(global/nonlocal能夠強制作)
# #####################
name = 'oldboy'
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) # ############################## nonlocal name = "老男孩" def func(): name = 'alex' def inner(): nonlocal name # 找到上一級的name name = 999 inner() print(name) func() print(name)
總結
補充
所有變量之後必須所有是大寫
USER_LIST = [11,22,3]
def func(): name = 'asdf' USER_LIST.append(12) USER_LIST.append(name) func() print(USER_LIST)