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

Python Iterations Practice Exercises

Uploaded by

vivaah148
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)
26 views3 pages

Python Iterations Practice Exercises

Uploaded by

vivaah148
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

CHAPTER 5: ITERATONS IN PYTHON

Computer Practice Questions


Q1. Write a python program to print the first 10 natural numbers.
Ans. (Using For Loop) (Using While Loop)
for i in range(1,11): i=1
print(i) while(i<=10):
print(i)
i=i+1
Q2. Write a python program to print the first 10 natural numbers in the
reverse order.
Ans. (Using For Loop) (Using While Loop)
for i in range(10,0,-1): i=10
print(i) while(i>=1):
print(i)
i=i-1
Q3. Write a python program to print the odd numbers between 51 to 70.
Ans. (Using For Loop) (Using While Loop)
for i in range(51,71,2): i=51
print(i) while(i<=70):
print(i)
i=i+2
Q4. Write a python program to print the even numbers between 51to70 in
reverse order.
Ans. (Using For Loop) (Using While Loop)
for i in range(70,51,-2): i=70
print(i) while(i>51):
print(i)
i=i-2
Q5. Write a python program to print the sum of first 5 natural numbers.
Ans. (Using For Loop) (Using While Loop)
s=0 s=0
for i in range(1,6): i=1
s=s+i while(i<=5):
print(s) s=s+i
i=i+1
print(s)

Lab Activity (pg. 81)


[Link] a program in Python to compute the factorial of a given
number.
Ans. #USING WHILE LOOP
n=int(input("Enter any number: "))
f=1
i=1
while(i<=n):
f=f*i
i=i+1
print(f)

#USING FOR LOOP


n=int(input("Enter any number: "))
f=1
for i in range(1,n+1):
f=f*i
print(f)
[Link] a program using while loop in Python to print the
multiplication table of 25.
Ans. #USING WHILE LOOP
i=1
while(i<=10):
print("25*",i,"=",25*i)
i+=1

#USING FOR LOOP


for i in range(1,11):
print("25*",i,"=",25*i)
[Link] a Python program to display the following pattern:
55555
4444
333
22
1
Ans. #USING WHILE LOOP
i=5
while(i>=1):
print(str(i)*i)
print()
i-=1
#USING FOR LOOP
for i in range(5,0,-1):
print(str(i)*i)
print()

Common questions

Powered by AI

For a `for loop`, start by initializing the factorial result `f = 1`, and loop through a range: `for i in range(1, n+1): f *= i`, updating the factorial by multiplying the current number `i`. Conversely, with a `while loop`, initialize similarly `f = 1`, set `i = 1`, and increment within the loop: `while(i <= n): f *= i; i += 1`. Both loops involve initialization, iterative multiplication, and controlled loop progression until the desired number is reached, calculating the factorial of `n`.

A `for loop` offers a cleaner and more intuitive approach: `for i in range(70, 51, -2): print(i)`, automatically integrating loop setup, condition checking, and counter decrement. This single line encompasses all controls needed for looping over the desired range, substantially reducing code complexity and potential loop control errors. In contrast, a `while loop` necessitates explicit initialization and manual updating: `i = 70; while(i > 51): print(i); i = i - 2`, a more detailed construct which can be verbose and error-prone when managing loop controls.

A nested loop, particularly a `for loop` or a `while loop`, is instrumental in creating ascending or descending patterns because it allows for an outer loop to handle the specific number to be printed and an inner loop to handle repetitions based on that number. For instance, to generate the pattern from 5 to 1, an outer loop like `for i in range(5, 0, -1)` controls the current number, while an inner loop or simple print like `print(str(i) * i)` or similarly in a `while` structure helps repeat the number as per `i`. This technique utilizes concise inner loop logic to match desired repetitions, aligning both number values and display output in descending order.

In a `while loop` set to print even numbers from 70 to 51 in reverse order, the logic sequence involves initializing the counter variable `i` to 70, checking if `i > 51` before each iteration, and then printing `i` followed by decrementing it by 2 to maintain even numbers. The loop structure would be initialized and managed as follows: `i = 70; while(i > 51): print(i); i = i - 2;`. This approach repeatedly checks the condition and adjusts the counter inside the loop until the condition fails.

Using a `for loop` for this purpose is generally more concise and less error-prone because it combines element initialization, condition checking, and incrementation in a single line: `for i in range(10, 0, -1): print(i)`. This reduces the risk of forgetting to update the counter variable or miswriting the loop conditions. The `while loop`, however, requires these steps separately: initializing `i = 10`, running `while(i >= 1): print(i); i = i - 1`, which can increase potential for bugs. The `for loop` can improve code readability and maintainability.

Iterative loops, such as `while` or `for` loops, are ideal for creating numeric patterns due to their precise control over iterations and intrinsic looping structures. When generating a descending pattern like from 5 to 1, these loops simplify repetition through specified boundaries and decrements, such as `for i in range(5,0,-1): print(str(i)*i)`. This deployment of loops maximizes code succinctness and readability while leveraging built-in iteration mechanisms to repeat number patterns dynamically, encapsulating both execution and flexible pattern adjustments efficiently within a singular, controlled logic structure.

The `range` function in a `for loop` optimizes the process of printing odd numbers by automating the step increment with an appropriate step size. Using `for i in range(51, 71, 2): print(i)`, the loop starts at 51 and increments by 2, directly ensuring that only odd numbers are included. This eliminates the need for manual checks to skip even numbers, thereby simplifying the logic and reducing potential errors in loop control.

In Python, using a 'for loop', you can print the first 10 natural numbers by iterating over a range object: `for i in range(1, 11): print(i)`. The loop automatically increments the counter variable `i` through each iteration of the range. In contrast, the 'while loop' requires explicit initialization and updating of the counter: start by setting `i = 1`, then use `while(i <= 10): print(i); i = i + 1`. The main structural difference is the manual control of the loop iteration variable in the 'while loop', which is handled automatically in the 'for loop'.

Using a `for loop`, the sum of the first 5 natural numbers is computed with `s = 0; for i in range(1, 6): s += i`, which is efficient as it combines all the necessary components: initialization, condition checking, and incrementation into a compact form. A `while loop` requires more lines: `i = 1; s = 0; while(i <= 5): s += i; i += 1`, which can be more prone to initialization or incrementation errors due to its more verbose and step-by-step nature. Both obtain the same result, but the `for loop` is more concise and automatic.

In a `for loop`, the multiplication table for 25 is straightforward with `for i in range(1, 11): print("25 *", i, "=", 25 * i)`, where `i` automatically increments, providing structure and termination implicitly. Conversely, a `while loop` needs more manual setup: starting with `i = 1; while(i <= 10): print("25 *", i, "=", 25 * i); i += 1`, requiring explicit initial setup and loop continuation conditions. The `for loop` informs the start and stop in a single line, thus simplifying comprehension and reducing the margin for errors.

You might also like