python學習筆記(3) 迴圈

2021-09-30 14:21:28 字數 879 閱讀 3230

python用冒號和縮進來代表**段

如:age = 3

if age >= 18:

print('your age is', age)

print('adult')

else:

print('your age is', age)

print('teenager')

if-else的完整版:

if 《條件判斷1>:

《執行1>

elif 《條件判斷2>:

《執行2>

elif 《條件判斷3>:

《執行3>

else:

《執行4>

注意python不是else if,而縮寫為了elif

input()返回的資料型別是str,str不能直接和整數比較,必須先把str轉換成整數。python提供了int()函式來完成這件事情

s = input('birth: ')

birth = int(s)

迴圈:(1)for...in迴圈

names = ['michael', 'bob', 'tracy']

for name in names:

print(name)

for x in ...迴圈就是把每個元素代入變數x,然後執行縮排塊的語句。

遍歷二維list:

testlist = [

[1,2],[2,3]

]print(testlist);

for i in  testlist:

for j in i:

print("%d " % j,end=""); #讓print之後不換行

print("");

(2) while

和c++類似,不多寫

python學習筆記3 迴圈1

1 while break continue 1 while語句 2 3while 判斷條件 4執行語句 5 6 count 0 7while count 9 8print the count is count 9 count count 1 1011 print good bye 1213 con...

機器學習 python篇學習筆記(3)迴圈結構

python中的迴圈結構主要分while和for兩種,與c語言相似 但是在具體細節上也有不同之處 在python中同樣需要事先申明變數,然後確定結束條件 while true 即在while後為迴圈條件,不滿足條件即退出迴圈例 i 1while i 10 print i i i 1 12 3456 ...

Python 學習筆記3(條件 迴圈)

1 條件判斷 這個和c語言有點類似,看下面 age 20if age 18 print your age is age print adult 你發現了什麼,if後面有個冒號,而且如果age小於18你猜結果會是怎樣,如果是c語言那麼肯定會輸出 adult 而py不會輸出 說明py的 執行和縮排有關 ...