Python學習——Functions__Python

來源:互聯網
上載者:User
前言:

本節將介紹如何自訂函數、調用函數、使用函數庫中的函數


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))


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.