From d87ec2f68350f8bd8023107d1b879ca4f53c14c8 Mon Sep 17 00:00:00 2001 From: wanyi_wang Date: Thu, 7 Jan 2021 20:41:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=5F=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1.py" | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" index ca4b4a08..d09767b7 100644 --- "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2541\350\212\202/1.py" @@ -37,13 +37,15 @@ print("———————————————————————— d = 0 e = 1 f = 0 +i = 0 print(d) print(e) -while 0 < f < 100: - print(f) - f = d + e - d = e - e = f +while i <= 100: + d, e = e, e + d + if e > 100: + break + print(e) + i += 1 print("——————————————————————————————————————————————————") -- Gitee From 7edb69d12cd7251503b7195faa2ad28597df2334 Mon Sep 17 00:00:00 2001 From: wanyi_wang Date: Thu, 7 Jan 2021 20:41:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=5F=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3-3.py" | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2543\350\212\202/3-3.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2543\350\212\202/3-3.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2543\350\212\202/3-3.py" new file mode 100644 index 00000000..d65ffbbf --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_\347\216\213\345\251\211\346\200\241/\347\254\2543\345\221\250/\347\254\2543\345\221\250\347\254\2543\350\212\202/3-3.py" @@ -0,0 +1,110 @@ +# 笔记 +# 作用域:程序在创建, 访问, 改变一个变量时, 都是在一个保存该变量的空间内进行, 这个空间被称为命名空间, 即作用域 +# 作用域:(变量起作用的范围)变量起作用的范围 +# 内置作用域:可以在Python环境中的任何模块, 任意位置访问和调用,(定义在函数外) +# 全局作用域:只作用于当前模块,(在函数体定义) +# 嵌套作用域:在嵌套函数中, 访问函数体之外的非全局作用域 +# 局部作用域:只作用于当前函数体 +# 优先级:内置作用域>全局作用域>嵌套作用域>局部作用域(查找顺序倒序:LGEB) + + +# 练习作用域之间的转换 +# 1.局部变量——全局变量 +a = 1 +b = 2 + + +def foo(): + global a + b = 3 # 在函数体中赋值,为局部变量 + a = a + b + print(a) # 4,变为全局变量 + print(b) # 3,为局部变量 + + +foo() +print(a, b) # 4 2 ,a为全局变量,b不变 + +# 注释: +# def foo(): +# global a +# a = a + b +# b = 3 +# 报错,在函数foo内部,逻辑无法判断b的作用域 +print("________________________________") + + +# 2.局部变量——自由变量 + +# 已知绳子的长度求围成的正方形面积 +def square_area(): + d = 0 + + def area(value): + nonlocal d + d += (value / 4) + return d ** 2 + + return area + + +my_area = square_area() +print(my_area(12)) +print(my_area(16)) +print("________________________________") + +# 默写一个装饰器, 用来输出函数的执行时间. +import time # 导入时间参数 + + +def clock_deco(func): # 定义时间装饰器,并省略不定长参数及关键字参数 + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) # 执行参数 + end_time = time.time() + print( + f"{func.__name__}execute time: {format(end_time - start_time, '.2f')} s") + # 'func.__name__'获取参数名称,执行时间参数相减 + return result + + return wrapper + + +@clock_deco +def foo(a, b): + c = 0 + while True: + if c >= a ** b: + break + c += 1 + + +foo(7, 7) + +print("________________________________") + +# 使用装饰器来为斐波那契函数添加缓存 +a = {} + + +def cache_deco(func): + global a + + def wrap(n): + if n in a: + return a[n] + a[n] = func(n) + return a[n] + + return wrap + + +@cache_deco +def fibo(n): + if n < 2: + return n + return fibo(n - 1) + fibo(n - 2) + + +fibo(10) +print(a) -- Gitee