社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
学过c和c++的同学一定要注意‼️while语句是可以有else语句的
例子:
count = 0
while count < 5:
print (count,"is less than 5")
count += 1
else:
print (count,"is more than 5")
1.Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
例一:遍历一个字符串
for letter in "Python":
print("当前字母",letter)
例二:遍历一个列表
fruits = ['banana','apple','lemon']
for fruit in fruits:
print('当前水果:',fruit)
print('bye')
3.通过索引执行循环
注意:?下面要用到range函数,不了解range函数的同学可以先看我的另一篇文章
传送门:https://blog.csdn.net/weixin_43118073/article/details/96858564
fruits = ["banana","apple","lemon"]
for index in range(len(fruits)):
print(fruits[index])
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行
while … else 也是一样。
#练习:判断10-20的质数
for num in range(10,20):
for i in range(2,num):
if num%i==0:
j = num/i
print ('%d等于%d*%d' % (num,i,j))
break
else:
print (num, '是一个质数')
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!