0% found this document useful (0 votes)
3 views4 pages

Loops in Python

The document covers the basics of loop constructs in Python, including the for and while loops, their syntax, and characteristics such as infinite loops. It includes fill-in-the-blank exercises, true/false statements, and short answer questions to reinforce understanding. Additionally, it provides examples of code snippets demonstrating the output of different loop implementations.
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)
3 views4 pages

Loops in Python

The document covers the basics of loop constructs in Python, including the for and while loops, their syntax, and characteristics such as infinite loops. It includes fill-in-the-blank exercises, true/false statements, and short answer questions to reinforce understanding. Additionally, it provides examples of code snippets demonstrating the output of different loop implementations.
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/ 4

1. Fill in the blanks using the words given in the help box.

a. The for loop executes a statement or a group of statements for a fixed number of times.
b. By default, the for loop always increments by 1.
c. The range function in Python generates the numbers from 0 to n-1.
d. In the range function, the default step size is 1.
e. Strings are the sequence of characters.

2. Tick (✔) the correct option.

a. How many loop constructs are there in Python?


i. 2 ✔

b. Looping statement is also known as ______ statement.


ii. Iterative ✔

c. Which of the following loop executes a block of statement(s) as long as the condition is true?
ii. while ✔

d. If the condition of the loop is always true, then the loop will never terminate. This type of loop
is called ______ loop.
iv. Infinite ✔

e. Which of the following function counts the number of times a specified string appears in the
string?
ii. count ✔

3. Write T for true statements and F for false ones.

a. A loop consists of a set of statements that need not be repeated a number of times. F
b. The for loop takes a counter variable along with the range. T
c. The expression is evaluated first if a sequence contains an expression. T
d. The upper() function converts the uppercase characters into lowercase, if any, in the string. F
e. The conversion of characters to numbers is called decoding. F

4. Very short answer type questions

a. What is the other name for iterative statements?


Looping statements.

b. Write any one advantage of loop statement.


It reduces code redundancy by repeating a set of instructions automatically.

c. Write the syntax of while loop.


while condition:
# statements

d. What is an infinite loop?


A loop that never terminates because the condition always evaluates to True.

e. What is the use of capitalize() function?


The capitalize() function converts the first character of a string to uppercase and the rest to
lowercase.

5. Short answer type questions

a. What is the output of the following code snippets?

i.
i=1
while i <= 10:
print(i**2)
i += 1
Output:
1
4
9
16
25
36
49
64
81
100

ii.
x = 10
y = 62
while x < 15 and y < 100:
print(x, y)
x += 1
y += 1
Output:
10 62
11 63
12 64
13 65
14 66

iii.
for i in range(3):
print(i)
Output:
0
1
2

iv.
for i in range(2**2):
print('Hello!')
Output:
Hello!
Hello!
Hello!
Hello!

6. Long answer type questions

a. Write the structure of looping statements.


For loop:
for variable in sequence:
# statements

While loop:
while condition:
# statements

b. What is an infinite loop? Give an example.


An infinite loop is a loop that runs indefinitely because its condition always evaluates to True.
Example:

while True:
print("This is an infinite loop")

c. What is the difference between the for loop and the while loop? Explain with an example.

For loop: Iterates over a sequence (like a list, string, or range).


Example:
for i in range(5):
print(i)
While loop: Repeats as long as the condition is true.
Example:
i=0
while i < 5:
print(i)
i += 1

d. What are strings in Python? Give example of any two strings.


Strings are sequences of characters enclosed in quotes.
Example:
string1 = "Hello, World!"
string2 = 'Python Programming'

e. Draw the flowchart of for loop.

[Flowchart for "for loop": Start -> Initialize -> Condition -> Execute Statement -> Increment ->
Check Condition -> End]

You might also like