Comma Code - Automate the Boring Stuff I'm currently working through an exercise of Chapter 4 from Automate the Boring Stuff, which reads as follows:

&quot;Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']
Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it.&quot;

Since I'm an absolute beginner to Python (and programming in general), I wanted to get some advice on how to make this code cleaner. It works with any size list, but I've reviewed some other solutions on SO and it seems like there are a million ways to build this out. How can I make this more straightforward?
spam = ['apples', 'bananas', 'tofu', 'cats']

def commaCode(listVar):
    if len(listVar) == 0: # condition for an empty list
        print('There are no items in this list.') 
    
    if len(listVar) == 1: # condition for a list with 1 item
        return (str(listVar[0])+'.')

    if len(listVar) == 2: # condition for a list with 2 items
        return (str(listVar[0]) + ' and ' + str(listVar[1]) + '.')

    if len(listVar) &gt;=3: # conditions for lists with 3+ items
        listDisp = ''
        for i in range(len(listVar[:-1])):
            listDisp = listDisp + str(listVar[i]) + ', '
        listDisp = listDisp + 'and ' + str(listVar[-1])
        return (listDisp)

commaCode(spam)
You want to look at str.join
by the last case, len(listVar) is run 4 times, which is very repetitive.  Better to set it to a temporary variable, and check that each time.
Python has some binary convenience  operators for common things like appending.  Instead of listDisp = listDisp + foo use listDisp += foo
instead of calling str() on everything as you build the string, why not call it up front, so it can't be missed somewhere along the way.  Something at the top like stringList = [ str(i) for i in listVar ] will remove the need for all those str() calls sprinkled throughout the code.
Naming:  listVar is a variable.  It doesn't need to have 'var(iable)' in its name.  How about 'inputList'?  Names are important - they're messages to the future (you or whoever reads your code) about what you are/were trying to accomplish.  Similar issue with the name commaCode.
range(len(listVar[:-1])) is the same as range(len(listVar)-1), which is both easier to read, and can take advantage of point 2 and end up written as range(listVarLen-1) which is definitely clearer.


Overall though, for a beginner, not bad!