Python学习笔记5:循环结构

五、循环结构

1.while

count = 0

while count < 5:

    print("hello world")

    count += 1  # count = count + 1

2.for

for i in range(5):

    print(i)

扫描二维码关注公众号,回复: 397690 查看本文章

else:

    print("结束执行....")

# range(end): 0~end-1
# range(start, end): start~end-1
# range(start, end, step): start~end-1, 步长为step

# range(1,10,2)  # 1, 1+2, 1+2+2,

3. 跳出循环

break:跳出循环

continue:跳过本次循环

#continue和break的区别


4.死循环

1)while True:

2)while 0<1:

3)while 1:

# if和while后面必须跟的是bool类型, 如果不是布尔类型,转化为bool类型
# print(bool("hello"))   # True
# print(bool(""))           # False

例题:


猜你喜欢

转载自blog.csdn.net/sangyumo/article/details/80098509