官术网_书友最值得收藏!

A while loop

while loops are used when a specific task is supposed to be executed until a specific condition is met. while loops are commonly used to execute code in an infinite loop. Let's look at a specific example where we would like to print the value of i from 0 to 9:

i=0 
while i<10:
print("The value of i is ",i)
i+=1

Inside the while loop, we increment i by 1 for every iteration. The value of i is incremented as follows:

i += 1

This is equivalent to i = i+1.

This example would execute the code until the value of i is less than 10. It is also possible to execute something in an infinite loop:

i=0 
while True:
print("The value of i is ",i)
i+=1

The execution of this infinite loop can be stopped by pressing Ctrl C on your keyboard.

It is also possible to have nested while loops:

i=0 
j=0
while i<10:
while j<10:
print("The value of i,j is ",i,",",j)
i+=1
j+=1

Similar to for loops, while loops also rely on the indented code block to execute a piece of code.

Python enables printing a combination of strings and integers as long as they are presented as arguments to the print function separated by commas. In the earlier-mentioned example, The value of i,j is, i are arguments to the print function. You will learn more about functions and arguments in the next chapter. This feature enables formatting the output string to suit our needs.

主站蜘蛛池模板: 东城区| 万载县| 石家庄市| 上思县| 嘉鱼县| 德清县| 杂多县| 九龙坡区| 肥东县| 张北县| 鄯善县| 株洲市| 松阳县| 柯坪县| 栖霞市| 阿尔山市| 桑日县| 盱眙县| 柳州市| 株洲县| 旌德县| 三门县| 梁平县| 新化县| 吴堡县| 城市| 岱山县| 平顶山市| 吉木萨尔县| 天水市| 襄垣县| 永清县| 偏关县| 西乌珠穆沁旗| 榆树市| 浦东新区| 永安市| 兴国县| 富裕县| 奈曼旗| 南靖县|