Python Math: Print a random sample of words from the system dictionary
54. Random Dictionary Words Sample
Write a Python program to print a random sample of words from the system dictionary.
Sample Solution:
Python Code:
import random
with open('/usr/share/dict/words', 'rt') as fh:
words = fh.readlines()
words = [w.rstrip() for w in words]
for w in random.sample(words, 7):
print(w)
Sample Output:
philatelist's thieve sparrows Loretta's hostiles naughty afflicts
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to load the system dictionary, randomly select 5 words using random.sample(), and print them.
- Write a Python script to read words from the system dictionary file, filter out words shorter than 5 letters, and then print a random sample of 7 words.
- Write a Python function to randomly select and print 5 unique words from the system dictionary that start with a consonant.
- Write a Python program to randomly choose a set of words from the dictionary and display them in alphabetical order.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to flip a coin 1000 times and count heads and tails.
Next: Write a Python program to randomly select an item from a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.