Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Nested Loops Practice

Loops inside loops — pattern printing, matrix traversal, and nested iteration.

1
Right-Angled Triangle Pattern
Easy
Given an integer N, print a right-angled triangle pattern of asterisks with N rows.
A single integer N.
Print N rows. Row i should contain i asterisks.
Sample Input 1
4
Sample Output 1
*
**
***
****
Sample Input 2
1
Sample Output 2
*
Sample Input 3
6
Sample Output 3
*
**
***
****
*****
******
1 ≤ N ≤ 50
Use outer loop for rows and inner loop for columns. Inner loop runs i times.
2
Square Pattern of Numbers
Easy
Given an integer N, print an N×N square pattern where each cell contains the column number.
A single integer N.
Print N rows, each containing N numbers separated by spaces. The j-th number in each row should be j.
Sample Input 1
4
Sample Output 1
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Sample Input 2
1
Sample Output 2
1
Sample Input 3
5
Sample Output 3
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 ≤ N ≤ 20
Outer loop for N rows, inner loop for N columns printing the column index.
3
Inverted Right-Angled Triangle
Easy
Given N, print an inverted right-angled triangle pattern. The first row has N stars, and each subsequent row has one fewer star.
A single integer N.
Print N rows. Row 1 has N stars, row 2 has N-1 stars, ..., row N has 1 star.
Sample Input 1
5
Sample Output 1
*****
****
***
**
*
Sample Input 2
1
Sample Output 2
*
Sample Input 3
4
Sample Output 3
****
***
**
*
1 ≤ N ≤ 50
Outer loop i from N down to 1, inner loop runs i times.
4
Number Triangle
Easy
Row i: number i repeated i times.
Single int N
N rows.
Sample Input 1
4
Sample Output 1
1
2 2
3 3 3
4 4 4 4
Sample Input 2
1
Sample Output 2
1
Sample Input 3
5
Sample Output 3
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
1≤N≤20
Nested loops.
5
Same Number Square
Easy
N×N square with row numbers.
Single int N
N rows of N numbers.
Sample Input 1
3
Sample Output 1
1 1 1
2 2 2
3 3 3
Sample Input 2
1
Sample Output 2
1
Sample Input 3
5
Sample Output 3
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
1≤N≤20
Nested loops.
6
Alphabet Square
Easy
N×N square with letters A,B,C...
Single int N (1-26)
N rows each with N letters.
Sample Input 1
4
Sample Output 1
A A A A
B B B B
C C C C
D D D D
Sample Input 2
1
Sample Output 2
A
Sample Input 3
26
Sample Output 3
A A A A A A A A A A A A A A A A A A A A A A A A A A
B B B B B B B B B B B B B B B B B B B B B B B B B B
C C C C C C C C C C C C C C C C C C C C C C C C C C
D D D D D D D D D D D D D D D D D D D D D D D D D D
E E E E E E E E E E E E E E E E E E E E E E E E E E
F F F F F F F F F F F F F F F F F F F F F F F F F F
G G G G G G G G G G G G G G G G G G G G G G G G G G
H H H H H H H H H H H H H H H H H H H H H H H H H H
I I I I I I I I I I I I I I I I I I I I I I I I I I
J J J J J J J J J J J J J J J J J J J J J J J J J J
K K K K K K K K K K K K K K K K K K K K K K K K K K
L L L L L L L L L L L L L L L L L L L L L L L L L L
M M M M M M M M M M M M M M M M M M M M M M M M M M
N N N N N N N N N N N N N N N N N N N N N N N N N N
O O O O O O O O O O O O O O O O O O O O O O O O O O
P P P P P P P P P P P P P P P P P P P P P P P P P P
Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q
R R R R R R R R R R R R R R R R R R R R R R R R R R
S S S S S S S S S S S S S S S S S S S S S S S S S S
T T T T T T T T T T T T T T T T T T T T T T T T T T
U U U U U U U U U U U U U U U U U U U U U U U U U U
V V V V V V V V V V V V V V V V V V V V V V V V V V
W W W W W W W W W W W W W W W W W W W W W W W W W W
X X X X X X X X X X X X X X X X X X X X X X X X X X
Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y
Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z
1≤N≤26
chr(65+i-1)
7
Odd Number Triangle
Easy
Row i: first i odd numbers.
Single int N
N rows.
Sample Input 1
4
Sample Output 1
1
1 3
1 3 5
1 3 5 7
Sample Input 2
1
Sample Output 2
1
Sample Input 3
5
Sample Output 3
1
1 3
1 3 5
1 3 5 7
1 3 5 7 9
1≤N≤20
Counter +=2 each iteration.
8
Binary Triangle
Easy
Row i: i binary digits starting with 1.
Single int N
N rows.
Sample Input 1
4
Sample Output 1
1
1 0
1 0 1
1 0 1 0
Sample Input 2
1
Sample Output 2
1
Sample Input 3
5
Sample Output 3
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
1≤N≤20
(j%2==1) for 1 else 0.
9
Right-Aligned Number Triangle
Easy
Row i: (N-i) spaces then 1..i
Single int N
N rows right-aligned.
Sample Input 1
4
Sample Output 1
1
  1 2
 1 2 3
1 2 3 4
Sample Input 2
1
Sample Output 2
1
Sample Input 3
5
Sample Output 3
1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5
1≤N≤20
Spaces + numbers loops.
10
V Pattern
Easy
V-shaped asterisk pattern.
Single int N (height ≥2)
N rows V shape.
Sample Input 1
4
Sample Output 1
*     *
 *   *
  * *
   *
Sample Input 2
2
Sample Output 2
* *
 *
Sample Input 3
5
Sample Output 3
*       *
 *     *
  *   *
   * *
    *
2≤N≤20
Stars at col==i and col==2*N-i.
1
Pyramid Pattern
Medium
Given N, print a centered pyramid pattern of asterisks.
A single integer N.
Print N rows forming a centered pyramid. Row i should have (N-i) spaces followed by (2*i - 1) asterisks.
Sample Input 1
4
Sample Output 1
*
  ***
 *****
*******
Sample Input 2
1
Sample Output 2
*
Sample Input 3
5
Sample Output 3
*
   ***
  *****
 *******
*********
1 ≤ N ≤ 30
Outer loop for rows. Inner loops: one for spaces (N-i), one for stars (2*i-1).
2
Diamond Pattern
Medium
Given N, print a diamond pattern of asterisks. The diamond should have 2*N - 1 rows.
A single integer N (half the height).
Print 2*N-1 rows forming a diamond shape centered horizontally.
Sample Input 1
3
Sample Output 1
*
 ***
*****
 ***
  *
Sample Input 2
1
Sample Output 2
*
Sample Input 3
4
Sample Output 3
*
  ***
 *****
*******
 *****
  ***
   *
1 ≤ N ≤ 20
First N rows: increasing pyramid (i=1..N). Next N-1 rows: decreasing pyramid (i=N-1..1).
3
Generate Multiplication Table (1 to N)
Medium
Given N, generate and display the multiplication tables from 1 to N.
A single integer N.
For each number i from 1 to N, print its multiplication table from 1 to 10. Separate each table with a blank line.
Sample Input 1
3
Sample Output 1
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Sample Input 2
1
Sample Output 2
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
Sample Input 3
2
Sample Output 3
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
1 ≤ N ≤ 20
Outer loop for each number from 1 to N, inner loop for multiplying 1 to 10.
4
Hourglass Pattern
Medium
N rows then N-1 rows mirrored. Centered.
Single int N (half)
2N-1 rows.
Sample Input 1
3
Sample Output 1
*****
 ***
  *
 ***
*****
Sample Input 2
2
Sample Output 2
***
 *
***
Sample Input 3
4
Sample Output 3
*******
 *****
  ***
   *
  ***
 *****
*******
2≤N≤20
Decrease then increase.
5
Number Pyramid
Medium
Row i: 1..i then i-1..1, centered.
Single int N (1-9)
N rows pyramid.
Sample Input 1
5
Sample Output 1
1
   121
  12321
 1234321
123454321
Sample Input 2
1
Sample Output 2
1
Sample Input 3
9
Sample Output 3
1
       121
      12321
     1234321
    123454321
   12345654321
  1234567654321
 123456787654321
12345678987654321
1≤N≤9
3 inner loops: spaces, asc, desc.
6
Butterfly Pattern
Medium
Top N rows increasing stars, bottom N mirror.
Single int N (half)
2N rows.
Sample Input 1
4
Sample Output 1
*      *
**    **
***  ***
********
********
***  ***
**    **
*      *
Sample Input 2
2
Sample Output 2
*  *
****
****
*  *
Sample Input 3
3
Sample Output 3
*    *
**  **
******
******
**  **
*    *
2≤N≤25
Spaces = 2*(N-i) top, 2*i bottom.
7
Matrix Diagonal Sum
Medium
Sum of primary and secondary diagonals.
Line1: N. Next N lines: matrix
"Primary: S" and "Secondary: S"
Sample Input 1
3
1 2 3
4 5 6
7 8 9
Sample Output 1
Primary: 15
Secondary: 15
Sample Input 2
2
1 2
3 4
Sample Output 2
Primary: 5
Secondary: 5
Sample Input 3
1
5
Sample Output 3
Primary: 5
Secondary: 5
1≤N≤100
i==j and i+j==N-1
8
Matrix Transpose
Medium
Transpose R×C matrix.
Line1: R C. Lines: matrix
C rows × R columns.
Sample Input 1
2 3
1 2 3
4 5 6
Sample Output 1
1 4
2 5
3 6
Sample Input 2
2 2
1 2
3 4
Sample Output 2
1 3
2 4
Sample Input 3
1 3
1 2 3
Sample Output 3
1
2
3
1≤R,C≤100
result[j][i] = matrix[i][j]
9
Matrix Multiplication
Medium
Multiply A(R1×C1) × B(R2×C2) where C1==R2
Line1: R1 C1. A. Line: R2 C2. B.
Result R1×C2 matrix.
Sample Input 1
2 3
1 2 3
4 5 6
3 2
7 8
9 10
11 12
Sample Output 1
58 64
139 154
Sample Input 2
1 1
2
1 1
3
Sample Output 2
6
Sample Input 3
2 2
1 2
3 4
2 2
5 6
7 8
Sample Output 3
19 22
43 50
C1==R2, ≤50
Triple nested loop: i,j,k.
10
Checkerboard Pattern
Medium
N×N 0/1 checkerboard.
Single int N
N rows of N numbers.
Sample Input 1
4
Sample Output 1
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Sample Input 2
1
Sample Output 2
0
Sample Input 3
5
Sample Output 3
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1≤N≤20
(i+j)%2==0 -> 0 else 1
1
Hollow Square Pattern
Hard
Given N, print a hollow square pattern of asterisks. The boundary of the square should be filled with stars, and the interior should be empty (spaces).
A single integer N (side length).
Print N rows forming a hollow square of side N.
Sample Input 1
5
Sample Output 1
*****
*   *
*   *
*   *
*****
Sample Input 2
3
Sample Output 2
***
* *
***
Sample Input 3
6
Sample Output 3
******
*    *
*    *
*    *
*    *
******
3 ≤ N ≤ 50
Print asterisk at positions where i==1, i==N, j==1, or j==N, otherwise print space.
2
Pascal's Triangle
Hard
Given N, print the first N rows of Pascal's Triangle. Each number is the sum of the two numbers directly above it.
A single integer N.
Print N rows of Pascal's Triangle. Each row should be centered. Values should be separated by spaces.
Sample Input 1
5
Sample Output 1
1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1
Sample Input 2
1
Sample Output 2
1
Sample Input 3
6
Sample Output 3
1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1
1 5 10 10 5 1
1 ≤ N ≤ 20
Each row starts and ends with 1. Middle values: row[i][j] = row[i-1][j-1] + row[i-1][j]. Use nested loops.
3
Matrix Addition
Hard
Given two matrices A and B of size R × C, compute their sum (element-wise addition).
First line: two integers R and C. Next R lines: matrix A (C integers per line). Next R lines: matrix B (C integers per line).
Print the resulting matrix with R rows, each containing C space-separated integers.
Sample Input 1
2 3
1 2 3
4 5 6
7 8 9
10 11 12
Sample Output 1
8 10 12
14 16 18
Sample Input 2
1 1
5
7
Sample Output 2
12
Sample Input 3
1 2
1 2
3 4
Sample Output 3
4 6
1 ≤ R, C ≤ 100, -10^6 ≤ elements ≤ 10^6
Use nested loops: outer for rows, inner for columns. Result[i][j] = A[i][j] + B[i][j].
4
Spiral Matrix
Hard
N×N spiral clockwise from 1 to N².
Single int N
N rows spiral matrix.
Sample Input 1
3
Sample Output 1
1 2 3
8 9 4
7 6 5
Sample Input 2
1
Sample Output 2
1
Sample Input 3
4
Sample Output 3
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
1≤N≤20
4 boundaries: top, bottom, left, right.
5
Snake Pattern Matrix
Hard
Numbers 1 to N² in snake order (L→R, R→L alternating).
Single int N
N rows.
Sample Input 1
4
Sample Output 1
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
Sample Input 2
1
Sample Output 2
1
Sample Input 3
3
Sample Output 3
1 2 3
6 5 4
7 8 9
1≤N≤50
Even row L→R, odd R→L.
6
Zigzag Traversal
Hard
Print matrix in zigzag (L→R, R→L per row).
Line1: R C. Lines: matrix
Elements space-separated.
Sample Input 1
3 3
1 2 3
4 5 6
7 8 9
Sample Output 1
1 2 3 6 5 4 7 8 9
Sample Input 2
2 2
1 2
3 4
Sample Output 2
1 2 4 3
Sample Input 3
1 3
1 2 3
Sample Output 3
1 2 3
1≤R,C≤100
Even row: L→R, odd: R→L
7
Rotate Matrix 90
Hard
Rotate N×N 90° clockwise in-place.
Line1: N. Lines: matrix
Rotated matrix.
Sample Input 1
3
1 2 3
4 5 6
7 8 9
Sample Output 1
7 4 1
8 5 2
9 6 3
Sample Input 2
1
5
Sample Output 2
5
Sample Input 3
2
1 2
3 4
Sample Output 3
3 1
4 2
1≤N≤100
Transpose then reverse each row.
8
Magic Square Check
Hard
All rows, cols, diagonals have same sum?
Line1: N odd. Lines: matrix
"Magic Square" or "Not"
Sample Input 1
3
8 1 6
3 5 7
4 9 2
Sample Output 1
Magic Square
Sample Input 2
3
2 7 6
9 5 1
4 3 8
Sample Output 2
Magic Square
Sample Input 3
2
1 2
2 1
Sample Output 3
Not
3≤N≤20, odd
Compute expected sum, verify.
9
Saddle Point
Hard
Element = row min AND col max.
Line1: N. Lines: matrix
"Saddle Point: V at (r,c)" or "No Saddle Point"
Sample Input 1
3
3 2 1
5 6 7
9 8 4
Sample Output 1
No Saddle Point
Sample Input 2
2
1 2
3 4
Sample Output 2
Saddle Point: 3 at (2, 1)
Sample Input 3
3
1 2 3
4 5 6
7 8 9
Sample Output 3
Saddle Point: 7 at (3, 1)
1≤N≤100
Find min per row, check if max in col.
10
Boundary Sum
Hard
Sum of first/last row and column elements (no double-count corners).
Line1: R C. Lines: matrix
Sum int.
Sample Input 1
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Sample Output 1
102
Sample Input 2
2 2
1 2
3 4
Sample Output 2
10
Sample Input 3
3 3
1 2 3
4 5 6
7 8 9
Sample Output 3
40
2≤R,C≤100
Add i==0, i==R-1, j==0, j==C-1, skip corners twice.