0% found this document useful (0 votes)
116 views6 pages

Python Loops: Questions & Answers Guide

The document provides a comprehensive overview of Python loops, including multiple-choice questions, fill-in-the-blanks, and true/false statements related to iterative statements and membership operators. It explains the use of the range() function, differentiates between for and while loops, and discusses the possibility of creating infinite loops with examples. Additionally, it includes application-based and competency-based questions to reinforce understanding of loop concepts in Python.

Uploaded by

thakurshivan062
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views6 pages

Python Loops: Questions & Answers Guide

The document provides a comprehensive overview of Python loops, including multiple-choice questions, fill-in-the-blanks, and true/false statements related to iterative statements and membership operators. It explains the use of the range() function, differentiates between for and while loops, and discusses the possibility of creating infinite loops with examples. Additionally, it includes application-based and competency-based questions to reinforce understanding of loop concepts in Python.

Uploaded by

thakurshivan062
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ch-6 Python Loops Question- Answers

A. Tick (✓) the correct option


1. b. terminated
o The else block executes when the loop terminates normally (without break)
2. b. range
o The range operator checks if a value lies within a sequence of values
3. b. for
o The for loop is used when you know how many times the loop will execute
4. a. range()
o The range() function produces a sequence from initial to final value
5. a. Looping
o An iterative statement is also known as a looping statement

B. Fill in the blanks


1. Loops are also known as iterative statements.
2. The in operator checks whether a given value is a part of the specified sequence.
3. The while loop can be applied to a program where the number of iterations is not
known beforehand.
4. A for or while loop can have an optional else block as well.
5. The range() function produces a list of a sequence.

C. Write T for true and F for false


1. F - An iterative statement is based on two values (true/false), not three values.
2. T - The range() function, by default, increments the value by 1.
3. F - There are three types of iterative statements: for, while, and do-while (though Python
doesn't have do-while, the statement refers to general programming).
4. T - The iterative statements keep repeating as long as the given condition is true (not
false).
5. T - The loop...else statement is optional in Python programs.

D. Answer the following


1. What do you mean by iterative statements? Give an example from your real life.
Iterative statements : is a control flow statement that allows code to be executed repeatedly
based on a given Boolean condition or for a specific number of times . Each execution of the
loop body is called an iteration.
Real-life example: Brushing your teeth - you repeat the brushing motion multiple times until
your teeth are clean. This is like a loop that continues until a condition (clean teeth) is met.
2. What are the membership operators in Python? Briefly explain their uses.
These operators are used to check whether a value is present (or not present) in a sequence
such as a string, list, tuple, or dictionary.
Python has two membership operators:
a) in Operator : The in operator returns True if the specified value is found in the given
sequence.
For eg:

b) not in Operator : The not in operator returns True if the specified value is not found in the
sequence.
For eg:

Uses: Membership operators are useful when you need to:


• Check if an item exists in a list before performing an action.
• Verify if a character exists in a string.
• Validate user input.
3. Explain the use of range() function.
The range() function in Python is used to generate a sequence of numbers. It’s most commonly
used with loops to repeat actions a certain number of times. The range() function includes the
start value but excludes the stop value

Parameter Meaning Default Value


start The number to begin from 0
stop The number to stop before —
step The difference between each number 1

Example:
• range(5) → 0, 1, 2, 3, 4
• range(2, 8) → 2, 3, 4, 5, 6, 7
• range(0, 10, 2) → 0, 2, 4, 6, 8
4. Differentiate between for and while loop.
Both for and while loops are used to repeat a set of statements multiple times.
However, they are used in different situations depending on how many times you need to
repeat the task.

For Loop While Loop


The for loop is used when you know The while loop is used when you don’t know in
beforehand how many times you want to advance how many times you need to repeat.
repeat a task. i.e It is used when iterations are i.e. It is used when iterations are unknown.
known. .
It usually works with the range() function or It keeps running as long as the condition is
any sequence (like a list or string). i.e. It True. i.e It runs until condition becomes false
iterates over a sequence. .

Syntax :
Syntax:
Output: Output:

5. Is it possible to create an infinite loop? If yes, write a code to show infinite loop?
Ans. An Infinite Loop (or endless loop) is a sequence of instruction in a computer program,
which loops endlessly. It happens either due to the loop having no terminating condition or the
condition that can never be met. Yes, it is possible to create an infinite loop.
Example code:
n=5
while n>=1:
print(n)
n=n+1

Note: Use Ctrl+C to stop an infinite loop.


E. Application based questions
1. Prateek's program to check divisibility by 7
Answer: Prateek should use the if-elif-else conditional statement with the modulo operator
(%).
Code:
number = int(input("Enter a number: "))

if number % 7 == 0:
print("Number is divisible by 7")
else:
print("Number is not divisible by 7")
2. Kirti wants to display all numbers from 1 to 100
Answer: Kirti should use a for loop with range() function.
Code:
for i in range(1, 101):
print(i)

F. Competency based questions


1. Step-by-step guide to explain components of a while loop
Components of a While Loop:
Step 1: Initialize the counter
• Set a variable before the loop starts
• Example: count = 1
Step 2: Write the while condition
• Define when the loop should continue
• Example: while count <= 5:
Step 3: Write the loop body
• Code to execute in each iteration
• Example: print(count)
Step 4: Update the counter
• Change the variable to eventually stop the loop
• Example: count += 1
Complete Example:
count = 1 # Initialize
while count <= 5: # Condition
print(count) # Loop body
count += 1 # Update
2. Using membership operators in for loop to display even numbers from 2 to 20
Process Explanation:
Step 1: Create a list of even numbers Step 2: Use for loop to iterate Step 3: Use membership
operator to check
Code:
# Method 1: Using range with step
for num in range(2, 21, 2):
print(num)

# Method 2: Using membership operator


even_numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
for num in range(2, 21):
if num in even_numbers:
print(num)

# Method 3: Using modulo with in operator


for num in range(2, 21):
if num % 2 == 0:
print(num)

You might also like