前言:
本節將介紹如何自訂函數、調用函數、使用函數庫中的函數
1、自訂函數
a、格式:def + 函數名(參數):
def hello_world(word):
注意,有colon冒號: b、可選的函數注釋
"""Prints 'Hello World!' to the console."""
c、函數體
word = "Hello World!"print(word)
整理上述代碼如下:
def hello_world(word):"""Prints 'Hello World!' to the console."""word = "Hello World!"print(word)
註:為了形式美觀,應該將注釋和函數體與函數定義行加以區分,即利用空格縮排
2、調用函數 函數名(參數)
helloworld("I love Python!")
3、函數調用函數 與C/C++一致,直接在函數內部調用另一個函數
def cube(number): return number*number*numberdef by_three(number): if (number % 3) == 0: return cube(number) else: return False
4、module 函數庫 import module
比如想使用math函數庫,需要在程式前面添加import math
import mathprint math.sqrt(25)#輸入結果為5
上述只為使用均方函數,但使用math.sqrt()較長的運算式,顯得冗長。
可以使用from module import function,即from math import sqrt,簡化調用運算式,即sqrt()即可。
from math import sqrtprint sqrt(25)
5、Universal Imports 若想普通方便調用函數庫中的函數,可以添加from module import *運算式
如想調用math函數庫中的所有函數,可利用下述程式碼
from math import *
6、Built-In Functions
def biggest_number(*args): print max(args) return max(args) def smallest_number(*args): print min(args) return min(args)def distance_from_zero(arg): print abs(arg) return abs(arg)biggest_number(-10, -5, 5, 10)smallest_number(-10, -5, 5, 10)distance_from_zero(-10)
函數分析:
max()——返回整型或浮點型數值集合中的最大值
maximum = max(1,2,3,8.8,9.9,4,7,6,9)print maximum
min()——返回整型或浮點型數值集合中的最小值
minimum = min(1,2,3,0.5)print minimum
abs()——返回輸入數值的絕對值
absolute = abs(-99)print absolute
type()——返回資料的類型
print type(99) #intprint type(99.9) #floatprint type('Python') #str
上述函數都是內建函數,即不需要添加函數庫
7、Review if/elif/else
def shut_down(s): if s == "yes": return "Shutting down" elif s == "no": return "Shutdown aborted" else: return "Sorry"
Modules
import mathprint math.sqrt(13689) #結果是117.0
Built-In Functions
def distance_from_zero(number): if type(number) == int or type(number) == float: return abs(number) else: return "Nope"
8、實戰Program——Taking a Vacation 8.1 Planning Your Trip
def hotel_cost(nights): return 140*nights
8.2 Getting There
def plane_ride_cost(city): if city == "Charlotte": return 183 elif city == "Tampa": return 220 elif city == "Pittsburgh": return 222 elif city == "Los Angeles": return 475
8.3 Transportation
def rental_car_cost(days): if days >= 7: return days*40 - 50 elif days >= 3: return days*40 - 20 return days*40
8.4 Together
def hotel_cost(nights): return 140*nights def plane_ride_cost(city): if city == "Charlotte": return 183 elif city == "Tampa": return 220 elif city == "Pittsburgh": return 222 elif city == "Los Angeles": return 475 def rental_car_cost(days): if days >= 7: return days*40 - 50 elif days >= 3: return days*40 - 20 return days*40 def trip_cost(city,days): return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)
8.5 Plan Your Trip What if we went to Los Angeles for 5 days and brought an extra 600 dollars of spending money?
def hotel_cost(nights): return 140*nights def plane_ride_cost(city): if city == "Charlotte": return 183 elif city == "Tampa": return 220 elif city == "Pittsburgh": return 222 elif city == "Los Angeles": return 475 def rental_car_cost(days): if days >= 7: return days*40 - 50 elif days >= 3: return days*40 - 20 return days*40 def trip_cost(city,days,spending_money): return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money print("Above costs %d dollars!" % trip_cost("Los Angeles", 5, 600))