0% found this document useful (0 votes)
24 views3 pages

Ch6 Loops Coding in Python

The document provides an overview of loops in Python, including the use of for and while loops, the range() function, and membership operators. It includes multiple-choice questions, fill-in-the-blanks, true/false statements, and practical coding examples to illustrate the concepts. Additionally, it explains iterative statements and their applications in programming.

Uploaded by

mayank4732011
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)
24 views3 pages

Ch6 Loops Coding in Python

The document provides an overview of loops in Python, including the use of for and while loops, the range() function, and membership operators. It includes multiple-choice questions, fill-in-the-blanks, true/false statements, and practical coding examples to illustrate the concepts. Additionally, it explains iterative statements and their applications in programming.

Uploaded by

mayank4732011
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
You are on page 1/ 3

Class 8.

Loops Coding in Python


Computer

A. Tick (✓) the correct option

1. The else block makes sure that the loop is terminated.


✓ b. terminated
2. The in operator checks whether a given value lies within a given set of values.
✓ c. in
3. The for loop is used when you are sure about how many times a loop body will be
executed.
✓ b. for
4. The range() function produces a list of a sequence starting with an initial value up to
a final value.
✓ a. range()
5. An iterative statement is also known as a Looping statement.
✓ a. Looping

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 for loop is used when the number of repetitions is 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. An iterative statement is based on three values.


T
2. The in operator checks whether a given value is a part of the specified sequence.
T
3. while loop can be applied to a program where the number of iterations is not known.
T
4. The range() function, by default, the value is incremented by 1.
T
5. There are two types of iterative statements: if and else.
F (Correct: for and while)
6. The iterative statements keep on repeating a set of statements as long as the given
condition is false.
F (Correct: condition is true)
7. The loop...else statement is optional to be used in a program.
T
D. Answer the following

1. What do you mean by iterative statements? Give an example from your real
life.
Iterative statements are those that keep on repeating themselves as long as a condition
is true. These are also called loops.
Example from real life: A girl running 5 rounds on a playground. After each round, a
variable X increases until it reaches 5.

2. What are the membership operators in Python? Briefly explain their uses.
Python has two membership operators:

• in: Returns True if a value exists in the sequence.


• not in: Returns True if a value does not exist in the sequence.
They are used to check if a value is present or not in a list, range, or string.

3. Explain the use of range() function.

Answer:
The range() function is used to create a sequence of numbers.
It takes:

• start: beginning value


• stop: end value (excluded)
• step: increment or decrement (default is 1)
It is helpful in loops for generating sequences of numbers.

4. Differentiate between for and while loop.

for loop while loop


Used when the number of repetitions Used when the number of repetitions
is known beforehand. is unknown.
Syntax: for i in range() Syntax: while condition:
Called a definite loop. Called a pre-tested loop.

5. Is it possible to create an infinite loop? If yes, write a code to show infinite


loop.
Yes, an infinite loop occurs when the condition never becomes False

n=5

while n >= 1:

print(n)

n=n+1
Application Based Questions

1. Prateek was writing a program to check whether a given number is


divisible by 7 or not. What conditional statement should he use?
He should use a conditional statement like if...else to check if the number is divisible by
7.

2. Kirti wants to display all the numbers from 1 to 100. How will she do it?

Answer (based on Code 7):

for i in range(1, 101):


print(i)

Competency Question

Create a step-by-step guide to explain the components of a while loop and how it is used.

Answer:

1. Initialisation – Set the starting value.


Example: i = 1
2. Condition – Checks if the loop should run.
Example: while i <= 5:
3. Loop Body – Statements to repeat.
Example: print(i)
4. Step Value – Updates the control variable.
Example: i = i + 1

Walk through the process of using membership operators in for loop to display even
numbers from 2 to 20.

Answer:

1. Use range(2, 21) to generate numbers from 2 to 20.


2. Use for i in range(2, 21): → in is the membership operator.
3. Check if number is even: if i % 2 == 0:
4. Print the number: print(i)

This prints all even numbers from 2 to 20.

You might also like