Questions & Answers
1.
What is the difference between a for loop and
a while loop?
A for loop is generally used when we want
to iterate over a sequence or repeat a block of code
for a known or countable number of iterations. A
while loop repeats a block of code as long
as a specified condition remains True.
for Loop |
while Loop |
|---|---|
| Iterates over a sequence or iterable. | Repeats while a condition is true. |
| Often useful when the number of iterations is known. | Useful when repetition depends on a condition. |
for i in range(5):
print(i)
count = 0
while count < 5:
print(count)
count += 1
2. What do you mean by membership operators?
Membership operators are used to
check whether a value is present in a sequence or
collection. Python provides two membership operators:
in and not in.
inreturnsTrueif the value is present.not inreturnsTrueif the value is absent.
fruits = ["apple", "banana", "mango"]
print("banana" in fruits)
print("orange" not in fruits)
True
True
3. Why should loops be used instead of writing repetitive code manually?
Loops should be used because they allow programmers to repeat instructions efficiently without writing the same code multiple times. They make programs shorter, easier to understand, easier to maintain and less prone to errors.
print() statement would have to be written
repeatedly.
With a loop:
for i in range(5):
print("Hello")
The loop executes the same instruction five times while keeping the program compact.
4.
What are the three arguments that can be passed in the
range() function?
The three arguments of Python's range()
function are:
- start – the value at which the sequence begins.
- stop – the value at which the sequence ends; this value is not included.
- step – the amount by which the sequence changes after each iteration.
range(start, stop, step)
for i in range(2, 10, 2):
print(i)
Output:
2, 4, 6, 8
5. What is the role of the loop control variable?
A loop control variable stores the
current value associated with an iteration of a loop.
In a for loop, it usually takes successive
values from the iterable or sequence.
for number in range(1, 4):
print(number)
Here, number is the loop control variable.
It takes the values 1, 2 and
3 during successive iterations.
Quick Revision
| Concept | Key Point |
|---|---|
| for Loop | Iterates over an iterable such as a sequence, list or range. |
| while Loop | Repeats statements while a condition is true. |
| Membership Operators | in and not in check membership in a collection or sequence. |
| range() | Can accept start, stop and step arguments. |
| Loop Control Variable | Represents the current value being processed during an iteration. |
| Loops | Reduce repetitive code and make programs more efficient and maintainable. |
for vs while – At a Glance
| Feature | for Loop |
while Loop |
|---|---|---|
| Primary basis | Iterates over an iterable. | Depends on a Boolean condition. |
| Common use | Processing items or repeating a known number of times. | Repeating until a condition changes. |
| Typical example | for i in range(10): |
while count < 10: |
| Important consideration | The iterable determines what values are processed. | The condition must eventually become false when termination is intended. |
Understanding range()
The range() function generates a sequence of
integers that can be used for iteration.
| Form | Meaning |
|---|---|
range(stop) |
Starts from 0 and goes up to, but does not include, stop. |
range(start, stop) |
Starts at start and stops before stop. |
range(start, stop, step) |
Starts at start and changes by step until reaching the stop boundary. |
Key Terms
Iteration: One execution of the loop body.
Iterable: An object whose elements can be accessed one at a time during iteration, such as a list, string or range.
Membership: The state of whether a particular value occurs in a collection or sequence.
Loop Control Variable: A variable that represents the current value being processed during a loop iteration.