Python program that uses the sorted() function to sort a list of words in alphabetic order:

Created with Sketch.

Python program that uses the sorted() function to sort a list of words in alphabetic order:

def sort_words(words):
    return sorted(words)

# Example usage
words = input("Enter words: ").split()
print("Sorted words:", sort_words(words))

The function sort_words(words) takes a list of words as an argument. The sorted() function is used to sort the list of words in alphabetic order. It returns the sorted list of words.

The example usage prompts the user to enter a list of words separated by spaces, using the input() function, and then splits them into a list using the split() method. Then it calls the sort_words() function to sort the words and finally it prints the sorted list of words.

You could also use sort() method which sorts the list in-place, this means that the original list is modified and sorted.

words = input("Enter words: ").split()
words.sort()
print("Sorted words:", words)

In this case, the original list is modified and sorted, so there is no need to return the sorted list.

Leave a Reply

Your email address will not be published. Required fields are marked *