w3resource

Python Math: Print a random sample of words from the system dictionary

Python Math: Exercise-54 with Solution

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:

Flowchart: Print a random sample of words from the system dictionary

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.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Lambdas for Sorting:

>>> students = [{'name': 'John', 'score': 98}, {'name': 'Mike', 'score': 94}, {'name': 'Jennifer', 'score': 99}]
>>> sorted(students, key=lambda x: x['score'])
[{'name': 'Mike', 'score': 94}, {'name': 'John', 'score': 98}, {'name': 'Jennifer', 'score': 99}]