7.包,模块,函数与变量作用域

while循环与使用场景

破解密码:暴力破解,其实就是枚举
python可以利用循环
for循环
while循环

新建文件夹为six,将之前的py文件放入six之中
新建文件夹为seven,新建py文件c1.py放入seven中
其中,six seven 文件夹都在D盘的Python文件夹下

condition = True

while condition :
    print('I am a while')

要运行这段代码,先输入cd seven ,进入seven文件夹
然后按照以前的,输入python c1.py,然后按tab键补全
运行后可以再按ctrl + C 强行终止

下面这段代码来说明while的运行机制

# condition = True

counter = 1

while counter :
    counter += 1
    print(counter)

while检测到条件为1,进行print并且couner + 1,之后
while再次进行检测,进行上述轮回,这样再运行时就会刷屏

下面演示while的正确用法,注意每次修改代码都要ctrl + S 进行保存,否则运行时并不会自动保存

# condition = True

counter = 1

while counter <= 10 :
    counter += 1
    print(counter)

所以,如果要正确使用while循环,即要得到不会死循环的结果
必须满足下列条件:
while的condition不可以是常量

上述代码还可以进行优化为

counter = 1

while counter <= 10 :
    counter += 1
    print(counter)
else :
    print('EOF')

for 和 for else 循环

新建c2.py在seven文件夹下

for 循环主要用来遍历循环,序列或者集合,字典

for 循环用来遍历:

a = ['apple','orange','banana','grape']
for x in a:
    print(x)

嵌套使用

a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
    print(x)
a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
    for y in x:
        print(y)

横向打印

a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
    for y in x:
        print(y,end='')

for循环遍历结束后,执行else语句

a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
    for y in x:
        print(y)
else :
    print('fruit is gone')

终止for循环

a = [1,2,3]
for x in a :
    if x == 2:
        break
    print(x)

break强行终止循环,之后的循环也不再执行,
continue终止当前循环,后续循环继续

a = [1,2,3]
for x in a :
    if x == 2:
        continue
    print(x)
a = [1,2,3]
for x in a :
    if x == 2:
        break
    print(x)
else :
    print('EOF')
a = [1,2,3]
for x in a :
    if x == 2:
        continue
    print(x)
else :
    print('EOF')

for 与 range

使用break跳出了内部的for循环,外部的循环还在继续

a = [['apple','orange','banana','grape'],(1,2,3)]
for x in a:
    for y in x:
        if y == 'orange':
            break
        print(y)
else :
    print('fruit is gone')

新建c3.py
range生成了序列,0表示开始数字,10表示数字总共数目

for x in range(0,10):
    print(x)

生成有间隔的序列

for x in range(0,10,2):
    print(x)

生成有间隔的序列,并且横向打印

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

生成递减数列

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

新篇章导言

新建c4.py

a = [1,2,3,4,5,6,7,8]
for i in range(0,len(a),2):
    print(a[i],end = ' | ')

序列的切片来实现

a = [1,2,3,4,5,6,7,8]
""" for i in range(0,len(a),2):
    print(a[i],end = ' | ') """


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

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

python工程的组织结构:包,模块,类

包,即一个文件夹
模块是.py文件

函数和变量

Python包与模块的名字

文件夹下还可以建立文件夹,包下建立子包
含有_init_.py这样一个py文件的文件夹才是真正的包
该init文件的文件名即seven(它所在的文件夹)

import 导入模块

在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片
在这里插入代码片

2级标题

在这里插入代码片
在这里插入代码片
在这里插入代码片

2级标题

在这里插入代码片
在这里插入代码片
在这里插入代码片

2级标题

在这里插入代码片
在这里插入代码片
在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值