包、模块、函数
一、while循环与使用场景
1、while循环:
counter = 1
while counter <= 10:
counter += 1
print(counter)
else:
print("EOF")
运行结果:
2
3
4
5
6
7
8
9
10
11
EOF
2、使用场景:
递归
二、for与for-else循环
for循环主要用来遍历/循环 序列或者集合、字典。
1、for循环:
a = [["apple", "orange", "banana", "grape"], (1, 2, 3)]
for x in a:
for y in x:
print("y: %s" % y)
运行结果:
y: apple
y: orange
y: banana
y: grape
y: 1
y: 2
y: 3
2、for-else循环:
a = [["apple", "orange", "banana", "grape"], (1, 2, 3)]
for x in a:
for y in x:
print("y: %s" % y)
else:
print("fruit is gone")
运行结果:
y: apple
y: orange
y: banana
y: grape
y: 1
y: 2
y: 3
fruit is gone
3、break:
# 示例1:
a = [1, 2, 3]
for x in a:
if x == 2:
break
print(x)
运行结果:
1
# 示例2:
a = [1, 2, 3]
for x in a:
if x == 2:
break
print(x)
else:
print("EOF")
运行结果:
1
# 示例3:
a = [["apple", "orange", "banana", "grape"], (1, 2, 3)]
for x in a:
for y in x:
if y == "orange":
break
print("y: %s" % y)
else:
print("fruit is gone")
运行结果:
y: apple
y: 1
y: 2
y: 3
fruit is gone
4、continue:
# 示例1:
a = [1, 2, 3]
for x in a:
if x == 2:
continue
print(x)
运行结果:
1
3
# 示例2:
a = [1, 2, 3]
for x in a:
if x == 2:
continue
print(x)
else:
print("EOF")
运行结果:
1
3
EOF
# 示例3
a = [["apple", "orange", "banana", "grape"], (1, 2, 3)]
for x in a:
for y in x:
if y == "orange":
continue
print("y: %s" % y)
else:
print("fruit is gone")
运行结果:
y: apple
y: banana
y: grape
y: 1
y: 2
y: 3
fruit is gone
三、for与range
# 示例1:
for x in range(0, 10):
print(x, end=" | ")
运行结果:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
# 事例2:
for x in range(0, 10, 2):
print(x, end=" | ")
运行结果:
0 | 2 | 4 | 6 | 8 |
# 示例3:
for x in range(10, 0, -2):
print(x, end=" | ")
运行结果:
10 | 8 | 6 | 4 | 2 |
四、Python工程的组织结构:包、模块儿、类
详图:
区分包和文件夹:包的下面存在有__init__.py文件。
五、Python导入模块
例如:import random.randint
六、from import导入变量
例如:from random import randint
七、__init__.py的用法
1、引入包、包下面模块的变量,包中的__init__.py会自动被执行
(1) 示例一:
目录结构如下:
a、__init__.py:
a = "this is __init__.py file"
print(a)
b、 test1.py:
import chapter02
运行结果:
(2)示例二:
目录结构如下:
a、__init__.py:
a = "this is __init__.py file"
print(a)
b、test.py:
mobile = "1111"
name = "2222"
c、test2.py:
from chapter02.test import mobile
运行结果:
(3) 应用场景:包和模块的初始化。
2、
目录结构:
a、__init__.py:
__all__ = ["test"]
b、test.py:
mobile = "1111"
name = "2222"
c、dev.py:
age = "man"
address = "beijin"
d、test3.py:
from chapter02 import *
print(test.mobile)
print(dev.age)
运行结果:
3、批量导入:
目录结构:
a、__init__.py:
import sys
import datetime
import io
b、test4.py:
import chapter02
print(chapter02.sys.path)
运行结果:
八、包与模块的几个常见错误
1、包和模块不会被重复导入
2、避免循环导入
(1) 示例一:
目录结构:
a、test.py
from dev import age
mobile = "1111"
name = "2222"
b、dev.py
from test import mobile
age = "man"
address = "beijin"
print(mobile)
运行结果:
(2) 示例二:
目录结构:
(a) test.py
mobile = "1111"
print(mobile)
(b) dev.py
from test import mobile
运行结果:
九、模块内置变量
1、dir函数返回当前模块的变量:
(1) 7_1.py:
a = 1
b = 2
infos = dir()
print(infos)
(2) 运行结果:
(3) 7_4.py:
import sys
infos = dir(sys)
print(infos)
(4) 运行结果:
2、内置变量:
(1) 目录结构:
(2) 7_3.py:
from p import p1
(3) p1.py:
"""
This is 7_2.py doc
"""
print("name: " + __name__)
print("package: " + __package__)
print("doc: " + __doc__)
print("file: " + __file__)
(4) 运行结果:
十、入口文件和普通模块内置变量的区别
1、示例一:
入口文件:7_3.py;普通模块:p1.py
(1) 目录结构:
(2) p1.py:
"""
This is p1.py doc
"""
print("name: " + __name__)
print("package: " + (__package__ or '当前模块不属于任何包'))
print("doc: " + (__doc__ or '当前模块没有文档注释'))
print("file: " + __file__)
(3) 7_3.py:
from p import p1
print("------------7_3.py---------------")
print("name: " + __name__)
print("package: " + (__package__ or '当前模块不属于任何包'))
print("doc: " + (__doc__ or '当前模块没有文档注释'))
print("file: " + __file__)
(4) 运行结果:
2、事例二:
入口文件:p1.py
(1) 目录结构:
(2) p1.py:
"""
This is p1.py doc
"""
print("name: " + __name__)
print("package: " + (__package__ or '当前模块不属于任何包'))
print("doc: " + (__doc__ or '当前模块没有文档注释'))
print("file: " + __file__)
(3) 运行结果:
3、示例三:
普通模块:7_3.py
(1) 7_3.py:
from chapter07.p import p1
print("------------7_3.py---------------")
print("name: " + __name__)
print("package: " + (__package__ or '当前模块不属于任何包'))
print("doc: " + (__doc__ or '当前模块没有文档注释'))
print("file: " + __file__)
(2) p1.py:
"""
This is p1.py doc
"""
print("name: " + __name__)
print("package: " + __package__)
print("doc: " + __doc__)
print("file: " + __file__)
(3) 运行结果:
十一、__name__的经典应用
1、示例一:
(1) 目录结构:
(2) c7_4.py:
if __name__ == "__main__":
print("This is app")
print("This is a module")
(3) c7_5.py:
import c7_4
(4) 运行结果:
2、示例二:
(1) 目录结构:
(2) c7_4.py:
if __name__ == "__main__":
print("This is app")
print("This is a module")
(3) 运行结果:
十二、相对导入和绝对导入
目录结构:
1、入口文件中不可以使用相对路径导入其他模块。
(1) main.py:
# 绝对路径,从顶级包依次往下点
from package2.package4.m2 import m
(2) m2.py:
from .m3 import m
from ..m4 import m
m = 2
print("m: %s" %m)
print(__package__)
(3) m3.py:
m = 3
print("m: %s" %m)
print(__package__)
(4) m4.py:
m = 4
print("m: %s" %m)
print(__package__)
(5) 运行结果:
2、将入口文件当做普通模块运行的话,可以使用相对路径导入其他模块。
(1) main.py:
# 相对路径:.表示同级;..表示上一级;..表示上上级
from .package2.package4.m2 import m
(2) m2.py:
from .m3 import m
from ..m4 import m
m = 2
print("m: %s" %m)
print(__package__)
(3) m3.py:
m = 3
print("m: %s" %m)
print(__package__)
(4) m4.py:
m = 4
print("m: %s" %m)
print(__package__)
(5) 运行结果: