Module 7 - For Loop Flashcards

1
Q

What’s a For Loop?

A

Runs for a specified number of times, designed to work with a sequence of data items (iterates once per item)

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

What is the syntax for a For Loop?

A

for variable in [val1, val2, val3,…}:

____statements

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

What is a target variable?

A

The variable which is the target of the assignment at the beginning of each iteration
Its purpose is to reference each item in a sequence as a loop iterates

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

What is the “in” operator in a for loop?

A

It allows the for loop variable to take on one value at a time during each iteration automatically so you won’t need to count or advance the loop variable

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

T or F: The control variables can only iterate on numerical lists.

A

False. It can iterate on any list - integer, float, string

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

What does the range function do?

A

Returns sequence values

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

How many arguments can a range function have?

A

1, 2, or 3: end; start and end; start, end, and step

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

What does range(5) return?

A

[0,1,2,3,4]
Default start value is 0, default increment is 1
NEVER include the end value (5) in the sequence!! Stop at the value before it

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

What does range(1,5) return?

A

[1,2,3,4]

The start value (1) is now specified

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

What does range(1,9,2) return?

A

[1,3,5,7]

The start value (1) and increment (2) are specified

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

How do you generate a sequence of numbers in descending order? e.g., 10 to 1?

A

you can use range function and make step negative:

range (10, 0, -1)

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

What’s a nested loop? What’s an real-world example of a nested loop?

A

Loop contained in another loop

Ex: Analog clock- hours and minutes hands

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

T or F: Outer loop goes through all of its iterations for each iteration of inner loop

A

False: Inner loop goes through all of its iterations for each iteration of outer loop
Inner loops complete their iterations faster than outer loops

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

How to calculate total iterations in a nested loop?

A

Total number of iterations in nested loop: number_iterations_inner x number_iterations_outer

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

When should you use a for loop? When should you use a while loop?

A

For Loop- when you need to perform the same action on every item in a list or character in a string
While Loop- when you need to perform a repeated operation but you don’t know ahead of time how many iterations will be needed

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

For loop vs. while loop

A

For loop runs a finite, pre-determined number of times based on the list
While loop will continue to run as long as condition is met