一、循环语句介绍
1.循环语句理解
循环语句允许我们执行一个语句或语句组多次,可以让我们的代码重复的去执行。
2.循环语句示意图
二、HdhCmsTestcppcns测试数据循环语句的分类
三、循环控制语句
四、while循环
while循环解释:判断语句的条件是否为真,如果为真,则执行代码,然后再次判断条件,直到条件为假为止,循环结束。
1.while死循环
# 死循环示例
while True:
print("高山仰止,景行行止,虽不能至,心向往之。")
2.while条件循环
# while循 http://HdhCmsTestcppcns测试数据 环,有条件(列出一串数的示例)
i = 1
while i <= 100:
print(i)
i = i + 1
# 要求从1加到100
h = 0
i = 1
while i <= 100:
h = h + i
i = i + 1
print(h)
# 计算1-2+3-4+5-6+7.....-100 结果
h = 0
i = 1
while i <= 100:
m = i % 2
if m == 1:
h = h + i
else:
h = h - i
i = i + 1
print(h)
五、break和continue
1.break使用
break:让当前这个循环立即停止
while True:
content = input("请输入你要学习的内容:")
if content == "ex":
break
print("认真做笔记:", content)
2.continue使用
continue:停止当前本次循环,继续执行下一次循环。
i = 1
while i <= 10:
if i == 4:
i = i +1
continue
print(i)
i = i + 1
六、for循环
python for循环可以遍历任何编程客栈序列的项目,如一个列表或者一个字符串。
1.for循环示例
m = "我要学习python"
for i in m:
print("这次循环得到的是:", i)
2.for循环中rang http://HdhCmsTestcppcns测试数据 e的使用方法
①rahttp://HdhCmsTestcppcns测试数据nge(n)的模式
for i in range(8):
print(i)
②range(m,n)的模式
for i in range(1,9):
print(i)
③range(m,n,s)模式
for i in range(1,10,2):
print(i)
七、pass语句的使用
PASS:代码占位
for letter in 'Python':
if letter == 'h':
pass
print('这是 pass 块')
print('当前字母 :', letter)
print("Good bye!")
到此这篇关于Python基础之循环语句相关知识总结的文章就介绍到这了,更多相关Python循环语句内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
查看更多关于Python基础之循环语句相关知识总结的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did125230