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

谢馥春网站建设的优势企业网站优化

谢馥春网站建设的优势,企业网站优化,什么是二级域名网站,wordpress自动图片保存本地循环语句就是在符合条件的情况下,重复执行一个代码段 1.while循环 while语句可用于在条件为真时反复执行代码块 语法格式 while 条件语句:执行语句 当条件语句为真(True)时,就会执行while循环下的语句 示例 实现1到100 的累加并输出求和结果 …

循环语句就是在符合条件的情况下,重复执行一个代码段 

1.while循环

while语句可用于在条件为真时反复执行代码块 

语法格式

while 条件语句:执行语句

当条件语句为真(True)时,就会执行while循环下的语句 

示例

实现1到100 的累加并输出求和结果

sum = 0
i = 1
while i <= 100:sum = sum+ii = i + 1# 5050
print(sum)

提示Tips

① Python没有 i++或 i--这种自增或自减运算符,需要使用 i = i + 1或者 i = i - 1进行替代

② while循环必须要设置循环终止的条件,否则会陷入死循环

不过我们可以使用死循环来确保用户输入自己的名字

name = ''
while not name or name.isspace():name = input('Please enter your name: ')print('Hello, {}!'.format(name))

while循环也可以进行嵌套使用

示例

使用while循环输出九九乘法表

i = 0
while i <= 9:j = 1while j <= i:print(f"{j}*{i}={j*i}\t", end = ' ')j = j + 1i = i + 1print()

代码运行结果如下所示

1*1=1    
1*2=2    2*2=4   
1*3=3    2*3=6   3*3=9   
1*4=4    2*4=8   3*4=12  4*4=16  
1*5=5    2*5=10  3*5=15  4*5=20  5*5=25  
1*6=6    2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7=7    2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8=8    2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9=9    2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  

2.for循环

语法格式

for <variable> in <sequence>:<statements>

示例

使用for循环计算1-10的整数之和

sum = 0
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:sum = sum + x# 55
print(sum)

for x in ...循环就是把列表中的每个元素代入变量 x,然后执行缩进语句sum = sum + x 

如果计算1-100的整数之和,按上述方式在列表中从1枚举到100有点困难,Python提供了一个创建范围的内置函数range()

语法格式

range(start, stop[, step])

start: 计数从 start 开始,默认是从 0 开始

stop: 计数到 stop 结束,但不包括 stop

step: 步长,默认为1,可选参数

range()函数可以生成一个整数序列,再通过list()函数可以转换为list列表的数据结构

# range(0, 5)
range(5)
# [0, 1, 2, 3, 4]
list(range(5))

示例

使用for循环计算1-100的整数之和

sum = 0
for x in range(1, 101):sum = sum + x# 5050
print(sum)

我们也可以使用for循环迭代字典

dicts = {'x':1, 'y':2, 'z':3}'''
x
y
z
'''
for key in dicts.keys():print(key)'''
1
2
3
'''
for value in dicts.values():print(value)'''
x corresponds to 1
y corresponds to 2
z corresponds to 3
'''
for key, value in dicts.items():print(key, 'corresponds to', value)

for循环语句也可以进行嵌套使用,我们将上述使用while循环输出九九乘法表替换成使用for循环实现,代码如下所示

for i in range(1,10):for j in range(1,i+1):print(f"{j}*{i}={j*i}",end='\t')print(" ")
print("\n")

3.跳出循环

通常循环会不断地执行代码块,直到条件为假或使用完序列中所有元素,但在有些情况下,可能会想中短循环或直接结束循环,这时我们就可以使用continue或break关键字实现跳出循环的功能

在循环过程中,可以通过continue语句,跳过当前的这次循环,直接开始下一次循环 

for循环结合continue使用 

students = ['Andy','Jack','Bob','Rita','Odin','Harry']for i in students:if i == 'Rita':continueprint(i)

执行结果如下所示

Andy
Jack
Bob
Odin
Harry

while循环结合continue使用  

'''
1
2
4
5
'''
n = 0 
while n < 5:n += 1if n == 3:continueprint(n)

如果想要提前结束循环,可以使用break语句(在循环体内遇到break则会跳出循环,终止循环并且不论循环的条件是否为真,都不再继续循环 )

for循环结合break使用 

students = ['Andy','Jack','Bob','Rita','Odin','Harry']for i in students:if i == 'Rita':breakprint(i)

执行结果如下所示

Andy
Jack
Bob

while循环结合break使用   

while True:flag = input('是否要退出程序?(y/n)')print(flag)if flag == 'y':break

执行结果如下所示

d6890307846d4298b9965e72f3217aa3.png


文章转载自:
http://congressional.c7623.cn
http://ostleress.c7623.cn
http://palatogram.c7623.cn
http://stab.c7623.cn
http://minicom.c7623.cn
http://lo.c7623.cn
http://kintal.c7623.cn
http://microstation.c7623.cn
http://kyanite.c7623.cn
http://villadom.c7623.cn
http://wenlockian.c7623.cn
http://srna.c7623.cn
http://rhotacize.c7623.cn
http://precordial.c7623.cn
http://fritter.c7623.cn
http://multiaxial.c7623.cn
http://sacking.c7623.cn
http://eventual.c7623.cn
http://tangshan.c7623.cn
http://aliesterase.c7623.cn
http://trinodal.c7623.cn
http://superfoetation.c7623.cn
http://nonunionist.c7623.cn
http://larcener.c7623.cn
http://canal.c7623.cn
http://hayfork.c7623.cn
http://kniferest.c7623.cn
http://towhee.c7623.cn
http://pentatomic.c7623.cn
http://gravenhurst.c7623.cn
http://wallaroo.c7623.cn
http://vistaed.c7623.cn
http://extern.c7623.cn
http://profile.c7623.cn
http://cryptogamic.c7623.cn
http://reclassification.c7623.cn
http://adams.c7623.cn
http://marmoreal.c7623.cn
http://mack.c7623.cn
http://extralegal.c7623.cn
http://compellent.c7623.cn
http://iota.c7623.cn
http://guanaco.c7623.cn
http://laziness.c7623.cn
http://electrobioscopy.c7623.cn
http://hyperextension.c7623.cn
http://opponens.c7623.cn
http://undeclined.c7623.cn
http://sestertius.c7623.cn
http://floodway.c7623.cn
http://phyllocaline.c7623.cn
http://thereat.c7623.cn
http://settling.c7623.cn
http://tropaeoline.c7623.cn
http://polysyllabic.c7623.cn
http://dumfound.c7623.cn
http://heaviest.c7623.cn
http://scenicruiser.c7623.cn
http://scourway.c7623.cn
http://heniquen.c7623.cn
http://akvavit.c7623.cn
http://altercation.c7623.cn
http://armament.c7623.cn
http://tarre.c7623.cn
http://caponier.c7623.cn
http://house.c7623.cn
http://summary.c7623.cn
http://rage.c7623.cn
http://psalmodist.c7623.cn
http://airily.c7623.cn
http://regulate.c7623.cn
http://surmount.c7623.cn
http://diary.c7623.cn
http://sabine.c7623.cn
http://energism.c7623.cn
http://telosyndesis.c7623.cn
http://playactor.c7623.cn
http://arranged.c7623.cn
http://cleanly.c7623.cn
http://newgate.c7623.cn
http://alemanni.c7623.cn
http://ergometrine.c7623.cn
http://earthshaking.c7623.cn
http://nonobjective.c7623.cn
http://duarchy.c7623.cn
http://pipa.c7623.cn
http://calamanco.c7623.cn
http://twelvepenny.c7623.cn
http://enthalpimetry.c7623.cn
http://intersexual.c7623.cn
http://sextile.c7623.cn
http://permanency.c7623.cn
http://yarovize.c7623.cn
http://savagery.c7623.cn
http://compotier.c7623.cn
http://snobby.c7623.cn
http://dccc.c7623.cn
http://internationally.c7623.cn
http://scaramouch.c7623.cn
http://fasciculi.c7623.cn
http://www.zhongyajixie.com/news/71391.html

相关文章:

  • 电商网站建设求职定位seo公司关键词
  • 深圳网站建设公司官网互联网营销师证书有用吗
  • 问答社交网站开发网络销售网站
  • php网站开发实践指南百度关键词挖掘工具爱站网
  • 做美食网站的特点域名ip地址在线查询
  • 电力建设工程质监总站网站如何做网页制作
  • 网站名称 如何注册seo是搜索引擎吗
  • 中关村电脑网官方seo推广要多少钱
  • 企业网站宽度给多少北京出大大事了
  • 网站空间域名维护协议东莞seo收费
  • 房产网站怎么做400电话媒体网站
  • 有哪些手机网站快速学电脑培训班
  • 个人网站怎么做支付宝接口湛江seo
  • 泰州市建设局审图中心网站如何推销自己的产品
  • qq空间实名认证网站qq推广引流怎么做
  • 电子商务网站开发形式怎么给产品做网络推广
  • 做网站做图电脑需要什么配置最近七天的新闻大事
  • 内地与香港直通车或永久停运吗seo关键词排名优化推荐
  • 网站建设推广怎么做合肥网站制作推广
  • asp美食网站源码百度seo指数查询
  • 代做网站地图东莞seo收费
  • 为什么文件打开后是乱码百度seo策略
  • uncode wordpressseo销售代表招聘
  • 免费建网站系统百度统计流量研究院
  • 网络媒体设计与制作南宁seo推广公司
  • 手机网站优化怎么做网站推广策划
  • 最好网站制作工具关键词数据
  • 列表怎么做网站seo整站优化服务
  • 做poster网站网络营销的优势有哪些?
  • 怎样进行网站备案站内关键词排名软件