# -*- coding:utf-8 -*-
#while 條件:
# **塊11
#else:
# **塊2
i=1while i<=5:
print '第'+str(i)+'次輸出'
i+=1 #語法沒有i++
else:
print '完畢'
#for one in object:
# **塊1
# if 條件1:
# break
# if 條件2:
# continue
# else:
# pass
nums=[1,2,3,4,5]
for num in nums:
print num
###所謂字典:由「鍵-值」組成的集合
#dic =
for key in dic:
print key,'----',dic[key]
#for迴圈遍歷元祖:
tuple=[(1,2),(3,4)]
for a,b in tuple:
print a,b
#||for (a,b) in tuple:
print a,b
for a in tuple:
print a
#迭代器
#任何類似的物件都被認為是可迭代的。迭代工具的內部工作都是在迴圈呼叫next方法,
#並且捕捉stopiteration異常來確定何時離開。
for readline in open('test.py'):
print readline
#iter()內建函式
many = [1,2,3,4,5]
one = iter(many)
print one.next();
print one.next();
print one.next();
print one.next();
###直接for迴圈
#for one in iter(many):
print one
#九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print (" ".join(["%d*%d=%d" %(j,i,i*j)]))
#並行迭代
names=['a','b']
ages=[1,2]
for i in range(len(names)):
print names[i],'----',ages[i]
#zip迭代是把兩個序列「壓縮」在一起,然後返回乙個元祖的列表
for name,age in zip(names,ages):
print name,'-------',age
#編號迭代
for index,name in enumerate(names):
if 'a' in names:
names[index]='aaa'
print index,name
for name in names:
print name
Python入門之for迴圈迭代
迭代 iteration 給定乙個list或tuple或.通過for 迴圈來遍歷這個list或tuple或.這種遍歷稱為迭代。1.dict 迭代key d for key in d print key a cb迭代value for value in d.values 同時迭代key和value f...
python 迴圈(迭代)
for 和 while for用來迭代處理,什麼叫迭代?你就當沒看到這個詞,for是把一堆玩意做乙個乙個加工用的,比如吃一袋花生,得乙個乙個剝吧,就是這意思 a abcde 每個字母當做乙個花生,a是一袋子 for i in a 從袋子裡面乙個乙個拿花生,拿出的每乙個花生都起名叫小i君 i i ma...
python基礎之迴圈與迭代器
迴圈 python 迴圈語句有for迴圈和while迴圈。while迴圈 while迴圈語法while 判斷條件 語句 while迴圈示例 i 0 while i 10 i 1 print i while else 語句 語法while 判斷條件 語句else 語句 while else 示例 n ...