iT邦幫忙

第 12 屆 iThome 鐵人賽

1

Python程式語言總共有二種迴圈的語法。第一種是"for"迴圈,另外一個則是"while"。

讓我們來看看以下的程式碼吧~

[In]
x=0
numbers=[]

while x <6: #當變數大於6中斷迴圈
    print(f"At the top is  {x}")
    numbers.append(x) #把X增加到串列裡面
    x=x+1
    print("Numbers now:"),numbers
    print(f"{x} is at the bottom")
    print("The numbers:")
for number in numbers:


     print(number)
[Out]
At the top is  0
Numbers now:
1 is at the bottom
The numbers:
At the top is  1
Numbers now:
2 is at the bottom
The numbers:
At the top is  2
Numbers now:
3 is at the bottom
The numbers:
At the top is  3
Numbers now:
4 is at the bottom
The numbers:
At the top is  4
Numbers now:
5 is at the bottom
The numbers:
At the top is  5
Numbers now:
6 is at the bottom
The numbers:
0
1
2
3
4
5

總結:

while和for最大的不一樣就是while-loop會直到他對應的布林表達式為False才會停下來。

Source:

https://ppt.cc/fopYQx
https://ppt.cc/f23jJx


上一篇
Day10:串列的循環
下一篇
Day12:字典(dictionary)
系列文
笨方法學python 大家來一起開心學python XD12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言