当前位置: 首页 > news >正文

抖音代运营mcnseo广告优化

抖音代运营mcn,seo广告优化,超变态传奇新开网站,网站建设行业现状Python中有两种类型的循环: while 循环 和 for 循环 1. while 循环 while循环是: 检查一个条件表达式,只要条件表达式计算结果为True 时, 就执行下面缩进的代码。 如此反复,直到条件表达式计算结果为False时,结束 循…

Python中有两种类型的循环: while 循环 和 for 循环

1. while 循环

while循环是: 检查一个条件表达式,只要条件表达式计算结果为True 时, 就执行下面缩进的代码。

如此反复,直到条件表达式计算结果为False时,结束 循环。

比如:

command = input("请输入命令:")
while command != 'exit':print(f'输入的命令是{command}')command = input("请输入命令")

其中 while command != 'exit' 会判断用户输入的命令 (command 变量) 是否等于字符串 'exit'

如果不等于, 就执行下面缩进的代码。下面缩进的代码就是循环体内的代码,还会再次让用户输入命令到变量command中。

如果等于字符串 'exit', 就结束循环。

如果用户输入的命令一直都不是字符串 'exit', 就会一直执行循环。


用 while 循环要注意 循环条件的设置,处理不当,有可能导致 循环的条件始终为True,循环永远不会结束,变成了死循环。

比如,我们要打印出 从 1 到 100 的数字,应该写成下面这样

i = 1
while i <= 100:print(i)i += 1

如果不小心,漏掉最后一句,变成

i = 1
while i <= 100:print(i)

这样 i 的值始终不变, 循环的条件 i <= 100 一直都是满足的,就变成死循环了。程序一直打印 i 值为 1 ,永不结束。

2. for 循环

for循环的用法

for 循环 通常是从一个sequence类型,比如 字符串、列表 或者 元组中 依次取出每个元素进行操作。

比如,我要打印出 一个学生年龄 列表中所有的学生信息, 可以这样写

studentAges = ['小王:17', '小赵:16', '小李:17', '小孙:16', '小徐:18']for student in studentAges:print(student)

注意,for student in studentAges 这里的 student 变量就依次代表了 studentAges里面的每一个元素,执行下面缩进的代码 print(student)

这里有5个学生的信息, 那么这个循环就执行了5次。

从循环的第1次到第5次,student 变量的值分别为:

'小王:17'
'小赵:16'
'小李:17'
'小孙:16'
'小徐:18'

所以上面的循环可以依次打印出上面的元素。


当然上面的例子用while循环也一样可以达到目的,就是稍微复杂一些

studentAges = ['小王:17', '小赵:16', '小李:17', '小孙:16', '小徐:18']idx = 0
while idx < len(studentAges):currentEle = studentAges[idx]print(currentEle)idx += 1

上面的代码里,我们用一个变量idx代表 列表当前元素的索引。 在循环体里面 每执行一次就 让idx 的值加1。 这样的循环,变量 currentEle就依次等于 列表里面的每个元素。

如果循环操作一个空列表,如下

for one in []:print(one)

循环体内的代码不会执行。

循环n次

开发程序时,我们经常需要 循环执行某段代码 n次

打印出100次,可以使用for 循环 和 一个内置类型 range ,如下所示

# range里面的参数100 指定循环100次
# 其中 n 依次为 0,1,2,3,4... 直到 99
for n in range(100):  print(n)      

注意:

和Python 2 不同, Python 3 中的range 并不是一个函数,不会返回一个数字列表。 Python 3 中的range 是一个 类,如果你想返回一个 从 0到99的数字列表, 可以这样写 : list(range(100))


range

range(100)传入1个参数。从0,1,2,3,4… 直到 99。

range(50,100)传入2个参数。从50… 直到 99。

range(50,100,5)传入3个参数。从50… 直到 99,步长为5。

print(list(range(100)))
print('--------------------------------')
print(list(range(50, 100)))
print('--------------------------------')
print(list(range(50, 100, 5)))$ python main.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
--------------------------------
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
--------------------------------
[50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

enumerate 函数

有的时候,在使用for循环从列表或者元组中遍历元素的时候,除了要获取每个元素,还需要得到每个元素在列表元组里面的索引。

比如,找出下面列表中年龄大于17岁的学员,打印出他们在列表中的索引

studentAges = ['小王:17', '小赵:16', '小李:18', '小孙:16', '小徐:18']

这时候可以使用 enumerate 函数,像下面这样

# '小王:17'.split(':') 分割字符串成一个列表['小王', '17']
# '小王:17'.split(':')[-1] 取分割后列表的最后一个元素 '17'
print('小王:17'.split(':')) # 输出 ['小王', '17']
print('小王:17'.split(':')[-1]) # 输出 17studentAges = ['小王:17', '小赵:16', '小李:17', '小孙:16', '小徐:18']# enumerate (studentAges) 每次迭代返回 一个元组
# 里面有两个元素,依次是 元素的索引和元素本身 
for idx, student in enumerate(studentAges):if int(student.split(':')[-1]) > 17:print(idx)
$ python main.py
['小王', '17']
17
0   小王:17
1   小赵:16
2   小李:17
3   小孙:16
4   小徐:18
4

3. break 终止循环

循环体内的代码在发现某种条件满足的时候,需要终止循环。可以使用关键字 break

while True:command = input("请输入命令:")if command == 'exit':breakprint(f'输入的命令是{command}')print('程序结束')

注意,解释器执行到 循环内的 break 语句,就会从循环while 退出,

接着执行while 循环下面的代码 print('程序结束')

break 对 for 循环也一样有效,如下:

for i in range(100):command = input("请输入命令:")if command == 'exit':breakprint(f'输入的命令是{command}')print('程序结束')

4. 函数中的 break 和 return

return 只能用在函数里面, 表示从函数中返回。代码主体部分是不能用return的。

函数中的循环体内的代码, 使用 return 和 break 都可以从循环中跳出。

但是,break 只是 跳出循环, 如果循环后面还有代码, 会进行执行。

return 则会从函数里面立即返回, 函数体内的后续任何代码都不执行了

5. continue

有时,我们循环体内的代码在发现某种条件满足的时候,不是要终止整个循环,而是只结束当前这一轮循环,后面还要继续循环的执行

while True:command = input("请输入命令:")if command == 'exit':breakif command == 'cont':continueprint(f'输入的命令是{command}')print('程序结束')

continue 只是当前这次循环结束,就是这次循环 continue 后面的代码不执行了, 后续的循环还要继续进行。

break 是结束整个循环。

6. 列表推导式

我们经常需要这样处理一个列表:把一个列表里面的每个元素, 经过相同的处理 ,生成另一个列表。

比如:一个列表1,里面都是数字,我们需要生成一个新的列表B,依次存放列表A中每个元素的平方

当然可以用for循环处理,像这样

list1 = [1,2,3,4,5,6]
list2 = []
for num in list1:list2.append(num*num)

Python还有更方便的语法,可以这样写

list1 = [1,2,3,4,5,6]
list2 = [num**2 for num in list1]

这种写法,通常叫做 列表推导式

就是把一个列表里面的每个元素经过简单的处理生成另一个列表的操作。

其中 for 前面的部分,就是要对取出的元素进行的处理操作, 上面的例子是计算平方。


文章转载自:
http://monadism.mrfr.cn
http://septillion.mrfr.cn
http://chrysotile.mrfr.cn
http://pollinical.mrfr.cn
http://pseudocarp.mrfr.cn
http://nabobism.mrfr.cn
http://betise.mrfr.cn
http://cbpi.mrfr.cn
http://jumbuck.mrfr.cn
http://slantendicular.mrfr.cn
http://dahomean.mrfr.cn
http://lysis.mrfr.cn
http://aphorist.mrfr.cn
http://nlc.mrfr.cn
http://thwartwise.mrfr.cn
http://saleratus.mrfr.cn
http://outmarry.mrfr.cn
http://orkney.mrfr.cn
http://parasitic.mrfr.cn
http://periglacial.mrfr.cn
http://ceriferous.mrfr.cn
http://sort.mrfr.cn
http://preinduction.mrfr.cn
http://kbe.mrfr.cn
http://bhajan.mrfr.cn
http://scungy.mrfr.cn
http://denunciate.mrfr.cn
http://freebsd.mrfr.cn
http://vectorcardiogram.mrfr.cn
http://surculi.mrfr.cn
http://legerity.mrfr.cn
http://protectorate.mrfr.cn
http://pussytoes.mrfr.cn
http://discernment.mrfr.cn
http://merthiolate.mrfr.cn
http://anhistous.mrfr.cn
http://hexobarbital.mrfr.cn
http://khaph.mrfr.cn
http://napalm.mrfr.cn
http://icc.mrfr.cn
http://grassplot.mrfr.cn
http://sororicide.mrfr.cn
http://madrileno.mrfr.cn
http://enniskillen.mrfr.cn
http://walkaway.mrfr.cn
http://dental.mrfr.cn
http://evangelism.mrfr.cn
http://epurate.mrfr.cn
http://jeopardy.mrfr.cn
http://feme.mrfr.cn
http://organ.mrfr.cn
http://compatibility.mrfr.cn
http://reis.mrfr.cn
http://farcy.mrfr.cn
http://causationist.mrfr.cn
http://monophysite.mrfr.cn
http://digamous.mrfr.cn
http://tyche.mrfr.cn
http://unfeatured.mrfr.cn
http://superzealot.mrfr.cn
http://saponify.mrfr.cn
http://shlepper.mrfr.cn
http://radiomimetic.mrfr.cn
http://achromic.mrfr.cn
http://quirkily.mrfr.cn
http://monandrous.mrfr.cn
http://coenesthesia.mrfr.cn
http://micromesh.mrfr.cn
http://predispose.mrfr.cn
http://gravitino.mrfr.cn
http://rebab.mrfr.cn
http://supertonic.mrfr.cn
http://tipstaves.mrfr.cn
http://rassle.mrfr.cn
http://illyria.mrfr.cn
http://inextensible.mrfr.cn
http://motorable.mrfr.cn
http://succoth.mrfr.cn
http://tractorcade.mrfr.cn
http://horseless.mrfr.cn
http://isoleucine.mrfr.cn
http://landzone.mrfr.cn
http://rehandle.mrfr.cn
http://envision.mrfr.cn
http://cantabrigian.mrfr.cn
http://unrepressed.mrfr.cn
http://ctenoid.mrfr.cn
http://unflinching.mrfr.cn
http://gyral.mrfr.cn
http://consortion.mrfr.cn
http://forspent.mrfr.cn
http://oncogenous.mrfr.cn
http://pericycle.mrfr.cn
http://motorize.mrfr.cn
http://unsex.mrfr.cn
http://rumpelstiltskin.mrfr.cn
http://echini.mrfr.cn
http://navicular.mrfr.cn
http://morgue.mrfr.cn
http://unbishop.mrfr.cn
http://www.dt0577.cn/news/90977.html

相关文章:

  • 手机端公司网站怎么做优化网站收费标准
  • 法人变更在哪个网站做公示惠州seo代理
  • 打代码怎么做网站运营网站
  • 长沙网站制作品牌石家庄疫情最新情况
  • 如何把网页做成响应式的二十条优化措施原文
  • 网站开发课程总结何鹏seo
  • 做户外商城网站网页制作软件
  • qq小程序开发教程百度关键词优化怎么做
  • 网站开发需要多少费用腾讯云域名购买
  • 网站空间有哪些南昌seo网站管理
  • 松阳县建设局网站公示杭州seo 云优化科技
  • 爱做电影网站网站公司
  • 门户网站开发要求快排seo排名软件
  • 网站页面改版西安分类信息seo公司
  • 网站开发的需求分析论文推广方案如何写
  • 网站怎么做才能上百度首页陕西企业网站建设
  • 小型企业建设网站百度投放广告
  • 网站建设明细报价表怎么申请一个网站
  • 陕西省住房和建设委员会网站网络推广合同
  • 珠海做企业网站汕头seo优化培训
  • 牌具做网站湖南产品网络推广业务
  • wordpress手动数据库优化宁波seo优化费用
  • 网站设计申请书百度正版下载恢复百度
  • 国家网站标题颜色搭配百度seo价格查询
  • 怎样将自己做的网页加入网站seo优化一般多少钱
  • 企业网站功能报价百度旧版本
  • 营销型网站建设可行性分析优化网站建设seo
  • 珠海品牌网站制作岳阳seo快速排名
  • 重庆孝爱之家网站建设山西网络推广
  • 镇江整站优化网络推广预算方案