Q:

Remove comma at end of pattern python 3

I am running the python code below and getting output 6, 3, 0 ,3, 6,. Can anyone suggest how to remove the comma in the end.
n = 6
k = 3
def pattern(n,k):
    if n<=0:
        print(n,end = " ,")
    else: 
        print(n,end =", ")
        pattern(n-k,k)
        print(n,end=", ")

pattern(n,k)

A:

The smallest modification to your code is to distinguish the top-level call, and not output the comma in that case:
def pattern(n, k, top=True):
    if n <= 0:
        print(n, end=", ")
    else: 
        print(n, end=", ")
        pattern(n-k, k, False)
        print(n, end = "" if top else ", ")

But I'd probably solve it quite differently:
def pattern(n, k):
    seq = list(range(n, -1, -k))
    return seq + list(reversed(seq[:-1]))

