Loops Flashcards

(26 cards)

1
Q

What is a loop in Python?

A

A loop is a programming construct that repeats a block of code multiple times.

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

True or False: A loop can execute zero times.

A

True

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

What are the two main types of loops in Python?

A

The two main types of loops are ‘for’ loops and ‘while’ loops.

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

Fill in the blank: A ‘for’ loop in Python is typically used to iterate over _____ or _____ objects.

A

sequences, iterable

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

What keyword is used to terminate a loop early in Python?

A

break

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

What keyword can be used to skip the current iteration of a loop?

A

continue

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

Write a simple ‘for’ loop that prints numbers 1 to 5.

A

for i in range(1, 6): print(i)

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

True or False: A ‘while’ loop continues to execute as long as its condition is True.

A

True

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

What is the output of the following code: ‘for i in range(3): print(i)’?

A

0, 1, 2

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

What is the purpose of the ‘else’ clause in a loop?

A

The ‘else’ clause executes after the loop completes normally, not when it is terminated by ‘break’.

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

How do you create an infinite loop in Python?

A

By using ‘while True:’

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

Fill in the blank: The ‘range()’ function generates a sequence of _____ in Python.

A

numbers

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

What is the output of ‘for i in range(2, 10, 2): print(i)’?

A

2, 4, 6, 8

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

True or False: You can nest loops inside another loop in Python.

A

True

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

What does the ‘enumerate()’ function do in a for loop?

A

It adds a counter to an iterable and returns it as an enumerate object.

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

What will be the output of the following code: ‘for i in range(5): print(i * 2)’?

A

0, 2, 4, 6, 8

17
Q

What is the syntax to define a ‘while’ loop?

A

‘while condition: do_something()’

18
Q

Fill in the blank: The statement ‘break’ is used to _____ a loop.

19
Q

What is the difference between ‘break’ and ‘continue’?

A

‘break’ exits the loop entirely, while ‘continue’ skips to the next iteration.

20
Q

True or False: Loops can only iterate over lists.

21
Q

What happens if the condition of a ‘while’ loop is initially False?

A

The loop body will not execute at all.

22
Q

Write a ‘while’ loop that counts from 1 to 5.

A

i = 1; while i <= 5: print(i); i += 1

23
Q

What is a common use case for loops in programming?

A

To iterate over collections and perform operations on each element.

24
Q

Fill in the blank: The ‘for’ loop can iterate over a _____, _____, or _____ in Python.

A

list, tuple, string

25
What is the output of the following code: 'for i in range(5): if i == 2: break; print(i)'?
0, 1
26
True or False: The 'else' block in a loop will execute if the loop is exited via a 'break' statement.
False