Colons in definitions

Easy question, and I worked it out by trial and error but I’d like to understand the logic…

Why would I not put colons after every line in this definition?

def append_size(lst):
new_addition = len(lst)
return lst

print(append_size([23, 42, 108]))

I.e. why would it not be -

def append_size(lst):
new_addition = len(lst):
return lst


Because the colon operator has a very specific usage. For the given example, a function, it’s used to denote a specific code block (you’ll see the same thing for classes and loops etc.). If you’re coming from a different language note that python doesn’t require newlines be separated by any particular character (though you can technically use semicolons) and uses indentation to group code together.

1 Like