Grade VIII • Python Programming

Short Answer Questions

Loops • Iterative Statements • The for Loop • The while Loop

Questions & Answers

1. What is the difference between a for loop and a while loop?

Answer:

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?

Answer:

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.

  • in returns True if the value is present.
  • not in returns True if the value is absent.
fruits = ["apple", "banana", "mango"]

print("banana" in fruits)
print("orange" not in fruits)
Output:
True
True

3. Why should loops be used instead of writing repetitive code manually?

Answer:

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.

Without a loop: The same 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?

Answer:

The three arguments of Python's range() function are:

  1. start – the value at which the sequence begins.
  2. stop – the value at which the sequence ends; this value is not included.
  3. step – the amount by which the sequence changes after each iteration.
range(start, stop, step)
Example:
for i in range(2, 10, 2):
    print(i)
Output: 2, 4, 6, 8

5. What is the role of the loop control variable?

Answer:

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.

In simple words: The loop control variable helps the program keep track of the current item or value being processed in each iteration.

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.