Table of contents
Loop Statement in Python
- Keyword: for, while
- for need to set number range
- while need to set when stop the repeatation
- use for: when you know how many repeat needed
- use while: when you do not know how many repeat needed
for or while [range condition] :
[space]code you want to repeat
for statement Example
for i in range(10):
print("Hi") # Hi printed 10 times
temp=[A,B,C]
for i in range(len(temp)):
print(i) # 0 1 2 index printed
temp=[A,B,C]
for i in range(temp):
print(i) # A B C values in List printed
for i, j in enumerate(a):
print(i, ":", j) # 0 : A 1 : B 2 : C index and value
numbers = [9, 7, 7]
letters = ["z", "x", "y"]
for pair in zip(numbers, letters):
print(pair)
# (9, 'z') (7, 'x') (7, 'y') index and value Tuple
for i in range(5, 10): # 5 ~ 9
print(i) # 5 6 7 8 9
a = "balloon"
for i in a:
print(i) # b a l l o loop until i is "o"
if i == "o":
break
while Statement
i = 0
while i < 5:
print(i) # 0 1 2 3 4 loop until i is bigger than 5
i = i + 1