函数
(一)函数是如何定义的
在python中函数是使用关键字def创建的,主要是为了做简单的,重复的事情,解决问题来创建的。如下例:
# this one is like your scripts with argv
def print_two(*args):
arg1,arg2 = args # 这个函数的参数与第二个函数的参数不一样
print(f"arg1: {arg1}, arg2: {arg2}")
# ok,that *args is actually pointless,we can just do this
def print_two_again(arg1,arg2):#与第一个参数形式不大一样
print(f"arg1: {arg1},arg2: {arg2}")
# this just takes one argument
def print_one(arg1):
print(f"arg1: {arg1}")
#this one takes no arguments
def print_none():
print("I got nothin.")
print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
输出:
arg1: Zed, arg2: Shaw
arg1: Zed,arg2: Shaw
arg1: First!
I got nothin.
[Finished in 171ms]
注意函数的定义格式,函数的调用格式。
(二)函数与变量
函数里的变量与脚本里的变量没有关系,即使变量名称一样,最好不使用相同变量。
def cheese_and_crackers(cheese_count,boxes_of_crackers):
print(f"You have {cheese_count} cheeses!")
print(f"You have {boxes_of_crackers} boxes of crackers!")
print("Man that's enough for a party!")
print("Get a blanket.\n")
print("We can just give the function numbers directly: ")
cheese_and_crackers(20,30)
print("OR,we can use variables from our scripts:")
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese,amount_of_crackers)
print("We can even do math inside too:")
cheese_and_crackers(10+20,5+6)
print("And we can combine the two,variables and math:")
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers +1000)
输出:
We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.
OR,we can use variables from our scripts:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.
We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.
And we can combine the two,variables and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.
[Finished in 131ms]
这个函数展示了给函数赋值的几种方式。
(三)函数与文件
注意函数如何与文件一起工作的
from sys import argv
script,input_file = argv
def print_all(f):
print(f.read())
def rewind(f):
f.seek(0)
def print_a_line(line_count,f):
print(line_count,f.readline())
current_file = open(input_file)
print("First let's print the whole file:\n")
print_all(current_file)
print("Now let's rewind,kind of like a tape.")
rewind(current_file)
print("let's print three lines:")
current_line = 1
print_a_line(current_line,current_file)
current_line = current_line + 1
print_a_line(current_line,current_file)
current_line = current_line + 1
print_a_line(current_line,current_file)
输出为:
First let's print the whole file:
This is stuff Ityped into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Now let's rewind,kind of like a tape.
let's print three lines:
1 This is stuff Ityped into a file.
2 It is really cool stuff.
3 Lots and lots of fun to have in here.
使用此函数需要给函数输入一个文件作为参数。
(四)函数返回值
举例如下:
def add(a,b):
print(f"ADDING {a} + {b}")
return a+b
def subtract(a,b):
print(f"SUBTRACTING {a} - {b}")
return a-b
def multiplay(a,b):
print(f"MULTIPLAYING {a} * {b}")
return a * b
def divide(a,b):
print(f"DIVIDING {a} / {b}")
return a / b
print("Let's do some math with just functions!")
age = add(30,5)
height = subtract(78,4)
weight = multiplay(90,2)
iq = divide(100,2)
print(f"Age:{age},Height:{height}, Weight:{weight},IQ:{iq}")
# A puzzle for the extra credit,type it in anyway.
print("Here is a puzzle.")
what = add(age,subtract(height,multiplay(weight,divide(iq,2))))
print("That becomes: ",what,"Can you do it by hand?")
输出:
Let's do some math with just functions!
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLAYING 90 * 2
DIVIDING 100 / 2
Age:35,Height:74, Weight:180,IQ:50.0
Here is a puzzle.
DIVIDING 50.0 / 2
MULTIPLAYING 180 * 25.0
SUBTRACTING 74 - 4500.0
ADDING 35 + -4426.0
That becomes: -4391.0 Can you do it by hand?
[Finished in 136ms]
理解函数的从里到外思维,注意函数的返回值以及调用。
|