-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.loop.py
63 lines (51 loc) · 1.48 KB
/
4.loop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#coding=utf-8
print "-------------------while loop-------------------"
count = 0
while (count < 9):
print 'The count is:', count
count += 1
print "Yahaha"
#continue与break
#continue用于跳过该次循环,break用于推出循环
# 该循环会无限输出双数
# i = 1
# while 1 < 10:
# i += 1
# if i%2 > 0: #除2无法除净 有余数时
# continue #跳过有余数的单数 只输出双数
# print i
j = 1
while 1: #就是while true
print j
j += 1
if j > 9:
break #当j>9跳出循环
print'End'
print "-------------------for loop-------------------"
for letter in 'Python':
print '当前字母:', letter
fruits = ['banana','apple','mango']
for fruit in fruits:
print '当前水果:',fruit
print 'End'
print "-----------------通过序列索引迭代----------------"
people = ['Tom','Anna','Wilson']
for index in range(len(people)):
print '当前人物:', people[index]
print 'End'
print "--------------------循环嵌套--------------------"
x = 2
while(x < 50):
y = 2
while(y <= (x/y)):
if not(x%y): break #没有余数时退出循环(not不为零的数为false)
y = y + 1
if (y > x/y): print x, " 是质数"
x = x + 1
print "End"
print not(0)
print "------------------continue语句------------------"
for letter in 'Python':
if letter == 'o':
continue #跳过当前循环剩余语句(删除满足循环调节下的某些不需要的部分)
print '当前字母:', letter