Unused import statement: that should be cleaned up.
Bad parameter name:
def print_list(list): The keyword
list() is a built-in function, you don't want your variable/parameter/function/method/class named like built-ins because later when you start relying on such keywords, this will create problems.
Example: (run this interactively)
>>> word = 'mouse'
>>> letters = list(word)
['m', 'o', 'u', 's', 'e']
>>> list = 5
>>> new_letters = list(word)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
TypeError: 'int' object is not callable
I think the example is self-explanatory why you shouldn't use built-ins in your naming.
Docstrings: Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. ... A docstring is simply a multi-line string, that is not assigned to anything. It is specified in source code that is used to document a specific segment of code. You should include a docstring to your public functions indicating what they do and type hints if necessary.
This is a bit complicated
for item in list:
if len(list) == 1:
elif item != list[-1]:
print(item + ', ', end='')
print('and ' + list[-1])
The whole function can be simplified and written properly:
def separate(items: list):
"""Return a string of the words in items separated by a comma."""
if len(items) > 1:
return ', '.join(items[:-1]) + ', and ' + items[-1]
if len(items) == 1:
if not items:
raise ValueError('Empty list')
if __name__ == '__main__': guard at the end of your script which allows it to be imported by other modules without running the whole script.
if __name__ == '__main__':
spam = ['apples', 'bananas', 'tofu', 'cats']