python變量作用域與列表入門詳解
變量作用域
變量由作用范圍限制
分類:按照作用域分類
- 全局(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]
總結
到此這篇關於python變量作用域與列表的文章就介紹到這瞭,更多相關python變量作用域與列表內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Python eval() 函數看這一篇就夠瞭
- 總結分析Python的5個硬核函數
- Python中eval函數的表達式作用示例
- python高級內置函數用法實例
- Python函數命名空間和作用域(Local與Global)