python - How to remove the last comma in a list of integers? -
the list supposed square of each number. i've managed need remove last comma in sequence.
when use code:
def multiplicator(): in range(3, 20): b = (a*a) print(b, end=",") multiplicator()
i get:
9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,
you can keep loop , add condition:
def multiplicator(): in range(3, 20): b = (a*a) print(b, end="") if a<19: # if not last element print(end=",") # print "," print() # print new line after multiplicator() # => 9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361
you can use ternary condition shorten code:
def multiplicator(): in range(3, 20): b = (a*a) print(b, end="," if a<19 else "") print() # print new line after multiplicator() # => 9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361
Comments
Post a Comment