Python第四节FUNCTIONS学习汇总。

来源:互联网 发布:webshell查杀工具 编辑:程序博客网 时间:2024/05/21 04:40

四、functions

(一)functions

1.可以重复使用

2.function junction
functions are defined with three components:

01.the header , which includes the 【def】keyword , the name of the function,and any parameters参数 the function requires. (圆括号必须有,如无变量就直接def():)
eg: def hello_world(): // there are no parameters

02.an optional comment注解 that explains what the function does:
eg: “”“prints ‘Hello World!’ to the console.”“”【三个引号注解用】

03.the body , which describes the procedures the function carries out.the body is indented (新段落首行缩进), just like for conditional statements.
eg: print ”Hello World!”
eg: def hello_world():
“”“prints ‘Hello World!’ to the console.”“”
print ”Hello World!”

3.call and response
after defining a function , it must be called to be implemented.【用call来“调用”函数】
eg: def square(n):
“”“Returns the square of a number.”“”
squared = n**2 【**两个是指求平方】
print “%d squared is %d.” % (n, squared)
return squared

4.parameters and arguments 【p:参数 arguments:具体数值】
power 【求幂】power(base底数 , exponent指数)

5.functions calling functions 【函数调用函数】
eg.: def fun_one(n):
return n * 5
def fun_two(m):
return fun_one(m) + 7

例:
First, def a function called cube that takes an argument called number. Don’t forget the parentheses【圆括号】 and the colon【冒号】!
Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).【cube=立方】
Define a second function called by_three that takes an argument called number.
if that number is divisible【除尽】by 3, by_three should call cube(number)and return its result. Otherwise, by_three should return False.

def cube(number):
return number * number * number 【return 后面跟一个值 不能跟cube = …】

def by_three(number):

if number % 3 == 0:     【只能是number来除尽3 不能是by_three来计算】    return cube(number)else:    return False

5.i know kung fu
(module) 【程序块、模块】is a file that contains definitions—including variables and functions—that you can use once it is imported. 【import之后可以多次使用】

6.generic imports 【普通的import】
python module—【math】
in order to access math , all you need is the import keyword.
insert 插入
eg: import math
print math.sqrt(25)

7.function imports
pulling in just a single function from a module is called a function imports, and it’s done with the 【from】keyword:
from module import function
now you can just type sqrt() to get the square root of a number — no more math.sqrt()

eg: from math import sqrt 【此时不需要()在后面】

8.universal imports 【全体的】
from module import *
eg : from math import * 【这个*跟数据库里面的一样的一样 全部信息】

9.here be dragons
8有弊端,不安全,如果你有sqrt函数,and you import math,函数是安全的,但是如果你import * ,又会有问题,two different functions with the exact same name. if you import* from several modules at once, you won’t be able to figure out with variable or function came from where
it’s best to stick with either 【import module and type module.name 】or【 just import specific variables and functions from various modules as needed】.

e.g.: import math # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything # Prints ‘em all!

10.on beyond strings
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)

11.max()
最大值
一般用整数或浮点数,不要用其他格式

12.min()
最小值

13.abs()
returns the 【absolute value】of the number it takes as an argument— that is , that number’s distance from 【0】on an imagined number line.
for instance , 3 and -3 both have the same absolute value: 3
一般是正值,it only takes a single number. 绝对值

14.type()
returns the 【type】of the data it receives as an argument.
输出格式:int float strings(str)

eg: print type(42)
就会出现type ‘int’

15.review
def shut_down(s): 【冒号一定要有 不能忘记】
if s == “yes”:
return “Shutting down”
elif s == “no”:
return “Shutdown aborted”
else:
return “Sorry”

16.review : modules
例: Import the math module in whatever way you prefer. Call its sqrt function on the number 13689 and print that value to the console.
有三种方式:(1) import math
print math.sqrt(13689)
(2) from math import sqrt
print sqrt(13689)
(3) from math import *
print sqrt(13689)

17.review: built-in functions
def distance_from_zero(s):
if type(s) == int or type(s) == float: 【int 和float不能加引号】
return abs(s) 【不能写成 type(s) == int or float】
else:
return “Nope”

18.在def中 无论有没有变量,都要加圆括号,如果有变量 就放在圆括号里面,用逗号隔开 ,且在最后要加上冒号

19.def里面的括号里面有什么,return里面圆括号里面的值也只能是那些,
例如: def trip_cost(city,days):

return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city)所以hotel_cost(nights)的nights就必须改为days (玩多少天就住多少个晚上)

20.输出格式:
def trip_cost(city,days,spending_money):

return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money

print trip_cost(“Los Angeles”,5,600)

0 0