Python学习——Functions

前言:

本节将介绍如何自定义函数、调用函数、使用函数库中的函数


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*number

def by_three(number):
    if (number % 3) == 0:
        return cube(number)
    else:
        return False

4、module 函数库

import module
比如想使用math函数库,需要在程序前面添加import math
import math
print math.sqrt(25)
#输入结果为5

上述只为使用均方函数,但使用math.sqrt()较长的表达式,显得冗长。
可以使用from module import function,即from math import sqrt,简化调用表达式,即sqrt()即可。
from math import sqrt
print 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)        #int
print type(99.9)      #float
print 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 math
print 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))


你可能感兴趣的:(Python,python)