loops Flashcards

1
Q

what is a loop in python

A

In programming, this process of using an initialization, repetitions, and an ending condition is called a loop. In a loop, we perform a process of iteration (repeating tasks).

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

what is Definite iteration

A

Where the number of times the loop will be executed is defined in advance (usually based on the collection size).

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

What is Indefinite iteration

A

where the number of times the loop is executed depends on how many times a condition is met.

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

what is a for loop

A

a type of definite iteration. (When a program needs to iterate a set number of times)

In a for loop, we will know in advance how many times the loop will need to iterate because we will be working on a collection with a predefined length

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

what is a tempory variable

A

that is used to represent the value of the element in the collection the loop is currently on.

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

how to layout a for loop

A

for <temporary> in <collection>:</collection></temporary>

<action>

ingredients = ["milk", "sugar", "vanilla extract", "dough", "chocolate"]

for ingredient in ingredients:
print(ingredient)

In this example:

ingredient is the <temporary>.

ingredients is our <collection>.

print(ingredient) was the <action> performed on every iteration using the temporary variable of ingredient.
</action></collection></temporary></action>

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

what is the ( in ) used for in a ( for ) loop

A

An in keyword separates the temporary variable from the collection used for iteration.

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

what is <collection> in a for loop</collection>

A

A <collection> to loop over. In our examples, we will be using a list</collection>

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

does a tempory variable need to be defined before hand ?

A

A temporary variable’s name is arbitrary and does not need to be defined beforehand.

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

do we need to indent a for loop ?

A

If we ever forget to indent, we’ll get an IndentationError or unexpected behavior.

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

example for loop

A

sport_games = [“football”, “hockey”, “baseball”, “cricket”]

For sports in sport_games:
print(sports)

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

how to use the range directly in our for loops as the collection to perform iteration ?

A

for temp in range(6):
print(“Learning Loops!”)

Would output:

Learning Loops!
Learning Loops!
Learning Loops!
Learning Loops!
Learning Loops!
Learning Loops!

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

what is the structure of a while loop

A

The structure follows this pattern:

while <conditional>:</conditional>

<action>

Let’s examine this example, where we print the integers 0 through 3:

count = 0
while count <= 3:
# Loop Body
print(count)
count += 1

output: 0, 1, 2, 3
</action>

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

what does a ( while ) loop do

A

a while loop and is a form of indefinite iteration ( where the number of times the loop is executed depends on how many times a condition is met. )

A while loop performs a set of instructions as long as a given condition is true.

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

what does the str ( ) do

A

converts the variable from an integer to a string

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

what is the purpose of the len ( ) in a loop list

A

If we use the length of the list as the basis for how long our while loop needs to run, we can iterate the exact length of the list.

We can use the built-in Python len() function to accomplish this:

Length would be equal to 5
length = len(ingredients)

15
Q

can you use while loops to iterate through a list

A

yes

16
Q

how do you construct 2 variables to make a for loop

A

length = len(ingredients)
index = 0

while index < length:
print(ingredients[index])
index += 1

17
Q

what does the ( break ) do in a loop

A

Once item is found in a list the break command will stop the search

if item == “knit dress”:
break

18
Q

what does the continue loop control do

A

if we want to print out all of the numbers in a list, but only if they are positive integers. We can use another common loop control statement called continue.

18
Q

what is a nested loop

A

if we wanted to print each item in the list we would actually need to nest our loops to be able to loop through each sub-list.

Loop through each sublist

for team in project_teams:

# Loop elements in each sublist

for student in team:
print(student)

This would output:

Ava
Samantha
James
Lucille
Zed
Edgar
Gabriel

19
Q

how do we nest loops in pyhton example code

A

for groupes in group:
for name in group:
print(name)

19
Q
A
19
Q
A
20
Q
A