基礎篇-python迴圈語句for,while習題練習
阿新 • • 發佈:2018-11-25
今天習題:
習題一:
1 用while語句的2種方法輸出數字:1到10
(1)
x = 1
while x <= 10:
print(x)
x += 1
(2)
word = [1,2,3,4,5,6,7,8,9,10]
for x in word:
print(x)
(3)
for y in range(1,10):
print(y,end=' ')
2 用for語句和continue 輸出結果:1 3 5 7 9
for x in range(1,10):
if x % 2 == 0:
continue
print(x,end=' ')
習題二:假設有列表
a = [1,2,3,4,5,6]
1 用for if else 的方法查詢數字8是否在列表a裡,如果在的話,輸出字串'find',如果不存在的話,
輸出字串'not find'
a = [1,2,3,4,5,6]
for x in a:
if x == 8:
print ("find")
break
else: print ("not find")
2 用while語句操作上面的列表a,輸出下面結果:
[2,3,4,5,6,7]
a = [1,2,3,4,5,6]
b = []
while a:
b.append(a.pop(0)+1)
print (b)