Module 0.3.0 - Loops & Iteration
0.3.1 - While Loops
i = 0
while i <= 5:
i += 1
print(i)
print("loop has ended")
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>> "loop has ended"i = 0
while i <= 5:
i += 1
print(i)
print("loop has ended")
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>> "loop has ended"while True:
print("This will not stop running")i = 0
while i <= 5:
i += 1
print(i)
if i == 3:
print("ending loop")
break
print("loop has ended")
>>> 1
>>> 2
>>> 3
>>> "ending loop"
>>> "loop has ended"i = 0
while i <= 10:
i += 1
print(i)
if i == 5:
print("halfway there")
continue
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>> "halfway there"
>>> 6
>>> 7
>>> 8
>>> 9
>>> 10
>>> 11 num_list = list(range(10))
print(num_list)
>>> [0, 1, 2, 3, 4, 5, 6 ,7, 8, 9]print(list(range(1, 16)))
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]even_nums_lessThan_10 = list(range(0,11,2))
print(even_nums_lessThan_10)
>>> [0, 2, 4, 6, 8, 10]for i in range(5):
print("hello")
print("loop has ended")
>>> "hello"
>>> "hello"
>>> "hello"
>>> "hello"
>>> "hello"
>>> "loop has ended"for i in range(5):
print("hello")
if i == 7:
break
print("loop has ended")
else:
print("loop ran sucessfully")
>>> "hello"
>>> "hello"
>>> "hello"
>>> "hello"
>>> "hello"
>>> "loop has ended"
>>> "loop ran sucessfully"