n = int(input("Enter the number: "))
startRow = 0
endRow = n - 1
startColumn = 0
endColumn = n - 1
k = 1
matrix = [[0]*n]*n
i = startColumn
while(i <= endColumn):
matrix[startRow][i] = k
k = k + 1
i += 1
matrix[0][1] = 6
print(matrix)
output
python print_patterns.py
Enter the number: 5
[[1, 6, 3, 4, 5], [1, 6, 3, 4, 5], [1, 6, 3, 4, 5], [1, 6, 3, 4, 5], [1, 6, 3, 4, 5]]
Q1. I am unable to understand why second element in every array is 6?
Q2. In the first while loop I have assigned values only to first array elements but still it allocated all the rows with same data?