Python

P
PythonHub
Progress 0%

Python

  • Home
  • History
  • Applications
  • Introduction
    • What is Python
    • Character Set
    • Tokens
    • Execution Mode
    • Variables
    • Data Types
    • Operators
    • Constants
    • Assignment
    • I/O
    • Scripts
    • Namespace
    • šŸ“ Assignments
  • Loops
    • Intro to Loops
    • While Loop
    • Nested While
    • šŸ“ While Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • šŸ“ Nested For Examples
    • Infinite While
    • Infinite For
    • Break/Continue
    • For vs While
    • For Each
    • šŸ“ For Each Assign.
    • šŸ“ All Loops Assign.

© Sankalan Data Tech

Python Language Interactive Tutorial

šŸ“š Python Nested For Loop: Practice Assignments

Python Nested For Loop Practice Assignments

Master nested for loops with patterns, matrices, and more.

Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers to make programming easy through practical examples and clear explanations.
šŸ“‘ On this page:
  • About
  • Patterns
  • Multiplication Tables
  • Matrix Operations
  • Advanced Patterns
  • Try It
Share:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link

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)
šŸ“– 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)
šŸ“– 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))
šŸ“– 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))
šŸ“– 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()
šŸ“– 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()
šŸ“– 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}")
šŸ“– 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()
šŸ“– 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)
šŸ“– 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)
šŸ“– 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)
šŸ“– 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)
šŸ“– 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()
šŸ“– 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)
šŸ“– 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.

Loading Pyodide... 0%
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!

šŸ“š Related Tutorials

šŸ”„ For Loop

Learn about for loops in Python

šŸ” Nested For Loop

Understand nested for loops

šŸ“ All Loops Assignments

More loop practice problems

šŸ“– Related Tutorials
  • For Loop Examples
  • While Loop
  • Break, Continue, Else
  • For vs While
Previous: Nested For Loop Next: Infinite While Loop →