Week 6: Simple Loops Flashcards

1
Q

How to search through a specific range of index numbers?

A

for x in range(1,11)
print(x)

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

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

How to search for a specific variable in a list?

A

for num in nums:
if nums == 3:
print(“found 3!”)
break
print(num)

1, 2, found 3!

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

What is a Nested Loop?

A

When a loop is inside another loop.

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

What type of loop is this and what might the output be:
for num in nums:
for letter in ‘abc’:
print(num, letter)

A

Nested.
1 a
1 b
1 c
2 a
2 b
2 c

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

How does an infinite loop happen?

A

When a while condition becomes False, there is no stoppage of a code.

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

What structure are Loops?

A

Repetition

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

How can you stop loops?

A

break

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

Define Iteration.

A

Path through a lap (#)

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

Define Loop.

A

Blocks of code that repeat. Can be while, for, or until statements.

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

Define Termination Condition.

A

What condition ends the loop.

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

Define Block.

A

Code that is the loop. Contains code that repeats and the code that ends the loop.

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

What is the body of the code?

A

Code that needs to repeat.

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

What are the 2 types of loop codes and how do they differ?

A

Pre-Test and Post-Test. In Pre, the condition terminating the code is at the beginning and Python only accepts this type.
Post has the condition terminating the loop at the end and always runs at least once.

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

What would an example of each look like?
a. Pre-Test
b. Post-Test

A

a.
condition code
—-repeating code
—-termination code
b.
—-repeating code
—-termination code
condition code

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

Give an example of a Test condition.

A

Until answer_by_user == “no”
or
While user_continue != “Q”

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

What is a Counter/Do Loop?

A

Easiest to write as its not up to the user, rather we configure how many times this code needs to loop (executes a set # of times).

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

In Do Loops, does the counter need to be declared? And if so, how might you do this?

A

Yes. Will need to be incrememented (Example: counter += counter).
counter = 0

18
Q

What does the syntax of a Do Loop look like?

A

counter = int
counter = 1
while counter <= 5:
—-print(counter)
—-counter =+ 1
end loop

19
Q

Tips to debug a loop?

A
  1. If it is an infinite, check to what can make it False.
  2. Check if loops are missing a line that implements the counter (counter = counter + 1)
  3. End prompt (exit strategy) is missing
  4. Syntax error / capitalization errors
20
Q

What does “…step 1” tell the computer to do?

A

To increment the counter by 1 with each pass or iteration through the loop.

21
Q

Which is it–While/Until or For Loop?
a. Runs a predetermined amount of times?
b. Counting variable is automatically declared?
c. Counting variable must be incremented?

A

a. For Loop
b. For Loop
c. While/Until

22
Q

What is a Nested Loop? Give an example.

A

Loop within a Loop.
for noun in nouns:
—-for adj in adjs:
——–print(noun, adj)

23
Q

How many times will Hello print and why:
Counter = 0
while Counter < 3:
—-print(“Hello”)
—-counter = counter + 2

A

Hello
Hello
1st iteration: because 0 < 3 is True. Prints once.
2nd: because now counter = 2 which is still < 3, True. Prints again.
3rd: Counter gets changes from 2 to 4 and is now False, as it is not < 3.

24
Q

How many times will this print and why:
for i in range(0,5,1):
—-print(“Hello”)

A

Hello
Hello
Hello
Hello
Hello
because of the 2nd/middle #

25
What does the 1st number tell you here: for i in range (0,5,1)
Where to start, so 0 1 2 3 4
26
What does the 3rd number tell you here: for i in range (0,5,2)
What step (step of 2). Like, how many to skip. Example: Hello -skip Hello -skip Hello
27
Show which to skip in: for i in range(0,10,3)
Hello -skip -skip -skip Hello -skip -skip -skip
28
What does "for i in range" mean?
Means the loop will iterate over each # sequence
29
How many times will the following run and why: range(1,10)
9 because it starts at 1 and goes to 9. Not starting at 0.
30
What is the following: end=
The end parameter in Python's print() function specifies what to print at the end of the output. By default, print() ends with a newline character (\n), which means each print() call outputs on a new line. However, you can change this behavior by setting the end parameter to a different string.
31
What will this output: print("Hello", end=" ") print("World")
Hello World
32
If a Nested Loop where the inner loop executes 10 times, and the outer 8, in total the inner loop will execute __ times.
80
33
What will print: for row in range(1, 6): # Outer loop for rows (1 to 5) for column in range(row): # Inner loop for columns (number of times to print the row number) print(row, end="") print("") # Add new line character after each row
1 22 333 4444 55555
34
What is the (loop) syntax to find the exponent of a #? What is another way?
1. exp = 4 base = 2 result = 1 for x in range(exp): ----result = base * result print(result) 2. result = 2 ** 3 print(result)
35
What does `inp = input('> ')` mean?
`input` command will wait for the user to type some information into the terminal and press `return`. `input` takes an string argument which will be displayed for the user. The information entered by the user is stored in the variable `inp`. All information entered for the `input` command will be stored as a string (even if you type a #).
36
What is an accumulator?
Variables that are integers. They get larger depending on the # the user enters.
37
How do you know if you'll need a counter in your loop code?
When you know how many times you need the code to run, or until a certain condition is met, when you need to accumulate a value over the iterations like summing numbers.
38
How to know if a # is positive or negative?
while answer != "q": ----number = int(input("Enter a number:")) ----if number<0: --------sum_neg = sum_neg + number ----else --------sum_pos = sum_pos + number ----answer = input("Enter q to quit") print("Total Neg Numbers:", sum_neg) print("Total Pos Numbers:", sum_pos)
39
Provide an example of a counter loop with a Do...Loop.
use a while loop. Example: counter = 0 max_count = 5 while True: print("Counter:", counter) counter += 1 if counter >= max_count: break
40
What does the random library do?
Generate random numbers and perform random operations.
41
What is the syntax of the radint function?
random.randint(a, b)