6.4 For Loop Flashcards

1
Q

What does the “For loop” do?

A

It executes a block of code repeatedly until the condition in the for statement is no longer valid.

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

What is an iterable?

A

It refers to anything that can be looped over, such as a string, list, tuple or dictionary.

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

What is the syntax for “For loop” usage in iterable?

A

for a in iterable:

print(a)

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

Write the script showing the “for loop” usage

A

flowers=[“Roses”,”lilies”,”daisies”,”Mandevila”]
for myFavourites in flowers:
print(myFavourites)

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

How can you display the index of the items in the list?

A

using the enumerate() function

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

What is the syntax of the enumerate () function?

A

for index, myFavourites in enumerate(flowers):

print(index,myFavourites)

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

Write the script for printing index numbers with for loop

A

for index, myFavourites in enumerate(flowers):

print(index,myFavourites)

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

Write the script for using the “for loop” to run it through the dictionary.

A

userAge={“Peter”:5,”John”:7}
for i in userAge:
print(i)

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

Write the script for using the “For loop” to get both the dictionary key and the data

A

userAge={“Peter”:5,”John”:7}
for i in userAge:
print(“Name=%s,Age=%d”%(i,age[i]))

first time the loop runs, it assigns the value Peter to i and age [i}] refers to the value assigned to Peter i.e. 5

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

What is an items method?

A

It is a built-in method. It returns each of the dictionary’s key-data pairas a tuple.

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

Write a script using “for loop” and items() method.

A

age={“Peter”:5,”John”:7}
for i,j in age.items():
print(“Name=%s,Age=%d”%(i,j))

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

Write the script for using the ‘for loop’ through a string

A

message = ‘Hello’
for i in message:
print(i)

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

How can you loop through a sequence of numbers?

A

by using built-in range() function

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

What is the syntax for the range() function and some important points to remember about it?

A

range(start,end,step)

Important points:

1) Start is always zero unless specified
2) End must be provided but isn’t included in the result
3) If step isn’t given, a list of consecutive numbers will be generated

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

What will range(5) yield?

A

0,1,2,3,4

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

What will range (3,10) yield?

A

[3,4,5,6,7,8,9]

17
Q

What will range (4,10,2) yield?

A

[4,6,8]

18
Q

Write the script using For loop + range() function.

A

for i in range(5):

print(i)