循环介绍

一、生活中的循环

image.png

二、软件开发中循环的使用场景

打印一万遍”hello world”

  1. print("hello world")
  2. print("hello world")
  3. print("hello world")
  4. ...(还有99997遍)...

使用循环语句一句话搞定

i = 0
    while i < 10000:
        print("hello world")
        i += 1

三、小总结

当一段代码需要重复执行多次的时候,我们可以使用循环语句来完成。在Python中,有一下两种循环语句可以使用:

  • while 循环
  • for 循环