(Sep-06-2019, 10:19 AM)Subash_P Wrote: Hi All,
I am new to Python programming.
I am trying to display nested list like a matrix. I am trying with below code.
x=[[1,2,3],[4,5,6],[7,8,9]]
i=0
for i in range(len(x)):
for j in range(len(x[i])):
print(x[i][j],end=' ')
print()
I am getting data displayed like matrix only.
My doubt here is "what exactly end =' ' and print()" functions are doing.
Can anyone of you clarify me this point.
Thanks,
Subash
Hi!
Although
Perfringo has given you an answer, and it's clear that
Perfringo has helped many Python users (with more than 800 posts here!!!), I think that maybe, depending on the users and their way of learning (more when they are newbies like myself), they can get lost with the documentation available. I would say that for me, at least, that documentation will be very helpful later on, when I know more about Python, but right now, it gives me more questions than answers... (For instance, what's sys.stdout?, what's flush?...)
If you are like me, a newbie, and one that needs to see what each bit of code does, maybe you just have to do exactly that, by modifying the code and see how that changes the output.
For that, I modified a little program of mine, to show a matrix and its output:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
for j in range(len(m1[i])):
print(m1[i][j], end=' ')
print()
That gives the following output:
Output:
Building matrix m1 (9x6):
00 01 02 03 04 05
10 11 12 13 14 15
20 21 22 23 24 25
30 31 32 33 34 35
40 41 42 43 44 45
50 51 52 53 54 55
60 61 62 63 64 65
70 71 72 73 74 75
80 81 82 83 84 85
You asked what
end=''
and
print()
do. Let's find out by modifying the code. First, we eliminate the
print()
from the code, so it looks now like this:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
for j in range(len(m1[i])):
print(m1[i][j], end=' ')
and that changes the output to:
Output:
Building matrix m1 (9x6):
00 01 02 03 04 05 10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55 60 61 62 63 64 65 70 71 72 73 74 75 80 81 82 83 84 85
So it seems that
print()
makes the program to print the next thing in another line (each thing in this case is each row, as
for j in range(len(m1[i])):
print(m1[i][j], end=' ')
prints a row of the matrix).
Also notice the indentation, as it is very important in Python. If you change the indentation for
print()
from the
for i loop
(the loop starting with
for i in range(len(m1)):
to the
for j loop
, the loop starting with
for j in range(len(m1[i])):
, the program changes to:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
for j in range(len(m1[i])):
print(m1[i][j], end=' ')
print()
producing the following output:
Output:
Building matrix m1 (9x6):
00
01
02
03
04
05
10
11
12
13
14
15
20
21
22
23
24
25
30
31
32
33
34
35
40
41
42
43
44
45
50
51
52
53
54
55
60
61
62
63
64
65
70
71
72
73
74
75
80
81
82
83
84
85
Why is that? Because
print()
makes print the next thing in a new line, and in this case, by being at the same level (indentation) as
print(m1[i][j], end=' ')
, what it does is printing each thing in a new line, but in this case, each thing is each element of the matrix!!!
You asked also what was the meaning of
end=''
, and although it is explained in the documentation provided by
Perfringo, I'd try to explain it more visually, by modifying again the original program with the elimination of
end=''
to see what happens:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
for j in range(len(m1[i])):
print(m1[i][j],)
print()
As you can see, I left the comma (,) that was before the
end=''
, but you can also eliminate it:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
for j in range(len(m1[i])):
print(m1[i][j])
print()
as both programs, with or without that comma (,) produce the same output (although I guess, doing it without the comma (,) is better code):
Output:
Building matrix m1 (9x6):
00
01
02
03
04
05
10
11
12
13
14
15
20
21
22
23
24
25
30
31
32
33
34
35
40
41
42
43
44
45
50
51
52
53
54
55
60
61
62
63
64
65
70
71
72
73
74
75
80
81
82
83
84
85
So, again, (as with
print()
), it is something related with the display of the result. The
end=''
makes print the elements of the result without any space between them (in this case, respecting the blank line between the elements of different rows, given by the
print()
line).
And finally, to show that in fact, the blank line between the elements of different rows, is given by the
print()
line in the last output, I'm going to modify, once again, the program to eliminate both the
end=''
and the
print()
parts from the program:
m1 = [['00', '01', '02', '03', '04', '05'], ['10', '11', '12', '13', '14', '15'], ['20', '21', '22', '23', '24', '25'], ['30', '31', '32', '33', '34', '35'], ['40', '41', '42', '43', '44', '45'], ['50', '51', '52', '53', '54', '55'], ['60', '61', '62', '63', '64', '65'], ['70', '71', '72', '73', '74', '75'], ['80', '81', '82', '83', '84', '85']]
print("\n\nBuilding matrix m1 (9x6):\n")
for i in range(len(m1)):
for j in range(len(m1[i])):
print(m1[i][j])
and now we have a new output:
Output:
Building matrix m1 (9x6):
00
01
02
03
04
05
10
11
12
13
14
15
20
21
22
23
24
25
30
31
32
33
34
35
40
41
42
43
44
45
50
51
52
53
54
55
60
61
62
63
64
65
70
71
72
73
74
75
80
81
82
83
84
85
because
end=''
(that makes
inside the print function to write each new element without any space after the previous one) is not present now, and because
print()
(that it seems to write a blank line after each row, but that in conjunction with
end=''
, it seems that it writes the following row just in another line) is not present now either.
I hope this clarifies what
end=''
and
print()
do for you.
All the best,