Kalebu Jordan

Become a Pro Python Developer

Python random module

Random choice is method available on Python Random Module of which you can use to Choose a random element from a non-empty sequence such as list , tuples , set and etc.

Example of usage :

>>> import random
>>> fruits = ['mangoes', 'apples', 'banana', 'passion']
>>> random.choice(fruits)

Sample Project :

For instance let’s try build a hard coded chatbot app that can greet you in different way in a random manner .

import random 

greetings = [
    'what\'s up bro', 
    'hey you good', 

while True:
    you = input('you : ')
    chatbot_message = random.choice(greetings)
    print('chatbot : ', chatbot_message)

Output :

kalebu@kalebu-PC:~$ python3 app.py 
you : greet me 
chatbot :  Ciao
you : again
chatbot :  hey you good
you : again   
chatbot :  Ciao
you : again
chatbot :  hi
you : again
chatbot :  hallo
you : again
chatbot :  what's up bro

Hope you find this post interesting , don’t forget to subscribe to get more tutorials and tips like this.

In case of any suggestion or comment , drop it on the comment box and I will reply to you immediately.

Leave a Reply


Enjoy this blog? Please spread the word :)