Python Loops Flashcards

1
Q
m=0
while m<4:
    print("begin=", m, end=", ")
    m+=2
    print("end= ",m, end= ", ")
A

begin= 0, end= 2, begin= 2, end= 4,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

for i in [“hello”,”hi”]:

print(“python”,end= “*”)

A

pythonpython

becus it counts the items in the python array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

for num in range(-2,-8,-2):

print(num,end= “, “)

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

when do will the program enter while else

A

When the boolean for the while loop becomes false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
i=7
while i <9:
     print(i, end = ", ")
     i+= 1
else :
     print(0)
A

7,8,0

no comma at the back

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is break statement for ?

A

To immediate exit the execution of the current loop. works on both while and for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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 =", ")
A

before 1, after 2,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Whats does continue statement do ?

A

exits the current loop iteration and returns to the start of the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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 =", ")
A

before 1,while 3, while 4, else 4, after 4,

value was 3, hence “while 4” was produced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
for num in range (4):
    if num == 4:
        break
    else:
        print(num, end ="")

else:
print(“else”)

A

result : 0123else

num does not reach 4, hence the for loop wont break because the range(4) starts from 0 to 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

range(5,11,3)

what does this range function do

A

starts from 5, ends at 10 with step size of 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

range(11)

what does this function do

A

starts from 0 to 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly