0% found this document useful (0 votes)
20 views2 pages

Nested For Loop Exercises Solutions

The document contains a series of nested for loop exercises in Python, each demonstrating different patterns and outputs. It includes code snippets for printing stars, numbers, ASCII characters, and creating specific shapes. The exercises range from simple to more complex patterns, providing practice for understanding nested loops.

Uploaded by

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

Nested For Loop Exercises Solutions

The document contains a series of nested for loop exercises in Python, each demonstrating different patterns and outputs. It includes code snippets for printing stars, numbers, ASCII characters, and creating specific shapes. The exercises range from simple to more complex patterns, providing practice for understanding nested loops.

Uploaded by

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

Answers – Nested For Loop Practice (Set 2)

Question 1
for i in range(1, 6):
for j in range(i):
print("*", end=" ")
print()

Question 2
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print()

Question 3
for i in range(4):
for j in range(65, 69):
print(chr(j), end=" ") #chr( ) in python brings the value of ascii characters for the given number
print()

Question 4
for i in range(1, 5):
for j in range(65, 65 + i):
print(chr(j), end=" ")
print()

Question 5
n=5
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()
Question 6
for i in range(1, 6):
for j in range(i):
print(i, end=" ")
print()

Question 7

Question 8
for i in range(5):
for j in range(5):
if (i + j) % 2 == 0:
print("X", end=" ")
else:
print("O", end=" ")
print()

Common questions

Powered by AI

To reverse the order of numbers printed in each row in Question 2, the inner loop can be modified to iterate in descending order. Instead of iterating from 1 to i+1, you iterate from i down to 1. This can be done by changing the for loop to for j in range(i, 0, -1):. This alteration will cause the numbers to be printed in decreasing order within each row, starting from the highest number down to 1 for rows 1 to 5.

In Question 5, the conditional logic checks whether a cell is on the border of a 5x5 grid, determined by whether the row (i) or column (j) indices are either 0 or n-1, where n is the grid size (5). If a cell meets these conditions, an asterisk is printed, otherwise, a space is printed. An alternative method could involve constructing the border strings separately and then combining them with empty-line strings for non-border sections, although this may reduce clarity and scalability.

The nested loop structure in Question 5 efficiently prints a 5x5 square with asterisks as the border. The conditionals in the inner loop ensure that the asterisks are printed only on the boundaries of the square. This approach is efficient given the fixed grid size; however, for larger grids, a change from a conditional check per cell to an indexed access strategy could improve performance slightly by reducing branching (particularly for languages with more significant branching costs). The current method is quite readable and directly translates logical conditions into code.

In Question 4, the nested for loop generates rows of increasing sequences of letters starting from 'A'. The outer loop controls the number of rows and starts each sequence, while the inner loop leverages ASCII values to increment letters up to the current row count, producing the pattern. Changing the loop boundaries, such as the maximum outer loop range or the start value of the inner loop, would directly affect the number of rows and the length of each sequence, potentially altering the intended pattern output significantly.

Question 8 creates a 5x5 grid of alternating 'X's and 'O's. This checkerboard pattern is formed by two nested loops, both iterating from 0 to 4, and a conditional statement within the inner loop. The pattern is controlled by the expression (i + j) % 2 == 0, which alternates the characters by checking the sum of the row and column indices. When the sum is even, an 'X' is printed; when odd, an 'O' appears. This setup exploits the modulo operation to simply switch between two characters, effectively creating the checkerboard effect.

The key structural difference between Question 1 and Question 6 is what the inner loop prints; Question 1 prints a single character ('*'), leading to asterisks forming a triangle. In contrast, Question 6 prints the current outer loop value repeatedly, which results in consecutive rows of increasing identical numbers. This choice of inner loop object to print fundamentally influences the output: Question 1 produces a char-based geometric shape, while Question 6 produces numeric series where each row's number and repetitions align with row indices.

In Questions 3 and 4, the ASCII character functions (chr()) are used to convert integer values to their corresponding character representations, thus allowing the printing of alphabetic characters based on their ASCII values. This conversion is necessary because the loop increments over integer values, which need to be transformed into characters to produce the desired patterns of letters. Utilizing ASCII allows the program to handle letters as numeric data, facilitating operations such as iteration and direct arithmetic manipulation, crucial for dynamically adjusting character sequences.

Question 6 has a nested loop where the outer loop ranges from 1 to 5 and the inner loop iterates up to the current value of the outer loop variable. This structure results in repeated printing of the outer loop's index value, generating a pattern where each row displays a number repeated according to its value. A potential drawback is the static nature of the upper range; if the range is extended, the implementation would not adapt seamlessly to dynamically adjust repetitions unless logic is changed to accommodate variable upper bounds, limiting reusability in different contexts.

Question 3 prints four rows of repeating sequences of the letters 'A' to 'D'. This is achieved by using the ASCII values for 'A' (65) through 'D' (68) and converting them to characters with the chr function. The outer loop runs four times, and the inner loop iterates over the ASCII values from 65 to 68, printing their character equivalent on each iteration of the outer loop.

Question 1 generates a right-angled triangle pattern using asterisks ('*'). Each row from 1 to 5 prints an increasing number of asterisks, starting with one asterisk in the first row and increasing to five asterisks in the fifth row. This pattern is achieved by using a nested for loop structure where the outer loop runs from 1 to 5, indicating the number of rows, and the inner loop runs from 0 to the current value of the outer loop variable, determining the number of times an asterisk is printed in that row.

You might also like