- Python Programming with Raspberry Pi
- Sai Yamanoor Srihari Yamanoor
- 254字
- 2021-07-02 23:48:45
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.
- 大數(shù)據(jù)管理系統(tǒng)
- 大數(shù)據(jù)戰(zhàn)爭(zhēng):人工智能時(shí)代不能不說(shuō)的事
- 精通MATLAB神經(jīng)網(wǎng)絡(luò)
- 返璞歸真:UNIX技術(shù)內(nèi)幕
- VMware Performance and Capacity Management(Second Edition)
- Google App Inventor
- 模型制作
- 樂(lè)高創(chuàng)意機(jī)器人教程(中級(jí) 下冊(cè) 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實(shí)踐指導(dǎo)叢書(shū))
- Mastering Elastic Stack
- Data Wrangling with Python
- Supervised Machine Learning with Python
- AWS Administration Cookbook
- 菜鳥(niǎo)起飛系統(tǒng)安裝與重裝
- 面向?qū)ο蟪绦蛟O(shè)計(jì)綜合實(shí)踐
- 從零開(kāi)始學(xué)C++