原创转载请注明出处:http://agilestyle.iteye.com/blog/2327550
loop.py
databases = ['oracle', 'mysql', 'cassandra', 'mongodb', 'hbase']
for database in databases:
print(database)
total = 0
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
total = total + i
print(total)
# range(0, 5)
print(range(5))
# [0, 1, 2, 3, 4]
print(list(range(5)))
total = 0
for i in range(101):
total = total + i
print(total)
total = 0
n = 100
while n > 0:
total = total + n
n = n - 2
print(total)
# break
n = 1
while n <= 10:
if n > 3:
break
print(n)
n = n + 1
print('END')
# continue
n = 0
while n < 10:
n = n + 1
if n % 2 == 0:
continue
print(n)
Console Output

本文通过Python代码展示了不同类型的循环应用,包括for循环遍历数据库列表、计算总和及使用range函数。此外,还演示了while循环的基本用法,如累加计数、条件判断中的break与continue语句。

被折叠的 条评论
为什么被折叠?



