Ch 8: More About Python
Exemplar
Exercise C :-
Q - Give the output of the following codes :
(a)
Num = 0
While (NUM<10):
Num+-1
If Num == 5:
Break
print(Num,end= “,”)
Output :
(b)
For val in “String”:
If val == “i”:
Break
print(val)
print(“Over”)
Output :
Exercise D :-
Ques 1 . What is the significance of using iterations in Python?
Ans 1 . Interactions are used in Python when there is a need to repeat a ser of statements more than once
based on a certain condition. This process of repetition is called loop or iteration in programming.
A loop is thus used to repeat a block of statements a specific number of times.
The loops available in Python are:-
1. for loop
2. while loop
Ques 2. Explain range() function with the help of an example.
Ans 2. The range() function in Python is used to generate a sequence of numbers.
It is often used in loops to iterate over a specific range of numbers.
The syntax of the range() function is:
range(start, stop, step)
● start (optional): The starting value of the sequence. By default, it is 0.
● stop (required): The value at which the sequence stops (exclusive).
● step (optional): The difference between consecutive numbers in the sequence. By default, it is 1.
Example :
# Generate numbers from 2 to 10 with a step of 2
for i in range(2, 11, 2):
print(i)
Ques 3. Differentiate between for and while loops with the help of an example.
Ans 3.
Difference between for and while loops in Python:
Feature for Loop while Loop
Usage Used when the number of Used when the number of
iterations is known beforehand iterations is unknown and
or iterating over a sequence. depends on a condition.
Syntax Iterates over a sequence (e.g., Continues as long as a condition
list, string, range). evaluates to True.
Condition Implicitly manages iterations Explicit condition checking
Checking based on the sequence or range determines when the loop stops.
provided.
Best Suited For Iterating through collections or Conditions that depend on
known ranges. dynamic inputs or unknown
values.
Example :
for Loop Example: while Loop Example:
# Print numbers from 1 to 5 using a for-loop # Print numbers from 1 to 5 using a while loop
for i in range(1, 6): count = 1
print(i) while count <= 5:
print(count)
count += 1
Output : Output :
1 1
2 2
3 3
4 4
5 5
Ques 4. What are jump statements ? Explain the jump statements in Python.
Ans 4. Jump statements are used to alter the normal flow of control in a program. These statements
"jump" the control to a different part of the program.
Jump statements in Python:
break Statement continue Statement
The break statement is used to terminate a loop The continue statement is used to skip the current
prematurely. It immediately exits the loop, iteration of a loop and move to the next iteration.
regardless of the loop's condition.
Example: Example:
for i in range(1, 6): for i in range(1, 6):
if i == 3: if i == 3:
break continue
print(i) print(i)
Output : Output :
1 1
2 2
4
5
Explanation: The break statement exits the loop Explanation: The continue statement skips the
when i becomes 3. iteration when i equals 3, and the loop continues
with the next iteration.