函数【1、功能性;2、隐藏性;3、避免编写重复的代码;4、组织代码】
定义函数时,函数可以没有参数;可以输入return返回函数的结果
函数名不要与内置函数名相同,否则会出现RecursionError
#内置函数
a = 1.12345
print(round(a,2)) #round()函数中第一个参数写入变量,第二个参数写入保留的小数位个数
#输出结果:1.12 「保留两位小数」
a = 1.12345
print(round(a,4)) #round()函数实行四舍五入的规则
#输出结果:1.1235 「保留4位小数」
#vscode终端命令行输入python3,然后输入help(函数名)即可查看函数的定义;输入import this即可查看python之禅
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly. 美胜于丑【优雅】
Explicit is better than implicit. 明确胜于含蓄【明确】
Simple is better than complex. 简单胜于复杂【简单】
Complex is better than complicated. 复杂胜于复杂化
Flat is better than nested. 扁平胜于嵌套【扁平,嵌套不要太多】
Sparse is better than dense. 间隔胜于紧凑【空格】
Readability counts. 要考虑可读性【可读性】
Special cases aren't special enough to break the rules. 特例不足以特别到打破规则【规则】
Although practicality beats purity. 虽然实用性重于简洁性
Errors should never pass silently. 从不应默默放过错误
Unless explicitly silenced. 除非已明确地解决
In the face of ambiguity, refuse the temptation to guess. 面对含糊不清的语句,拒绝猜测
There should be one-- and preferably only one --obvious way to do it. 应该存在一个明显的方法(最好只有一种)去处理
Although that way may not be obvious at first unless you're Dutch. 虽然这一方法可能不是一开始就显而易见,除非你是Python之父
Now is better than never. 现在行动胜于从不行动
Although never is often better than *right* now. 虽然从不行动经常优于马上就行动
If the implementation is hard to explain, it's a bad idea. 如果执行的操作难以解释,那意味着是一个糟糕的方案
If the implementation is easy to explain, it may be a good idea. 如果执行的动作容易解释,那可能是一个不错的主意
Namespaces are one honking great idea -- let's do more of those! 命名空间是一个绝妙的理念,多加利用!【命名】
import sys
sys.setrecursionlimit() #括号内设置递归「程序调用自身的编程技巧」的限制次数
#目标:1)实现两个数字的相加;2)打印输入的参数
#函数的定义
def add(x,y):
result = x + y
return result #不输入result,会默认返回空值None
def print_result(code): #函数名不要与内置函数名相同
print(code) #输不输入return,都会默认返回空值None
return
print('over') #return后面的语句不会再执行
a = add(1,2)
b = print_result('hold on')
print(a,b) #会打印在同一行
#输出结果:
#hold on #print_result()函数内置打印功能
#3 None 「add()函数有返回结果,但print_result()函数没有返回结果」
#函数的定义
def add(x,y):
result = x + y
return #不输入result,会默认返回空值None
def print_result(code): #函数名不要与内置函数名相同
print(code) #输不输入return,都会默认返回空值None
print('over') #不写入return,后面的语句才会执行
a = add(1,2)
b = print_result('hold on')
print(a,b) #会打印在同一行
#输出结果:
#hold on #print_result()函数内置打印功能
#over #print_result()函数内置的打印功能
#None None #「add()函数和print_result()函数没有返回结果,默认返回空值None」
#局部变量和全局变量「局部变量不会覆盖全局变量」
secret = 'sleep more' #全局变量:在所有函数之外赋值的变量,可以在程序的任何位置使用(全局作用域)
def how_to_succeed():
base1 = 'eat less,' #局部变量:在函数内部赋值的变量,仅能在该函数内部使用(局部作用域)
base2 = 'exercise more,' #局部变量:在函数内部赋值的变量,仅能在该函数内部使用(局部作用域)
base3 = 'drink more' #局部变量:在函数内部赋值的变量,仅能在该函数内部使用(局部作用域)
secret = base1 + base2 +base3 #局部变量:在函数内部赋值的变量,仅能在该函数内部使用(局部作用域)
print(secret)
how_to_succeed()
print(secret)
#输出结果:「局部变量不会覆盖全局变量」
#eat less,exercise more,drink more
#sleep more
参数:1)必须参数「定义时的参数—形式参数(形参);调用时的参数—实际参数(实参),必须参数调用时实参位置要与定义时的形参一致」;2)关键字参数「调用时指定实参对应的形参」(增加代码的可读性);3)默认参数「定义函数时某些不会变动的参数可以设置为默认参数;若在调用时需要更改定义时的默认参数,直接在调用时传递实参即可」
注意事项📋
- 必须参数和关键字参数:定义了多少个形参,就要传递多少个实参
- 调用时关键字参数在位置上可以不与形参在位置上一一对应「因为已经指定哪一个实参与哪一个形参对应」
- 默认参数放在必须参数后面「必须参数定义完后再定义默认参数」
- 调用时必须参数、关键字参数以及需要更改的默认参数的传递顺序不要混淆在一起「必须参数调用完后再调用关键字参数或需要更改的默认参数」
#必须参数和默认参数 def promotion(reading_minutes, listening_minutes, browsing_items = 3, sharing_task = 1): reading_points = reading_minutes * 4 listening_points = listening_minutes * 2 browsing_points = browsing_items * 0.5 * 20 sharing_points = sharing_task * 10 return reading_points, listening_points, browsing_points, sharing_points #return可以以元组的形式返回多个结果 #关键字参数和默认参数的修改 daily_reading_points, daily_listening_points, daily_browsing_points, daily_sharing_points = promotion(60, 60, browsing_items = 1, sharing_task = 0) #运用序列解包而不是元组的索引操作来提取结果 print("Today's reading scores:"+str(daily_reading_points), end = ';') print("Today's listening scores:"+str(daily_listening_points), end = ';') print("Today's browsing scores:"+str(daily_browsing_points), end = ';') print("Today's sharing scores:"+str(daily_sharing_points)) #输出结果:Today's reading scores:240;Today's listening scores:120;Today's browsing scores:10.0;Today's sharing scores:0 #必须参数和默认参数 def promotion(reading_minutes, listening_minutes, browsing_items = 3, sharing_task = 1): reading_points = reading_minutes * 4 listening_points = listening_minutes * 2 browsing_points = browsing_items * 0.5 * 20 sharing_points = sharing_task * 10 return reading_points, listening_points, browsing_points, sharing_points #return可以以元组的形式返回多个结果 #关键字参数和默认参数的修改 daily_reading_points, daily_listening_points, daily_browsing_points, daily_sharing_points = promotion(60, 60, sharing_task = 0, browsing_items = 1) #必须参数调用完后再调用关键字参数或需要更改的默认参数,关键字参数和需要修改的默认参数的位置可变,但都要在必须参数后面 print("Today's reading scores:"+str(daily_reading_points), end = ';') print("Today's listening scores:"+str(daily_listening_points), end = ';') print("Today's browsing scores:"+str(daily_browsing_points), end = ';') print("Today's sharing scores:"+str(daily_sharing_points)) #输出结果:Today's reading scores:240;Today's listening scores:120;Today's browsing scores:10.0;Today's sharing scores:0 def time_of_JDReader(): global reading_minutes, listening_minutes, browsing_minutes, sharing_minutes, total_mimutes reading_minutes = int(input("please input Today's reading time(min):")) listening_minutes = int(input("please input Today's listening time(min):")) browsing_minutes = int(input("please input Today's browsing_items(num(0-3)):")) * 0.5 sharing_minutes = int(input("please input Today's sharing_task(1/0):")) * 0.1 total_mimutes = reading_minutes + listening_minutes + browsing_minutes + sharing_minutes return reading_minutes, listening_minutes, browsing_minutes, sharing_minutes, total_mimutes def points_of_JDReader(): global reading_points, listening_points, browsing_points, sharing_points, total_points reading_points = reading_minutes * 4 listening_points = listening_minutes * 2 browsing_points = browsing_minutes * 20 sharing_points = sharing_minutes * 100 total_points = reading_points + listening_points + browsing_points + sharing_points return reading_points, listening_points, browsing_points, sharing_points, total_points #return可以以元组的形式返回多个结果 def print_of_outcome(): print("Today's reading time(min):"+str(reading_minutes), "Today's reading scores:"+str(reading_points), sep = ',') print("Today's listening time(min):"+str(listening_minutes), "Today's listening scores:"+str(listening_points), sep = ',') print("Today's browsing time(min):"+str(browsing_minutes), "Today's browsing scores:"+str(browsing_points), sep = ',') print("Today's sharing time(min):"+str(sharing_minutes), "Today's sharing scores:"+str(sharing_points), sep = ',') print("Today's total time(min) spent in JDReader:"+str(total_mimutes), "Today's total scores get in JDReader:"+str(total_points), sep = ',') time_of_JDReader() points_of_JDReader() print_of_outcome() #输出结果 please input Today's reading time(min):60 please input Today's listening time(min):60 please input Today's browsing_items(num(0-3)):1 please input Today's sharing_task(1/0):0 Today's reading time(min):60,Today's reading scores:240 Today's listening time(min):60,Today's listening scores:120 Today's browsing time(min):0.5,Today's browsing scores:10.0 Today's sharing time(min):0.0,Today's sharing scores:0.0 Today's total time(min) spent in JDReader:120.5,Today's total scores get in JDReader:370.0
class JDReader(): #必须参数和默认参数 def __init__(self, reading_minutes, listening_minutes, browsing_items = 3, sharing_task = 1): self.reading_minutes = reading_minutes self.listening_minutes = listening_minutes self.browsing_items = browsing_items self.sharing_task = sharing_task print("Today's reading time(min):"+str(self.reading_minutes)) print("Today's listening time(min):"+str(self.listening_minutes)) print("Today's browsing time(min):"+str(self.browsing_items * 0.5)) print("Today's sharing time(min):"+str(self.sharing_task * 0.5)) def promotion(self): reading_points = self.reading_minutes * 4 listening_points = self.listening_minutes * 2 browsing_points = self.browsing_items * 0.5 * 20 sharing_points = self.sharing_task * 10 print("Today's reading scores:"+str(reading_points)) print("Today's listening scores:"+str(listening_points)) print("Today's browsing scores:"+str(browsing_points)) print("Today's sharing scores:"+str(sharing_points)) Monday = JDReader(60, 60, 1, 0) Monday.promotion() #输出结果: Today's reading time(min):60 Today's listening time(min):60 Today's browsing time(min):0.5 Today's sharing time(min):0.0 Today's reading scores:240 Today's listening scores:120 Today's browsing scores:10.0 Today's sharing scores:0 class JDReader(): def __init__(self, reading_minutes, listening_minutes, browsing_items = 3, sharing_task = 1): self.reading_minutes = reading_minutes self.listening_minutes = listening_minutes self.browsing_items = browsing_items self.sharing_task = sharing_task def promotion(self): reading_points = self.reading_minutes * 4 listening_points = self.listening_minutes * 2 browsing_points = self.browsing_items * 0.5 * 20 sharing_points = self.sharing_task * 10 print("Today's reading time(min):"+str(self.reading_minutes), "Today's reading scores:"+str(reading_points), sep = ',') print("Today's listening time(min):"+str(self.listening_minutes), "Today's listening scores:"+str(listening_points), sep = ',') print("Today's browsing time(min):"+str(self.browsing_items * 0.5), "Today's browsing scores:"+str(browsing_points), sep = ',') print("Today's sharing time(min):"+str(self.sharing_task * 0.5), "Today's sharing scores:"+str(sharing_points), sep = ',') Monday = JDReader(60, 60, 1, 0) Monday.promotion() #输出结果: Today's reading time(min):60,Today's reading scores:240 Today's listening time(min):60,Today's listening scores:120 Today's browsing time(min):0.5,Today's browsing scores:10.0 Today's sharing time(min):0.0,Today's sharing scores:0 #进化版: class JDReader(): def __init__(self, reading_minutes, listening_minutes, browsing_items = 3, sharing_task = 1): self.reading_minutes = reading_minutes self.listening_minutes = listening_minutes self.browsing_items = browsing_items self.sharing_task = sharing_task def promotion(self): reading_points = self.reading_minutes * 4 listening_points = self.listening_minutes * 2 browsing_points = self.browsing_items * 0.5 * 20 sharing_points = self.sharing_task * 10 total_mimutes = self.reading_minutes + self.listening_minutes + self.browsing_items * 0.5 + self.sharing_task * 0.1 total_points = reading_points + listening_points + browsing_points + sharing_points print("Today's reading time(min):"+str(self.reading_minutes), "Today's reading scores:"+str(reading_points), sep = ',') print("Today's listening time(min):"+str(self.listening_minutes), "Today's listening scores:"+str(listening_points), sep = ',') print("Today's browsing time(min):"+str(self.browsing_items * 0.5), "Today's browsing scores:"+str(browsing_points), sep = ',') print("Today's sharing time(min):"+str(self.sharing_task * 0.1), "Today's sharing scores:"+str(sharing_points), sep = ',') print("Today's total time(min) spent in JDReader:"+str(total_mimutes), "Today's total scores get in JDReader:"+str(total_points), sep = ',') Monday = JDReader(60, 60, 1, 0) Monday.promotion()