变量作用域
- 变量由作用范畴限度
-
分类:依照作用域分类
- 全局 (global):在函数内部定义
- 部分 (local):在函数外部定义
-
变量的作用范畴
- 全局变量:在整个全局范畴都无效
- 全局变量在部分能够应用(即函数外部能够拜访函数内部定义的变量)
- 局部变量在部分范畴能够应用
- 局部变量在全局范畴无奈应用
-
LEGB 准则
- L(Local)部分作用域
- E(Enclosing function local)内部嵌套函数作用域
- G(Global module)函数定义所在模块作用域
- B(Buildin):python 内置模块的作用域
# 认为 a1 是全局的
a1 = 100
def fun():
print(a1)
print("I am in fun")
# a2 的作用范畴是 fun
a2 = 99
print(a2)
print(a1)
fun()
# print(a2)
100
100
I am in fun
99
晋升局部变量为全局变量
- 应用 global
- 案例如下
def fun():
global b1
b1 = 100
print(b1)
print("I am in fun")
b2 = 99
print(b2)
fun()
print(b1)
100
I am in fun
99
100
global,local 函数
- 能够通过 globals 和 locals 显示出局部变量和全局变量
- 参考一下案例
# globals 和 locals
# globals 和 locals 叫做内建函数
a = 1
b = 2
def fun(c,d):
e = 111
print("Locals={0}".format(locals()))
print("Globals={0}".format(globals()))
fun(100, 200)
Locals={'c': 100, 'd': 200, 'e': 111}
Globals={'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['','# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a2)','# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)','# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\n# print(a2)','# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\nprint(a2)','# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\n# print(a2)','def fun():\n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n \nfun()','def fun():\n global b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals())\n \nfun(100, 200)','# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals()))\n \nfun(100, 200)','# globals 和 locals\n# globals 和 locals 叫做内建函数 \na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals()))\n \nfun(100, 200)'],'_oh': {},'_dh': ['d:\\Jupyter\\nootbook\\ 笔记 '],'In': ['', '# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a2)', '# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)', '# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\n# print(a2)', '# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\nprint(a2)', '# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\n# print(a2)', 'def fun():\n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n \nfun()', 'def fun():\n global b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)', 'def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)', 'def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)', 'def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)', '# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals())\n \nfun(100, 200)', '# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals()))\n \nfun(100, 200)', '# globals 和 locals\n# globals 和 locals 叫做内建函数 \na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals()))\n \nfun(100, 200)'], 'Out': {}, 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x000001B07AF18BA8>>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x000001B07D7398D0>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x000001B07D7398D0>, '_': '','__':'', '___': '','_i':'# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals()))\n \nfun(100, 200)','_ii':'# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals())\n \nfun(100, 200)','_iii':'def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','_i1':'# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a2)','a1': 100,'fun': <function fun at 0x000001B07D8C41E0>,'_i2':'# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)','_i3':'# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\n# print(a2)','_i4':'# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\nprint(a2)','_i5':'# 认为 a1 是全局的 \na1 = 100\n\ndef fun():\n print(a1)\n print("I am in fun")\n a2 = 99\n print(a2)\n \nprint(a1)\nfun()\n# print(a2)','_i6':'def fun():\n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n \nfun()','_i7':'def fun():\n global b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','_i8':'def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','_i9':'def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','_i10':'def fun():\n global b1 \n b1 = 100\n print(b1)\n print("I am in fun")\n b2 = 99\n print(b2)\n\nprint(b1)','_i11':'# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals())\n \nfun(100, 200)','_i12':'# globals 和 locals\na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals()))\n \nfun(100, 200)','a': 1,'b': 2,'_i13':'# globals 和 locals\n# globals 和 locals 叫做内建函数 \na = 1\nb = 2\n\ndef fun(c,d):\n e = 111\n print("Locals={0}".format(locals()))\n print("Globals={0}".format(globals()))\n \nfun(100, 200)'}
eval() 函数
- 把一个字符串当成一个表达式来执行,返回表达式执行后的后果
- 语法:
eval(string_code, globals=None, locals=None)
exec() 函数
- 跟 eval 性能相似,然而,不返回后果
- 语法:
exec(string_code, globals=None, locals=None)
x = 100
y = 200
# 执行 x +y
# z = x + y
z1 = x + y
z2 = eval("x+y")
print(z1)
print(z2)
300
300
# exec 案例
x = 100
y = 200
# 执行 x +y
# z = x + y
z1 = x + y
# 1. 留神字符串中引号的写法
# 2. 比对 exec 执行后果和代码执行后果
z2 = exec("print('x+y:',x+y)")
print(z1)
print(z2)
x+y: 300
300
None
递归函数
- 函数间接或者间接调用本身
- 长处:简洁,了解容易
- 毛病:对递归深度有限度,耗费资源大
- python 对递归深度有限度,超过限度报错
- 在写递归程序的时候,肯定留神完结条件
# 递归调用深度限度代码
x = 0
def fun():
global x
x += 1
print(x)
# 函数本人调用本人
fun()
# 调用函数
# fun()
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-23-bfedb7e396bc> in <module>
10
11 # 调用函数
---> 12 fun()
<ipython-input-23-bfedb7e396bc> in fun()
7 print(x)
8 # 函数本人调用本人
----> 9 fun()
10
11 # 调用函数
... last 1 frames repeated, from the frame below ...
<ipython-input-23-bfedb7e396bc> in fun()
7 print(x)
8 # 函数本人调用本人
----> 9 fun()
10
11 # 调用函数
RecursionError: maximum recursion depth exceeded while calling a Python object
# 斐波那契数列
# 一列数字,第一个值是 1,第二个也是 1,从第三个开始,每一个数字的值等于前两个数字呈现的值的和
# 数学公式为:f(1) = 1, f(2) = 1, f(n) = f(n - 1) + f(n - 2)
# 例如:1,1,2,3,5,8,13....
# n 示意求第 n 个数字的斐波那契数列的值
def fib(n):
if n == 1 or n == 2:
return 1
elif n > 0:
return fib(n-1) + fib(n-2)
else:
return None
print(fib(3))
print(fib(5))
print(fib(10))
print(fib(-1))
print(fib(1))
2
5
55
None
1
内置数据结构(变量类型)
- list
- set
- dict
- tuple
list(列表)
- 一组有程序的数据的组合
-
创立列表
- 空列表
# 1. 创立空列表
l1 = []
# type 是内置函数,负责打印出变量的类型
print(type(l1))
print(l1)
# 2. 创立带值的列表
l2 = [100]
print(type(l2))
print(l2)
# 3. 创立列表,带多个值
l3 = [2,3,5,5,9,7,8,]
print(type(l3))
print(l3)
# 4. 应用 list()
l4 = list()
print(type(l4))
print(l4)
<class 'list'>
[]
<class 'list'>
[100]
<class 'list'>
[2, 3, 5, 5, 9, 7, 8]
<class 'list'>
[]
列表罕用操作
-
拜访
- 应用下标操作(索引)
- 列表的位子是从 0 开始
-
分片操作
- 对列表进行任意一段的截取
- l[:]
# 下标拜访列表
l = [3,2,5,1,9,8,7]
print(l[1])
2
print(l[0])
3
# 分片操作
# 留神截取的范畴,蕴含右边的下标值,不蕴含左边的下标值
print(l[1:4])
# 下标值能够为空,如果不写,右边下标值默认为 0,左边下标值为最大数加一,即示意截取到最初一个数据
print(l[:])
print(l[:4])
print(l[2:])
[2, 5, 1]
[3, 2, 5, 1, 9, 8, 7]
[3, 2, 5, 1]
[5, 1, 9, 8, 7]
print(l)
# 分片能够管制增长幅度,默认增长幅度为 1
print(l[1:6:1])
# 打印从下标 1 开始的数字,每次隔一个
print(l[1:6:2])
# 下标能够超出范围,超出后不在思考多余下标内容
print(l[2:10])
# 下标值,增长幅度能够为正数
# 为正数,表明为从右往左
# 规定:数组最初一个数字的下标是 -1
[3, 2, 5, 1, 9, 8, 7]
[2, 5, 1, 9, 8]
[2, 1, 8]
[5, 1, 9, 8, 7]
# 分片之正数下标
print(l)
# 上面显示的是为空,因为默认分片总是从左向右截取
print(l[-2:-4])
print(l[-4:-2])
# 如果分片肯定右边值比左边大,则步长参数须要应用正数
# 此案例为一个 list 间接正反截取提供了一个思路
print(l[-2:-4:-1])
print(l[-1:-8:-1])
[3, 2, 5, 1, 9, 8, 7]
[]
[1, 9]
[8, 9]
[7, 8, 9, 1, 5, 2, 3]
分片操作是生成一个新的 list
- 内置函数 id,负责显示一个变量或者数据的惟一确定编号
# id 函数举例
a = 100
b = 200
print(id(a))
print(id(b))
# a 跟 c 指向同一份数据
c = a
print(id(c))
a = 101
print(a)
print(c)
print(id(a))
print(id(c))
140734817148832
140734817152032
140734817148832
101
100
140734817148864
140734817148832
# 通过 id 能够直接判断出分片是从新生成了一份数据还是应用同一份数据
l = [3,5,6,8,5,43,4,7]
ll = l[:] # 分片操作
lll = ll
# 如果两个 id 值一样,则表明分片产生的列表是应用的同一地址同一份数据
# 否则,则表明分片是从新产生了一份数据,即一个新的列表,而后把数据拷贝到新列表中
print(id(l))
print(id(ll))
print(id(lll))
# 通过 id 晓得,ll 和 lll 是同一份数据,验证代码如下
l[1] = 100
print(l)
print(ll)
print(lll)
ll[1] = 100
print(ll)
print(lll)
1857540073800
1857540052488
1857540052488
[3, 100, 6, 8, 5, 43, 4, 7]
[3, 5, 6, 8, 5, 43, 4, 7]
[3, 5, 6, 8, 5, 43, 4, 7]
[3, 100, 6, 8, 5, 43, 4, 7]
[3, 100, 6, 8, 5, 43, 4, 7]