循環::
for循環是一個結構,導致程序要重複一定的次數。
list1=[1,2,3,4] for i in list1: print i---在序列裏,使用for循環遍歷。
range() 可以快速的生成一個序列
range(i,j,步進值)
如果所創建的對象爲整數,可以用range
列表重寫:
print [i*2 for i in range(1,11)] print [i for i in range(1,11) if i%2==0] print [i**2 for i in range(1,11)] for i in [i**2 for i in range(1,11)]: print i
for i in [i**2 for i in range(1,11) if i%2!=0]:
print i
xrange(對象)//返回的是一個對象,對對象進行遍歷。
a= xrange(10) for i in a: print i
a= xrange(10) for i in a: print i for i in xrange(100): print i
for 遍歷訪問字典:
dic1={'a':100,'b':100,'c':100,'d':100,'e':100} # dic1=dic.fromkeys('abcde',100) print dic1 for k in dic1: print k ,dic1[k] for k in dic1: print "%s --->%s" % (k,dic1[k]) for k in dic1: print "%s --->%s" % (k,dic1[k]), print dic1.items() for i in dic1.items(): print i for k,v in dic1.items(): print k,v for k ,v, in dic1.iteritems(): print k,v for i in dic1: print i ,dic1[i] for i in dic1.items(): print i for k,v in dic1.items(): print k,v
{'a': 100, 'c': 100, 'b': 100, 'e': 100, 'd': 100} a 100 c 100 b 100 e 100 d 100 a --->100 c --->100 b --->100 e --->100 d --->100 a --->100 c --->100 b --->100 e --->100 d --->100 [('a', 100), ('c', 100), ('b', 100), ('e', 100), ('d', 100)] ('a', 100) ('c', 100) ('b', 100) ('e', 100) ('d', 100) a 100 c 100 b 100 e 100 d 100 a 100 c 100 b 100 e 100 d 100 a 100 c 100 b 100 e 100 d 100 ('a', 100) ('c', 100) ('b', 100) ('e', 100) ('d', 100) a 100 c 100 b 100 e 100 d 100九九乘法表
for i in xrange(1,10): for j in xrange(1,i+1): print "%s*%s=%s" % (j,i,j*i), print
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
for
else
for 循環如果正常結束,纔會執行else 語句
break
continue
pass //佔位
exit()//退出
import time for i in xrange(10): print i time.sleep(1) else: print 'main end'
0 1 2 3 4 5 6 7 8 9 main end
for i in xrange(10): print i # time.sleep(1) if i == 5: break else: print 'main end'
0 1 2 3 4 5
for i in xrange(10): if i==3: continue elif i==5: continue elif i==6: pass elif i==7: # sys.exit() pass print i else: print 'main end' print "hahaha"
0 1 2 3 4 5
for i in xrange(10): if i==3: continue elif i==5: continue elif i==6: pass elif i==7: sys.exit() # pass print i else: print 'main end' print "hahaha"
0 1 2 4 6作業: 猜數字遊戲
系統生成一個20以內的隨機整數
玩家有6次機會進行猜猜看,每次猜測都有反饋(猜大了,猜小了,猜對了,結束)
六次中,猜對了,玩具贏
否則系統贏了。
隨機模塊:
random.randint(1,20)
Python 第三方軟件:
pip list 查看
for 與while 相比
for 循環用在有次數的循環上
while 循環用在有條件的控制上。
while
while 循環,知道表達式爲假,才退出
while循環,表達式是一個邏輯表達式,必須返回一個true或false。
語法:
while expresson:
statement(s)
while是條件循環是如此,當條件變爲假,循環結束。
n=0 while True: if n==10: break; print 'hello' n+=1
hello hello hello hello hello hello hello hello hello hello
while True: print 'hello' input=raw_input("please input something,q for quit:") if input=='q': break
hello please input something,q for quit:a hello please input something,q for quit:b hello please input something,q for quit:q
while input!='q': print 'hello' input=raw_input("please input something,q for quit:")abs()
hello please input something,q for quit:a hello please input something,q for quit:a hello please input something,q for quit:a hello please input something,q for quit:q
x='' while x!='q': print 'hello' x=raw_input("please input something,q for quit:") if x=='': break else: print 'hello world '
hello please input something,q for quit:q hello world
x='' while x!='q': print 'hello' x=raw_input("please input something,q for quit:") if x=='': break if x=='quit': continue print 'continue' else: print 'hello world '
hello please input something,q for quit:a continue hello please input something,q for quit:quit hello please input something,q for quit:q continue hello world