python新手入門 函式

2021-09-23 15:44:00 字數 985 閱讀 9860

2019/5/25 魚c教程19、20課

全域性變數與區域性變數

def discounts(price,rate):

final_price = price * rate

return final_price

old_price = float(input('請輸入原價:'))

rate = float(input('請輸入折扣率:'))

new_price = discounts(old_price,rate)

print('打折後**是:',new_price)

count = 5  #全域性

def myfun():

count = 10 #區域性

print(10)

myfun(count)

5 #優先全域性

count = 5 

def myfun():

global count = 10 #global強制全域性

print(10)

myfun(count)

函式巢狀閉包

def funx(x):

def funy(y):

return x*y

return funy

funx(8)(5)

40i = fun(8)

i=

關鍵字: nonlocal->強制轉換為非區域性變數

Python新手入門之函式

函式的定義 函式就是執行特定的任務和特定功能的一段 函式的作用 復用 隱藏實現細節 提高可維護性 提高可讀性便於除錯 函式的建立 def 函式名 輸入引數 函式體 return 函式的引數傳遞 def sun a,b print a a print b b return a b print sun ...

python新手入門

1.先看下python的基礎,然後去看 ng的機器學習課程,最後結合周志華的機器學習和 機器學習實戰兩本書 2.盡量不要直接呼叫工具箱,自己去實現演算法 3.先安andconda,再安ide就可以了,pycharm 4.4,測試anaconda,桌面 開始 右下角執行 ipython第乙個 執行 5...

Python新手入門之函式(一)

1.1 定義函式 使用關鍵字def告訴python你要定義函式,向python指出了函式名,還可能在括號內指出函式為完成其任務需要什麼樣的資訊。簡單示例如下 文件字串用三引號括起,python使用它們來生成有關程式中函式的文件。行print hello 是函式體內的唯一一行 greet user 只...