七、包、模块、函数与变量作用域

 

一、while循环

1、关键点

1)条件不能是常量

2)代码块中必须有改变条件的语句

3)可结合else使用

4)适用场景:撸啊撸打怪,不知道目标有多远,直到达到目标。递归中常使用while循环

 1 # coding=utf-8
 2 
 3 # #条件为常量,无限死循环
 4 # CONDITION=True
 5 # while CONDITION:
 6 #     print('I am while')
 7 
 8 
 9 # #初始条件不满足,不执行代码块
10 # counter=0
11 # while counter:
12 #     counter+=1
13 #     print(counter)
14 
15 # counter=1
16 # while counter<=10:
17 #     # counter+=1
18 #     print(counter)
19 #     counter+=1
20 
21 # 与else结合使用
22 counter = 1
23 while counter <= 10:
24     counter += 1
25     print(counter)
26 
27 else:
28     print('EOF')

二、for与for-else循环

1、关键点

1)适用场景:主要用来遍历(循环)序列、集合或者字典

2)可嵌套使用

3)现在一行打印使用print(x,end=' ')

4)可与else结合使用

5)遍历部分元素,可使用if

6)易错:break的使用

 1 # coding=utf-8
 2 
 3 # # 基本用法
 4 # a = ['apple', 'orange', 'banana', 'grape']
 5 # for i in a:
 6 #     print(i)
 7 
 8 
 9 # # # 嵌套使用
10 # s = [['a', 'b', 'c'], (1, 2, 3)]
11 # for x in s:
12 #     for y in x:
13 #         print(y)
14 #
15 #
16 # #一行打印end=' '
17 # a = ['apple', 'orange', 'banana', 'grape']
18 # for i in a:
19 #     print(i,end=' ')
20 
21 
22 # #与else结合使用
23 # a = ['apple', 'orange', 'banana', 'grape']
24 # for i in a:
25 #     print(i)
26 # else:
27 #     #当列表中所有元素遍历完成后执行else代码
28 #     print('fruit is gone')
29 
30 #
31 # # 结合if,遍历部分元素
32 # a = [1, 2, 3, 4]
33 # for x in a:
34 #     if x == 2:
35 #         break#中止循环
36 #     print(x)
37 #
38 # print('--------------------')
39 #
40 # a1 = [1, 2, 3, 4]
41 # for x in a1:
42 #     if x == 2:
43 #         continue#跳过当前循环
44 #     print(x)
45 
46 
47 # #易错,break的使用
48 # a=[1,2,3]
49 # for x in a:
50 #     if  x == 2:
51 #         break
52 #     print(x)
53 # else:
54 #     print('EOF')#不会打印EOF
55 
56 a=[['a', 'b', 'c'], (1, 2, 3)]
57 for x in a:
58     for y in x:
59         if y=='b':
60             break
61         print(y)
62 else:
63     print('gone')#gone会打印,break的作用域仅限于内层循环

三、for与range

1、关键点

1)range可按我们知道的规则生成序列

2)for循环与range结合,可实现按次数遍历

3)range与切片

 1 # coding=utf-8
 2 
 3 # for x in range(0,10):#包含0不包含10
 4 #     print(x)
 5 
 6 
 7 # for x in range(0,10,2):#第三个参数2指定步长
 8 #     # print(x)
 9 #     print(x,end='|')#等差数列
10 
11 
12 # for x in range(10,0,-2):#改变方向
13 #     print(x,end='|')#递减等差数列
14 
15 # range和切片
16 # 打印a中相间隔的元素
17 a = [1, 2, 3, 4, 5, 6, 7, 8]
18 # 方法一:for循环
19 for i in range(0, len(a), 2):
20     print(a[i], end='|')
21 
22 # 方法二:切片
23 b=a[0:len(a):2]
24 print(b)

四、python项目的组织结构

五、包与模块的命名

1、命名

包的名字:文件夹的名字

模块的名字:文件名

包.模块

区别:不同包下相同名字的模块,命名空间不同

2、关系

3、包的构成

文件夹+__init__.py=包

 __init__模块的名字就是包名,而不是包名.__init__

六、多个模块间相互调用

好的结构是,提取公共代码在一个模块中,由其他模块调用。

引入方式:import 模块

1、python是解释型语言,使用前需要先定义

2、需要先引入模块,再使用模块中的方法、变量等

3、同级模块间调用,直接import 模块名;不同级模块调用,import 包.模块;多层调用,import 包.子包.模块,可使用别名as 简化调用

4、import后只能跟模块名,不能跟模块中的类、变量

七、from...import...导入

1、基本用法from 包/模块 import 模块/变量、类、方法

2、导入多个:from module import a,b,c,d

3、导入所有:from module import *

4、__all__内置属性控制*的范围

在c4.py中如下定义

1 __all__=['a','b']
2 
3 a = 1
4 b = 2
5 c = 3

c6.py调用c时,就会报错

from c4 import *

print(a, b, c)
"C:\Program Files\Python36\python3.exe" E:/pyClass/seven/c6.py
Traceback (most recent call last):
  File "E:/pyClass/seven/c6.py", line 3, in <module>
    print(a, b, c)
NameError: name 'c' is not defined

九、__init__.py的用法

1、如何一次导入多个模块

from moudle import a,b,c,d....

1)代码换行

python一行最多允许80个字符

方法一:\

from c4 import a,b,\

c

方法二:()

from c4 import (a,b,

c)

2、__init__.py文件的作用

当包或模块被导入时,系统会自动先执行包下的__init__文件。

common的__init__文件中输入以下内容:

print("this is __init__.py")

 c6.py导入c7文件

from common import c7

 执行c6.py

"C:\Program Files\Python36\python3.exe" E:/pyClass/seven/c6.py
this is __init__.py

Process finished with exit code 0

3、在__init__文件中使用__all__内置变量

c7.py中代码如下:

1 __all__ = ['a', 'c']
2 
3 a = 2
4 c = 3
5 d = 4

c8.py中代码如下:

1 e = 2
2 f = 3
3 g = 4

__init__.py中代码如下

__all__=['c7']

c6.py中代码:

1 from common import *
2 
3 print(c7.a)
4 print(c7.c)
5 print(c7.d)
6 print(c8.e)

运行结果

"C:\Program Files\Python36\python3.exe" E:/pyClass/seven/c6.py
Traceback (most recent call last):
2
  File "E:/pyClass/seven/c6.py", line 4, in <module>
    print(c8.e)
NameError: name 'c8' is not defined

4、__init__可完成批量导入

例如:c9.py中需要导入多个类库

import sys
import datetime
import io


print(sys.path)

在c10.py、c11.py、c12.py、c13.py、c14.py...都需要导入以上类库,那么可以将以上类库放在t包的__init__.py文件中,其他文件导入t即可

t的init文件输入以下内容

import sys
import datetime
import io

 其他文件,导入t即可

import t
print(t.sys.path)

 十、包与模块的几个常见错误

1、包和模块是不会被重复导入的

2、避免循环导入

3、python中导入某个模块时,会执行该模块内的代码

4、入口文件的概念

 

posted @ 2018-07-31 11:05  小白2510  阅读(394)  评论(0编辑  收藏  举报