[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
什么是函数:
函数指被封装起来的、实现某种功能的一段代码。Python安装包、标准库中自带的函数统称为内置函数,用户自己编写的函数称为自定义函数,不管是哪种函数,其定义和调用方式都是一样的。
#定义好的函数直到被程序调用时才会执行。函数的调用格式如下: #函数名([参数列表]) 如 调用刚刚定义的weather()函数。 def
weather(): print("*" * 13) print("日期:4月8日") print("温度:14~28℃") print("空气状况:良")
print("*" * 13) weather() #点击运行 运行后结果 # ************* # 温度:14~28℃ # 空气状况:良 #
*************
位置参数:
关键字参数:
默认参数:
只有一个值时将127.0.0.1传给ip 这个参数 ,当他有两个值时,port =8080将取代port=3306。
不定长参数:
我们写的函数有时候需要另一个函数调用,那么我们就需要返回一个值:return
# 作用: # 1.return会给函数的执行者返回值,x需要通过print才能打印出来 # 如果返回多个值,以元组的形式返回的 def test():
num = 100 return num, 'jiuge', '卡布奇诺', '卡布基诺' print(test()) #
2.函数中遇到return,此函数结束,不继续执行 def test2(): return 123 print(456) print(test2())
#return 和print的区别 # 1. return之后的代码不执行,print会一直执行 # 2. return是返回计算值, print是打印结果
编写程序,实现根据用户输入的数字输出斐波那契数列的功能。
""" 斐波那契数列又称为兔子数列,因数学家列昂纳多·斐波那契以兔子繁殖为例子引入,这个数列中的数据满足以下公式: F(1)=1, F(2)=1,
F(n)= F(n-1) + F(n-2) (n>=3,n∈N*) 使用递归解决 斐波那契数列 """ def fib(n): if n == 1 or n
== 2: return 1 return fib(n - 1) + fib(n - 2) number = int(input('请输入一个正整数: '))
result = fib(number) print("第%d个斐波那契数为:%d" % (number, result)) # def
factorial(n): # if n < 1: # return -1 # if n == 1 or n == 2: # return 1 # else:
# return factorial(n - 1) + factorial(n - 2) # # # number =
int(input('请输入一个正整数:')) # result = factorial(number) # print("%d 的斐波那契数列最大值:%d"
% (number, result))