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))
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
+ 1
Not necessarily better. It all depends on what your program does and how you want the output to look like.
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)
0
The only difference is that strings are being appended.
0
So code N3 is more laconic, which is better right?