函數是一種工具,能夠重複調用python
一、防止代碼冗餘框架
二、加強代碼可讀性函數
def 函數名(變量1,變量2):工具
""" 函數註釋描述 """code
函數體class
return值變量
函數都是先定義後調用,定義時只檢測語法不執行代碼語法
函數名的命名規範跟變量名的命名規範相同命名
一、直接調用 函數名() 例如:index()數據
二、從新賦值函數名
f = 函數名
f()
三、函數當參數傳入函數中
index(a,index())
函數都是先定義後調用,定義時只檢測語法不執行代碼
是一個函數結束的標誌,函數體代碼只要運行到return則函數執行結束
一、不用寫返回值return,默認返回值是None
def index(): print("hello word") print(index()) >>> hello word >>> None
二、只寫return只有結束函數的效果 ,返回值是None
def index(): print("hello word") return print(index()) >>> hello word >>> None
三、寫return None也是結束函數的效果跟只寫return相同,返回值是None
四、return返回一個值
能夠將返回結果當作一個變量使用
def index(a,b): if a>b: return a else: return b print(index(1,3))
五、return返回多個值
1. 將返回值的多個值默認存入元組返回 2. 函數的返回值不想被修改 3. 能夠return+數據,本身指定
def func1(): return 1, "2" print(func1()) >>>(1, '2') def func(a, b, c, d, e): return [a, b, c, d, e] print(func(a, b, c, d, e)) >>> [1, 2, '3', [4, 5], {'name': 'sean'}]