1. 程式人生 > >程式設計小白的第一本Python入門書(2)

程式設計小白的第一本Python入門書(2)

這一文主要講一件函式

1、Python3.5中有68個內建函式(Python自帶的函式),我們已經接觸到了len()、int()等

2、Python區分中英文冒號,一定要注意(相信很多程式語言都一樣)

3、Python的函式通過縮排來表示語言和邏輯的從屬關係(這一點和很多語言不一樣,例如C語言、VBA都是通過“{ }”來表示從屬)

4、一個函式例項

def fahrenheit_converter(C): #攝氏轉華氏溫度
    fahrenheit = C*9/5+32
    return str(fahrenheit)+'F'
print(fahrenheit_converter(35))

5、傳遞引數的兩種方式

def trapezoid_area(base_up,base_down,height):
    return 0.5 * (base_up+base_down)*height

print(trapezoid_area(1,2,3)) #位置引數
print(trapezoid_area(base_up=1,base_down=2,height=3)) #關鍵詞引數

6、給函式定義預設引數

def trapezoid_area(base_up,base_down,height=2):
    return 0.5 * (base_up+base_down)*height

print(trapezoid_area(1,2)) #位置引數
print(trapezoid_area(1,2,height=3)) #關鍵詞引數