day15-python之变量和递归

2019-05-13 07:14:38来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

1.局部变量与全局变量

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 
  4 # name='lhf'
  5 # def change_name():
  6 #     global name
  7 #     name='帅了一比'
  8 #     print('change_name',name)
  9 #
 10 #
 11 # change_name()
 12 # print(name)
 13 
 14 # name='lhf'
 15 #
 16 # def change_name():
 17 #     name1='帅了一比'
 18 #     name2='帅了一比'
 19 #     name3='帅了一比'
 20 #     print('change_name',name)
 21 #
 22 # globals
 23 
 24 # NAME = "杠娘"
 25 #
 26 # def yangjian():
 27 #     global NAME
 28 #     NAME = "小东北"
 29 #     print('我要搞', NAME)
 30 #
 31 # def qupengfei():
 32 #     NAME = "基"
 33 #     print('我要搞', NAME)
 34 #
 35 # yangjian()
 36 # qupengfei()
 37 
 38 # NAME = "产品经理"
 39 #
 40 # def yangjian():
 41 #     # NAME = "史正文"
 42 #     global NAME # 已经声明,NAME就是全局的的那个变量
 43 #     print('我要搞', NAME)
 44 #     NAME = "小东北"  # 修改 全局的变量
 45 #     print('我要搞', NAME)
 46 #
 47 # def qupengfei():
 48 #     NAME = "基"
 49 #     print('我要搞', NAME)
 50 
 51 # 如果函数的内容无global关键字,
 52 #   - 有声明局部变量
 53 # NAME = ["产品经理","廖波湿"]
 54 # def qupengfei():
 55 #     NAME = "自己"
 56 #     print('我要搞', NAME)
 57 # qupengfei()
 58 #   - 无声明局部变量
 59 # NAME = ["产品经理","廖波湿"]
 60 # def qupengfei():
 61 #     NAME.append('XXOO')
 62 #     print('我要搞', NAME)
 63 # qupengfei()
 64 
 65 # 如果函数的内容有global关键字
 66 #   - 有声明局部变量
 67 # NAME = ["产品经理","廖波湿"]
 68 # def qupengfei():
 69 #     global NAME
 70 #     NAME = "自己"
 71 #     print('我要搞', NAME)
 72 # qupengfei()
 73         # 错误示例
 74 # NAME = ["产品经理","廖波湿"]
 75 # def qupengfei():
 76 #     NAME = "自己"
 77 #     global NAME
 78 #     print('我要搞', NAME)
 79 # qupengfei()
 80 #   - 无声明局部变量
 81 # NAME = ["产品经理","廖波湿"]
 82 # def qupengfei():
 83 #     global NAME
 84 #     NAME = ["阿毛"]
 85 #     NAME.append('XXOO')
 86 #     print('我要搞', NAME)
 87 # qupengfei()
 88 
 89 ######## 全局变量变量名大写
 90 ######## 局部变量变量名小写
 91 
 92 # 优先读取局部变量,能读取全局变量,无法对全局变量重新赋值 NAME=“fff”,
 93 #     但是对于可变类型,可以对内部元素进行操作
 94 # 如果函数中有global关键字,变量本质上就是全局的那个变量,可读取可赋值 NAME=“fff”
 95 # qupengfei()
 96 # yangjian()
 97 
 98 # NAME = ["产品经理","廖波湿"]
 99 #
100 # def yangjian():
101 #     # NAME = "史正文"
102 #     global NAME # 已经声明,NAME就是全局的的那个变量
103 #     print('我要搞', NAME)
104 #     NAME = "小东北"  # 修改 全局的变量
105 #     print('我要搞', NAME)
106 #
107 # def qupengfei():
108 #     # NAME = "aS"
109 #     NAME.append('天扎龙')
110 #     print('我要搞', NAME)
111 #
112 # yangjian()
113 
114 # NAME = ["产品经理","廖波湿"]
115 # def qupengfei():
116 #     name = "自己"
117 #     global NAME
118 #     print('我要搞', NAME)
119 # qupengfei()
120 
121 # NAME = '海风'
122 #
123 # def huangwei():
124 #     name = "黄伟"
125 #     print(name)
126 #     def liuyang():
127 #         name = "刘洋"
128 #         print(name)
129 #         def nulige():
130 #             name = '沪指花'
131 #             print(name)
132 #         print(name)
133 #         nulige()
134 #     liuyang()
135 #     print(name)
136 #
137 # huangwei()  # 黄伟 刘洋 刘洋 沪指花 黄伟
138 
139 # name = "刚娘"
140 # 
141 # def weihou():
142 #     name = "陈卓"
143 #     def weiweihou():
144 #         nonlocal name   # nonlocal,指定上一级变量,如果没有就继续往上直到找到为止
145 #         name = "冷静"
146 # 
147 #     weiweihou()
148 #     print(name)
149 # 
150 # print(name)
151 # weihou()
152 # print(name)
153 # 刚娘
154 # 冷静
155 # 刚娘

2.前向引用

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 
 5 # def foo():
 6 #     print('from foo')
 7 #     bar()
 8 #
 9 # foo()
10 
11 # def bar():
12 #     print('from bar')
13 # def foo():
14 #     print('from foo')
15 #     bar()
16 #
17 # foo()
18 
19 
20 
21 # def foo():
22 #     print('from foo')
23 #     bar()
24 #
25 # def bar():
26 #     print('from bar')
27 # foo()
28 
29 # def foo():
30 #     print('from foo')
31 #     bar()
32 # 
33 # foo()
34 # 
35 # def bar():
36 #     print('from bar')

3.测试

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 name='海风'
 4 def huangwei():
 5     name = "黄伟"
 6     print(name)
 7     def liuyang():
 8         name = "刘洋"
 9         print(name)
10         def nulige():
11             name = '炉指花'
12             print(name)
13         print(name)
14         nulige()
15     liuyang()
16     print(name)
17 
18 print(name)
19 huangwei()
20 print(name)  # 海风 黄伟 刘洋 刘洋 炉指花 黄伟 海风
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 def calc(n):
 4     print(n)
 5     if int(n / 2) == 0: # 10/2  5/2  2/2
 6         return n
 7     res=calc(int(n / 2))
 8     return res
 9 
10 
11 calc(10)#   10 5 2 1

4.递归

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # def calc(n):
 4 #     print(n)
 5 #     if int(n / 2) == 0:
 6 #         return n
 7 #     res=calc(int(n / 2))
 8 #     return res
 9 #
10 #
11 # res=calc(10)
12 # print(res)
13 
14 # while True:
15 #     print(10)
16 
17 # import time
18 #
19 # person_list=['alex','wupeiqi','linhaifeng','zsc']
20 # def ask_way(person_list):
21 #     print('-'*60)
22 #     if len(person_list) == 0:
23 #         return '根本没人知道'
24 #     person=person_list.pop(0)
25 #     if person == 'linhaifeng':
26 #         return '%s说:我知道,老男孩就在沙河汇德商厦,下地铁就是' %person
27 #
28 #     print('hi 美男[%s],敢问路在何方' % person)
29 #     print('%s回答道:我不知道,但念你慧眼识猪,你等着,我帮你问问%s...' % (person, person_list))
30 #     time.sleep(1)
31 #     res=ask_way(person_list)
32 #
33 #
34 #     print('%s问的结果是: %res' %(person,res))
35 #     return res
36 #
37 # res=ask_way(person_list)
38 # print(res)
39 # 我          来英                杨建         是征文
40 # res等沙河      res等沙河       res等 沙河      return ‘沙河’
41 #                 print          printres
42 
43 
44 import time
45 res=time.sleep(10)
46 print('----------->')

 


原文链接:https://www.cnblogs.com/sqy-yyr/p/10848036.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python书籍推荐:量化投资:以Python为工具

下一篇:day22 01 初识面向对象----简单的人狗大战小游戏