转载:https://www.cnblogs.com/uncleyong/p/11230413.html 局部变量和全局变量
1 #== == == == == == == == 第一部分 == == == == == == == == == 2 res = None 3 def calc(a, b): 4 res = a + b 5 calc(1, 2) 6 print(res) 7 #上面代码结果是:None 8 9 res = None 10 def calc(a, b): 11 res = 0 12 res = a + b 13 calc(1, 2) 14 print(res) 15 #上面代码结果是:None 16 17 res = None 18 def calc(a, b): 19 global res 20 res = a + b 21 calc(1, 2) 22 print(res) 23 #上面代码结果是:3 24 25 res2 = None 26 def calc(a, b): 27 global res 28 res = a + b 29 calc(1, 2) 30 print(res) 31 #上面代码结果是:3 32 33 # res = None 34 # def calc(a, b): 35 # res = a + b 36 # global res 37 # calc(1, 2) 38 # print(res) 39 #上面代码结果是:运行报错 40 41 res = None 42 def calc(a, b): 43 global res 44 res = 0 45 res = a + b 46 calc(1, 2) 47 print(res) 48 #上面代码结果是:3 49 50 # res = None 51 # def calc(a, b): 52 # res = 0 53 # global res 54 # res = a + b 55 # calc(1, 2) 56 # print(res) 57 #上面代码结果是:运行报错 58 59 # res = None 60 # def calc(a, b): 61 # res = 0 62 # res = a + b 63 # global res 64 # calc(1, 2) 65 # print(res) 66 #上面代码结果是:运行报错 67 68 money = 0 69 def tom(): 70 global money 71 money = 100 72 def jack(): 73 global money 74 money = money - 50 75 tom() 76 jack() 77 print('jack消费后剩余%s' % money) 78 #上面代码结果是: 50 (不是-50) 79 80 def tom(): 81 global money 82 money = 100 83 def jack(): 84 global money 85 money = money - 50 86 tom() 87 jack() 88 print('jack消费后剩余%s' % money) 89 #上面代码结果是:50 (不是-50)
1 # == == == == == == == == 第二部分 == == == == == == == == == 2 d = {} 3 def test(): 4 d['url'] = 'https://www.cnblogs.com/uncleyong/p/10530261.html' 5 def test2(): 6 d['url'] = 'https://www.cnblogs.com/uncleyong/' 7 test() 8 test2() 9 print(d) 10 # 上面代码结果是: {'url': 'https://www.cnblogs.com/uncleyong/'} 11 12 def test(): 13 d = {} 14 d['url'] = 'https://www.cnblogs.com/uncleyong/p/10530261.html' 15 def test2(): 16 d = {} 17 d['url'] = 'https://www.cnblogs.com/uncleyong/' 18 test() 19 test2() 20 print(d) 21 # 上面代码结果是: {'url': 'https://www.cnblogs.com/uncleyong/'}(不会报错) 22 23 def test(): 24 d = {} 25 d['url'] = 'https://www.cnblogs.com/uncleyong/p/10530261.html' 26 def test2(): 27 global d 28 d = {} 29 d['url'] = 'https://www.cnblogs.com/uncleyong/' 30 test() 31 test2() 32 print(d) 33 # 上面代码结果是:{'url': 'https://www.cnblogs.com/uncleyong/'} 34 35 info = {'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'} 36 def test(): 37 global info 38 info = {} 39 info['name'] = 'qzcsbj' 40 test() 41 print(info) 42 # 上面代码结果是:{'name': 'qzcsbj'} 43 44 info = {'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'} 45 def test(): 46 info = {} 47 info['name'] = 'qzcsbj' 48 test() 49 print(info) 50 # 上面代码结果是:{'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'} 51 52 info = {'age': 18, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'} 53 def test(): 54 info['age'] = info['age'] + 1 55 test() 56 print(info) 57 # 上面代码结果是:{'age': 19, 'url': 'https://www.cnblogs.com/uncleyong/p/10530261.html'} 58 59 s = 'https://www.cnblogs.com/uncleyong/p/10530261.html' 60 def test(): 61 s = 'test' 62 test() 63 print(s) 64 # 上面代码结果是:https://www.cnblogs.com/uncleyong/p/10530261.html 65 66 url = 'https://www.cnblogs.com/uncleyong/p/10530261.html' 67 def test(): 68 s = 'test' 69 test() 70 print(s) 71 # 上面代码结果是:https://www.cnblogs.com/uncleyong/p/10530261.html (没报错) 72 73 url = 'https://www.cnblogs.com/uncleyong/p/10530261.html' 74 def test(): 75 global s 76 s = 'test' 77 test() 78 print(s) 79 # 上面代码结果是:test 80 81 s = 'https://www.cnblogs.com/uncleyong/p/10530261.html' 82 def test(): 83 global s 84 s = 'test' 85 test() 86 print(s) 87 # 上面代码结果是:test 88 89 s = [1, 2, 3] 90 def test(): 91 s[0] = 123 92 test() 93 print(s) 94 # 上面代码结果是:[123, 2, 3] 95 96 s = [1, 2, 3] 97 def test(): 98 s = [] 99 s.append(123) 100 test() 101 print(s) 102 # 上面代码结果是: [1, 2, 3] 103 104 s = [1, 2, 3] 105 def test(): 106 global s 107 s[0] = 123 108 test() 109 print(s) 110 # 上面代码结果是:[123, 2, 3] 111 112 s = [1, 2, 3] 113 def test(): 114 global s 115 s = [] 116 s.append(123) 117 test() 118 print(s) 119 # 上面代码结果是:[123] 120 121 # s = (1, 2, 3) 122 # def test(): 123 # s[0] = 123 124 # test() 125 # print(s) 126 # 上面代码结果是:这句 s[0] = 123 报错 'tuple' object does not support item assignment 127 128 # s = (1, 2, 3) 129 # def test(): 130 # global s 131 # s[0] = 123 132 # test() 133 # print(s) 134 # 上面代码结果是:这句 s[0] = 123 报错 'tuple' object does not support item assignment 135 136 s = {1, 2, 3} 137 def test(): 138 s.add(5) 139 test() 140 print(s) 141 # 上面代码结果是:{1, 2, 3, 5} 142 143 s = {1, 2, 3} 144 def test(): 145 global s 146 s.add(5) 147 test() 148 print(s) 149 # 上面代码结果是:{1, 2, 3, 5} 150 151 s = {1, 2, 3} 152 def test(): 153 s = set() 154 s.add(5) 155 test() 156 print(s) 157 # 上面代码结果是:{1, 2, 3} 158 159 s = {1, 2, 3} 160 def test(): 161 global s 162 s = set() 163 s.add(5) 164 test() 165 print(s) 166 # 上面代码结果是:{5}