Python for while break continue

原创转载请注明出处: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


 

猜你喜欢

转载自agilestyle.iteye.com/blog/2327550