-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor_while.py
108 lines (91 loc) · 1.88 KB
/
for_while.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2017-10-28 21:28:42
# @copyright by hoojo@2018
# @changelog Added python3 `loop -> for while` example
'''
遍历循环
for i in list:
print(i)
for i in list:
print(i)
else:
print('xxx')
while i > 0:
i += 1
print(i)
'''
names = [ 'jack', 'tom', 'alex', 'charry' ]
# for 循环
for name in names:
print('element: ', name)
print()
# 通过下标进行迭代循环
for i in range(len(names)):
print('index: %s, val: %s' % (i, names[i]))
print()
# range 序列函数,range可以生成一个指定区间的list集合
ints = range(5)
for i in ints:
print('i: ', i)
print()
# for...else...
for w in 'abcdef':
print(w)
else:
print('循环结束时执行')
print()
# 正常结束会被执行,break中断的不被执行
for x in range(20):
if x % 3 == 0:
print('能被3整除', x)
else:
break
else:
print('正常结束会被执行,break不被执行')
print()
users = [ 'json', 'kili', 'M', 'jack' ]
for user in users:
for name in names:
if user == name:
print('找到了', user)
break
else:
print('没有找到')
print()
# while 循环
x = 10
while x > 0:
print('x: ', x)
x = x - 1
print(x)
print()
# while...else
x = 100
sum = 0
while x > 0:
sum = sum + x
x = x - 1
else:
print(sum)
print()
# continue continue后的语句不被执行,正常结束else被执行
for i in range(1, 20):
if i % 17 == 0:
print('%s %% 7 == 0' % i)
continue
print('i ->', i)
else:
print('else被执行')
print()
# 站位,不做任何事情
i = 5
while i > 0:
i -= 1
if i == 2:
pass
print('pass code line')
print('i', i)