python 循环语句

python yekong 46℃

for循环

0到5


for i in range(5):
    print(i)

0-10,步进3


for i in range(0, 10, 3):
    print(i)

负数循环


for i in range(-10, -100, -30):
    print(i)

字符串循环


name = 'chengdu'
for x in name:
    print(x)

列表循环


a = ['aa', 'bb', 'cc', 'dd']
for i in range(len(a)):
    print(i, a[i])

while循环


i = 0

while i < 5:
    print("当前是第%d次执行循环" % (i + 1))
    print("i=%d" % i)
    i += 1

1-100的和


i = 0
sum = 0

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

else的使用


count = 0
while count < 5:
    print(count, '小于5')
    count += 1
else:
    print(count, '大于等于5')

break

跳出for和while的循环体

continue

跳出当前循环,进入下一个循环

pass

占位语句不作为

喜欢 (0)