Python 使用 while 循环打印 1 到 10
我们将使用 Python 的 while 循环来打印从 1 到 10 的数字。while 循环会一直执行,直到给定的条件不再满足为止。
实例
i = 1
while i <= 10:
print(i)
i += 1
while i <= 10:
print(i)
i += 1
代码解析:
i = 1:初始化变量i,并将其值设置为 1。while i <= 10::这是一个while循环,只要i的值小于或等于 10,循环就会继续执行。print(i):在每次循环中,打印当前i的值。i += 1:在每次循环结束后,将i的值增加 1。
输出结果:
1 2 3 4 5 6 7 8 9 10
Python3 实例
点我分享笔记