Python(五) 包、模块、函数与变量作用域

一、while循环与使用场景

CONDITION=1

while CONDITION <=5 :
    CONDITION +=1
    print("hello")
else:
    print("EOF")
    
hello
hello
hello
hello
hello
EOF

 

二、for与for-else循环

# 主要是用来遍历/循环 序列或者集合、字典

a=[["a","b","c","d"],(1,2,3)]

for x in a:
    for y in x:
        print(y)
       # print(y,end='') #end 在一行 打印
else:
    print("fruit  is gone")
结果:

a
b
c
d
1
2
3
fruit  is gone

break 终止当前循环

continue 终止出本次循环

a=[["a","b","c","d"],(1,2,3)]

for x in a:
    for y in x:
        if y == 'b':
            break
        print(y)
else:
    print("fruit  is gone")

a
1
2
3
fruit  is gone

 

a=[["a","b","c","d"],(1,2,3)]

for x in a:
    for y in x:
        if y == 'b':
            continue
        print(y)
else:
    print("fruit  is gone")

a
c
d
1
2
3
fruit  is gone

 

三、for 与 range

for x in range(0,10,2):
    print(x, end=' | ' )
0 | 2 | 4 | 6 | 8 |

 

a=[1,2,3,4,5,6,7,8]

for i in range(0,len(a),2):
    print(a[i],end=' | ')

1 | 3 | 5 | 7 |

 

b=a[0:len(a):2]
print(b)

[1, 3, 5, 7]

 

四、新篇章导言

高性能、封装性(可复用)、抽象

直白、美与不美

 

五、Python工程的组织结构:包、模块儿、类

包 模块 类  函数、变量

 

六、Python包与模块的名字

包的名字就是文件夹的名字  

模块的名字就是文件的名字

在一个文件夹里面存在 _init_.py  文件 就是包 

 

七、import导入模块

import 模块名 as 自定义命名

 

八、from import 导入变量

from  包名 /模块名 import 具体变量名/模块名/*(全部引入)

__all__ =['a','c']  模块的内置导入变量。

 

九、__init__.py 的用法

VSCode  去掉 __pycache__ 文件夹,在设置里面搜索

"files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/__pycache__":true
    }

from c1 import a,b,\c

from c1 import (a,b,c) 换行

__init__.py  在导入包或者包中的模块的时候 自动运行包包含其中的 __init__.py文件

批量导入包

 

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

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

避免循环导入

 

 

十一、模块内置变量

十二、入口文件和普通模块内置变量的区别

十三、__name__的经典应用

十四、相对导入和绝对导入 一

十五、相对导入和绝对导入 二

posted @ 2018-08-16 22:21  革凡  阅读(481)  评论(0编辑  收藏  举报