Python loops Flashcards

1
Q

Types of python loops

A
  1. While loops

2. For loops

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

While loops repeatedly execute a statement or block of statements as long as the condition is _____

A

TRUE

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

When the condition is _____ the loop terminates and the line immediately after the loop in a program is executed

A

FALSE

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

Python uses indentation as its method of _______ statements

A

to indicate a block/grouping of code

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

All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single _____ of code

A

block

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

count = 0
while (count < 3):
count = count + 1

Identify the

a) initializing of the counter variable
b) checking of condition
c) update
d) sentinel

A

a) initialize count - count =0
b) check condition - while (count<3)
c) update - count = count +1
d) sentinel - 3

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

count = 0
while (count < 3):
count = count + 1
print(“Hello Geek”)

What would be the output?

A

Hello Geek
Hello Geek
Hello Geek

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

The _____ clause is only executed when your while condition becomes false

A

else

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

____ loops are used for sequential traversal. For example: traversing a list or string or array etc.

A

For

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

In Python, the counter is not the letter __

A

C

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

The _____ function gives a sequence of numbers based on the start and stop index given

A

Range

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

In case the start index is not given, the index is considered as __, and it will increment the value by 1 till the stop index.
(range function)

Eg. for i in range(10):
print(i, end =” “)

A

0

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

range(5)

What would the output be?

A

0,1,2,3,4

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

n = 4
for i in range(0, n):
print(i)

What is printed?

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

_______ iterations are for when you DON’T know how many times to do the action. They require you to initialize, check and update on your own

A

unbounded

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
d = 12
while d>9
       print (d,"...getting smaller")
       d = d-1
print ("Dat number is too small now.")

What is the output?

Where is the variable initialized?

What is the counter?

Where is the counter being updating?

Identify the sentinel

A

12…getting smaller
11…getting smaller
10…getting smaller
Dat number is too small now.

variable initialized - d = 12

counter - d

counter updating - d = d-1

sentinel - 9

17
Q

In python there is no end while.

How can you indicate that the loop is finish?

A

Stop indenting

18
Q

Define sentinel

A

the value that allows you to end the loop

19
Q

In the range there are two values, one to _____the loop and one to identify the ______of the loop PLUS ONE.

A

start

end

20
Q

for x in range (2,6)
print(x)

Where does it starts and stops?

What is the output ?

A

start - 2
stops - 5

Output 
2
3
4
5
21
Q

for letter in “STAR”:
print (“Give me a: ,letter, “!”)

OUTPUT
Give me a: S!
Give me a: T!
Give me a: A!
Give me a: R!

this shows for loops through the letters in a ______

A

String

22
Q

For i in (10)
print “LOVE”

Which number does this loop starts and stops?
How many times will LOVE be printed?

A

start - 0
stop -9

10 ten

23
Q

for i in range (5):
print( i + 1)

What is the output?

A
1
2
3
4
5