Python loops Flashcards
Types of python loops
- While loops
2. For loops
While loops repeatedly execute a statement or block of statements as long as the condition is _____
TRUE
When the condition is _____ the loop terminates and the line immediately after the loop in a program is executed
FALSE
Python uses indentation as its method of _______ statements
to indicate a block/grouping of code
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
block
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) initialize count - count =0
b) check condition - while (count<3)
c) update - count = count +1
d) sentinel - 3
count = 0
while (count < 3):
count = count + 1
print(“Hello Geek”)
What would be the output?
Hello Geek
Hello Geek
Hello Geek
The _____ clause is only executed when your while condition becomes false
else
____ loops are used for sequential traversal. For example: traversing a list or string or array etc.
For
In Python, the counter is not the letter __
C
The _____ function gives a sequence of numbers based on the start and stop index given
Range
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 =” “)
0
range(5)
What would the output be?
0,1,2,3,4
n = 4
for i in range(0, n):
print(i)
What is printed?
Output 0 1 2 3
_______ 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
unbounded
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
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
In python there is no end while.
How can you indicate that the loop is finish?
Stop indenting
Define sentinel
the value that allows you to end the loop
In the range there are two values, one to _____the loop and one to identify the ______of the loop PLUS ONE.
start
end
for x in range (2,6)
print(x)
Where does it starts and stops?
What is the output ?
start - 2
stops - 5
Output 2 3 4 5
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 ______
String
For i in (10)
print “LOVE”
Which number does this loop starts and stops?
How many times will LOVE be printed?
start - 0
stop -9
10 ten
for i in range (5):
print( i + 1)
What is the output?
1 2 3 4 5