''' Python 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. In Ubuntu 18.04.3 ''' ## Demonstration of result depending on how matrix A is constructed ################################################################### ### creating text with 25 characters (25 = 5 x 5) tekst = 'All hands on deck.*******' # j is sqrt(25) j=5 #Creating a 5 x 5 square natrix A ### First trial ## A is defined in a straight manner, filling it with numbers as B ## in the second trial A=[[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]] # initial state of experiment print(A) print(j) print(tekst) #Now the experiment itself, with defined A: for r in range(j): for k in range(j): A[r][k] = tekst[j*r+k] print(A) ### Success! print("This result is great!") print() ## Second trial goes wrong: # B is created by creating a row first, and then filling B with the rows # B is filled with numbers too, not the same numbers just like A but # that shouldn't matter # creating a row with numbers first row=[] for k in range(j): row.append(k) #next creating the matrix B B=[] for r in range(j): B.append(row) # Initial state experiment print(B) print(j) print(tekst) # Experiment itself for r in range(j): for k in range(j): B[r][k] = tekst[j*r+k] print(B) ### Disaster! print("Disastrous result: all rows are equal to the last one") print() ### THIRD experiment ### Check whether A and B are the same or equal immediately after their ### definition/constructions A=[[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]] # construction of B row=[] for k in range(j): row.append(k) #next creating the matrix B B=[] for r in range(j): B.append(row) print() print("A and B immediately after creation:") print() print("A is ", A) print() print("B is ", B) print() print("A is B : ", A is B) print("A == B : ", A == B) print() ''' ##### RESULTS ### jaap@bromo:~/Bureaublad$ python3 codevierkant_experiment.py [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]] 5 All hands on deck.******* [['A', 'l', 'l', ' ', 'h'], ['a', 'n', 'd', 's', ' '], ['o', 'n', ' ', 'd', 'e'], ['c', 'k', '.', '*', '*'], ['*', '*', '*', '*', '*']] This result is great! [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] 5 All hands on deck.******* [['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*']] Disastrous result: all rows are equal to the last one THIRD experiment: A and B immediately after their creation: [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] False True <========= This means that the matrix B is equal to the matrix A, immediately after their construction! But their behaviour is different! jaap@bromo:~/Bureaublad$ '''