Python Loops Flashcards
m=0 while m<4: print("begin=", m, end=", ") m+=2 print("end= ",m, end= ", ")
begin= 0, end= 2, begin= 2, end= 4,
for i in [“hello”,”hi”]:
print(“python”,end= “*”)
pythonpython
becus it counts the items in the python array
for num in range(-2,-8,-2):
print(num,end= “, “)
-2,-4,-6,
Explanation: the starting point is -2 and the step size is -2.
The num variable does not reach -8 aas it is not included in the range.
when do will the program enter while else
When the boolean for the while loop becomes false
i=7 while i <9: print(i, end = ", ") i+= 1 else : print(0)
7,8,0
no comma at the back
What is break statement for ?
To immediate exit the execution of the current loop. works on both while and for loop
value = 1
print(“before”, value , end =”,”)
while value <= 3: value+=1 if value ==2: break print("while",value,end =", ") else: print("else", value, end=", ") print("after", value , end =", ")
before 1, after 2,
Whats does continue statement do ?
exits the current loop iteration and returns to the start of the loop
value = 1
print(“before”, value , end =”,”)
while value <= 3: value+=1 if value ==2: continue print("while",value,end =", ") else: print("else", value, end=", ") print("after", value , end =", ")
before 1,while 3, while 4, else 4, after 4,
value was 3, hence “while 4” was produced
for num in range (4): if num == 4: break else: print(num, end ="")
else:
print(“else”)
result : 0123else
num does not reach 4, hence the for loop wont break because the range(4) starts from 0 to 3
range(5,11,3)
what does this range function do
starts from 5, ends at 10 with step size of 3
range(11)
what does this function do
starts from 0 to 10