About These Assignments
This page contains 30+ practice assignments covering nested for loops in Python. You'll work with patterns, multiplication tables, matrix operations, and more.
š¢ Beginner
š Intermediate
š“ Advanced
š” Tip: Try each problem yourself first. Click "Show Solution" only after attempting.
1. Pattern Printing 6 Questions
šÆ Learning Goal: Print various patterns using nested for loops.
Beginner Q1: Right Triangle Star Pattern
Print a right-angled triangle of stars with 5 rows.
š” Solution:
n = 5
for i in range(1, n+1):
print('*' * i)
for i in range(1, n+1):
print('*' * i)
š Explanation: Outer loop controls rows, inner loop (via string multiplication) prints stars.
Beginner Q2: Inverted Right Triangle
Print an inverted right-angled triangle of stars with 5 rows.
š” Solution:
n = 5
for i in range(n, 0, -1):
print('*' * i)
for i in range(n, 0, -1):
print('*' * i)
š Explanation: Loop runs from n down to 1, decreasing stars each row.
Intermediate Q3: Pyramid Pattern
Print a pyramid of stars with 5 rows (centered).
š” Solution:
n = 5
for i in range(1, n+1):
print(' ' * (n-i) + '*' * (2*i-1))
for i in range(1, n+1):
print(' ' * (n-i) + '*' * (2*i-1))
š Explanation: Uses spaces for centering and odd number of stars per row.
Intermediate Q4: Diamond Pattern
Print a diamond pattern with 5 rows (upper + lower half).
š” Solution:
n = 5
# Upper half
for i in range(1, n+1):
print(' ' * (n-i) + '*' * (2*i-1))
# Lower half
for i in range(n-1, 0, -1):
print(' ' * (n-i) + '*' * (2*i-1))
# Upper half
for i in range(1, n+1):
print(' ' * (n-i) + '*' * (2*i-1))
# Lower half
for i in range(n-1, 0, -1):
print(' ' * (n-i) + '*' * (2*i-1))
š Explanation: Combines pyramid and inverted pyramid.
Advanced Q5: Hollow Square Pattern
Print a hollow square of stars with side length 6.
š” Solution:
n = 6
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()
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()
š Explanation: Prints '*' on borders, spaces inside.
Advanced Q6: Floyd's Triangle
Print Floyd's triangle with 5 rows (numbers).
š” Solution:
n = 5
num = 1
for i in range(1, n+1):
for j in range(i):
print(num, end=' ')
num += 1
print()
num = 1
for i in range(1, n+1):
for j in range(i):
print(num, end=' ')
num += 1
print()
š Explanation: Consecutive numbers printed row by row.
2. Multiplication Tables 3 Questions
šÆ Learning Goal: Generate multiplication tables using nested loops.
Beginner Q7: Single Table
Print the multiplication table for a given number (1-10).
š” Solution:
n = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
š Explanation: Simple loop to print table.
Intermediate Q8: Full Multiplication Table
Print the full multiplication table from 1 to 10 (10x10 grid).
š” Solution:
for i in range(1, 11):
for j in range(1, 11):
print(f"{i*j:4}", end='')
print()
for j in range(1, 11):
print(f"{i*j:4}", end='')
print()
š Explanation: Nested loops with formatted output.
Intermediate Q9: Custom Range Table
Generate a multiplication table for a given range (start, end).
š” Solution:
start = int(input("Start: "))
end = int(input("End: "))
for i in range(start, end+1):
for j in range(1, 11):
print(f"{i} x {j} = {i*j}")
print("-"*10)
end = int(input("End: "))
for i in range(start, end+1):
for j in range(1, 11):
print(f"{i} x {j} = {i*j}")
print("-"*10)
š Explanation: Dynamic range for multiplication tables.
3. Matrix Operations 4 Questions
šÆ Learning Goal: Perform matrix operations using nested for loops.
Intermediate Q10: Matrix Addition
Add two 3x3 matrices and print the result.
š” Solution:
A = [[1,2,3],[4,5,6],[7,8,9]]
B = [[9,8,7],[6,5,4],[3,2,1]]
result = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
for j in range(3):
result[i][j] = A[i][j] + B[i][j]
for row in result:
print(row)
B = [[9,8,7],[6,5,4],[3,2,1]]
result = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
for j in range(3):
result[i][j] = A[i][j] + B[i][j]
for row in result:
print(row)
š Explanation: Nested loops to add corresponding elements.
Advanced Q11: Matrix Multiplication
Multiply two 2x2 matrices.
š” Solution:
A = [[1,2],[3,4]]
B = [[5,6],[7,8]]
result = [[0,0],[0,0]]
for i in range(2):
for j in range(2):
for k in range(2):
result[i][j] += A[i][k] * B[k][j]
for row in result:
print(row)
B = [[5,6],[7,8]]
result = [[0,0],[0,0]]
for i in range(2):
for j in range(2):
for k in range(2):
result[i][j] += A[i][k] * B[k][j]
for row in result:
print(row)
š Explanation: Triple nested loop for matrix multiplication.
Advanced Q12: Transpose of Matrix
Find the transpose of a 3x4 matrix.
š” Solution:
A = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
rows, cols = 3, 4
transpose = [[0]*rows for _ in range(cols)]
for i in range(rows):
for j in range(cols):
transpose[j][i] = A[i][j]
for row in transpose:
print(row)
rows, cols = 3, 4
transpose = [[0]*rows for _ in range(cols)]
for i in range(rows):
for j in range(cols):
transpose[j][i] = A[i][j]
for row in transpose:
print(row)
š Explanation: Swaps rows and columns.
4. Advanced Patterns 4 Questions
šÆ Learning Goal: Create complex patterns using nested loops.
Advanced Q13: Pascal's Triangle
Print Pascal's triangle with 6 rows.
š” Solution:
n = 6
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
val = 1
for j in range(i+1):
print(val, end=' ')
val = val * (i-j) // (j+1)
print()
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
val = 1
for j in range(i+1):
print(val, end=' ')
val = val * (i-j) // (j+1)
print()
š Explanation: Uses binomial coefficient calculation.
Advanced Q14: Spiral Pattern
Print a spiral pattern of numbers (n x n).
š” Solution:
n = 4
matrix = [[0]*n for _ in range(n)]
top, bottom, left, right = 0, n-1, 0, n-1
num = 1
while top <= bottom and left <= right:
for i in range(left, right+1):
matrix[top][i] = num; num += 1
top += 1
for i in range(top, bottom+1):
matrix[i][right] = num; num += 1
right -= 1
for i in range(right, left-1, -1):
matrix[bottom][i] = num; num += 1
bottom -= 1
for i in range(bottom, top-1, -1):
matrix[i][left] = num; num += 1
left += 1
for row in matrix:
print(row)
matrix = [[0]*n for _ in range(n)]
top, bottom, left, right = 0, n-1, 0, n-1
num = 1
while top <= bottom and left <= right:
for i in range(left, right+1):
matrix[top][i] = num; num += 1
top += 1
for i in range(top, bottom+1):
matrix[i][right] = num; num += 1
right -= 1
for i in range(right, left-1, -1):
matrix[bottom][i] = num; num += 1
bottom -= 1
for i in range(bottom, top-1, -1):
matrix[i][left] = num; num += 1
left += 1
for row in matrix:
print(row)
š Explanation: Uses boundary variables to create spiral.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own nested loop code.
Python Code Editor
========================================
NESTED FOR LOOP PRACTICE
========================================
Right Triangle:
*
**
***
****
*****
Multiplication Table (1-5):
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
ā Write your nested loop solutions here!
NESTED FOR LOOP PRACTICE
========================================
Right Triangle:
*
**
***
****
*****
Multiplication Table (1-5):
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
ā Write your nested loop solutions here!
š Related Tutorials
š For Loop
Learn about for loops in Python
š Nested For Loop
Understand nested for loops
š All Loops Assignments
More loop practice problems