每日学习5:递归函数、命名空间和时间函数的定义和运用(参考代码)
-
递归函数
-
递归函数的定义
- 如果一个函数在内部调用自身本身,这个函数就是递归函数。
- 递归函数需要满足两个条件:自己调用自己,有停止条件。
-
递归函数的运用
-
# 阶乘 def factorial(n): # 出口 if n == 1: return 1 # 自己调用自己 return n * factorial(n - 1) print(factorial(10)) #输出结果为:3628800
-
# 定义一个函数fib,能够指定数量n,生成著名的斐波拉契数列(Fibonacci) # 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 除第一、二个数之外,后一个数等于前两个数之和 def Fibonacci(n): # 出口 if n == 1 or n == 2: return 1 # 自己调用自己 return Fibonacci(n-1) + Fibonacci(n-2) print(Fibonacci(12)) #输出结果为:144
-
-
-
命名空间
-
命名空间的定义
- 命名空间提供了在项目中避免名字冲突的一种方法。各个命名空间是独立的,没有任何关系的,所以一个命名空间中不能有重名,但不同的命名空间是可以重名而没有任何影响。
- Python中一般有三种命名空间:
- 内置名称: python内置的模块去寻找。
- 全局名称:在整个代码之前去寻找。
- 局部名称:函数内部找变量。
- Python 的作用域一共有4种:
- L(Local):包含局部变量,比如一个函数/方法内部。
- E(Enclosing):包含了非局部(non-local)也非全局(non-global)的变量。
- G(Global):最外层,比如当前模块的全局变量。
- B(Built-in):包含内建变量/关键字等,最后被搜索。
-
命名空间的运用
-
# 局部作用域 # 局部找到数据就使用局部作用域内的数据 def aaa(): m = 10 n = 20 print(m,n) aaa() #输出结果为:10 20
-
# 局部作用域 # 局部找到数据就使用局部作用域内的数据 def aaa(): m = 10 n = 20 print(m,n,x) # 全局作用域 n=3 #读取流程为,先读取def里面局域作用域的数据信息,如果没有则向上一级寻找,如果有,则不需要向上一级寻找。(比如,该代码def里面缺少x对应的值,n则不需要) x = 11 aaa() #输出结果为:10 20 11
-
# 闭包函数 # outer 数据enclosing 闭包函数外的函数 def outer(): o_count = 1 print(o_count) # inner 属于局部作用域 def inner(): i_count = 2 print(i_count,o_count) return inner inner = outer() inner()
-
-
-
时间函数
-
时间函数的定义(常用的是
time
和datetime
模块)-
time.time()
:- 返回当前时间的时间戳(自1970年1月1日以来的秒数)。
-
time.sleep(seconds)
:- 使程序暂停执行指定的秒数。
-
time.ctime([seconds])
:- 转换时间戳为本地时间的可读字符串。如果没有提供时间戳,默认使用当前时间。
-
time.localtime([seconds])
:- 将时间戳转换为本地时间的
struct_time
对象。
- 将时间戳转换为本地时间的
-
time.gmtime([seconds])
:- 将时间戳转换为UTC时间的
struct_time
对象。
- 将时间戳转换为UTC时间的
-
time.mktime(tuple)
:- 将
struct_time
或time.struct_time
对象转换为时间戳。
- 将
-
time.strftime(format[, tuple])
:- 将
struct_time
或time.struct_time
对象格式化为指定格式的字符串。
- 将
-
time.strptime(string[, format])
:- 将字符串解析为
struct_time
对象。
- 将字符串解析为
-
datetime.date(year, month, day)
:- 表示一个具体的日期。
-
datetime.time(hour, minute, second, microsecond)
:- 表示一个具体的时间。
-
datetime.datetime(year, month, day, hour, minute, second, microsecond)
:- 表示一个具体的日期和时间。
-
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
:- 表示两个日期或时间之间的差异。
-
-
时间函数的运用
-
from datetime import datetime, timedelta # 创建一个日期对象 some_date = datetime(2024, 7, 14) print("Date:", some_date) # 创建一个时间对象 some_time = datetime(2024, 7, 14, 12, 30) print("Time:", some_time) # 创建一个时间差 time_difference = timedelta(days=2, hours=6) print("Time Difference:", time_difference) # 将日期和时间相加 new_date = some_date + time_difference print("New Date:", new_date) #输出结果为: Date: 2024-07-14 00:00:00 Time: 2024-07-14 12:30:00 Time Difference: 2 days, 6:00:00 New Date: 2024-07-16 06:00:00
-
from datetime import datetime, timedelta # 创建一个日期对象 some_date = datetime(2024, 7, 14) print("Date:", some_date) # 创建一个时间对象 some_time = datetime(2024, 7, 14, 12, 30) print("Time:", some_time) # 创建一个时间差 time_difference = timedelta(days=2, hours=6) print("Time Difference:", time_difference) # 将日期和时间相加 new_date = some_date + time_difference print("New Date:", new_date) #输出结果为: Date: 2024-07-14 00:00:00 Time: 2024-07-14 12:30:00 Time Difference: 2 days, 6:00:00 New Date: 2024-07-16 06:00:00
-
-