0

Explain to me the difference

code N1 a = [] for x in range(1500,2000): if x % 7 == 0 and x % 5 == 0: a.append(x) print (a) ################# Code N2 a = [] for x in range(1500, 2000): if (x%7==0) and (x%5==0): a.append(str(x)) print (','.join(a))

12th May 2019, 1:16 AM
kuzantiv
kuzantiv - avatar
5 Answers
+ 3
1. In Code N1 you append integers to list "a". In Code N2 you append strings. 2. Output is different. Code N1: [1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995] Code N2: 1505,1540,1575,1610,1645,1680,1715,1750,1785,1820,1855,1890,1925,1960,1995
12th May 2019, 1:22 AM
Diego
Diego - avatar
+ 1
Not necessarily better. It all depends on what your program does and how you want the output to look like.
12th May 2019, 1:38 AM
Diego
Diego - avatar
0
Then what is the difference now? #code N3 a = [] for x in range(1500, 2000): if (x%7==0) and (x%5==0): a.append(str(x)) print (a)
12th May 2019, 1:29 AM
kuzantiv
kuzantiv - avatar
0
The only difference is that strings are being appended.
12th May 2019, 1:30 AM
Diego
Diego - avatar
0
So code N3 is more laconic, which is better right?
12th May 2019, 1:33 AM
kuzantiv
kuzantiv - avatar