Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Python Script to create random jokes using pyjokes

  • Last Updated : 02 Dec, 2020

Python supports creation of random jokes using one of its libraries. Let us explore it a little more, Pyjokes is a python library that is used to create one-line jokes for programmers. Informally, it can also be referred as a fun python library which is pretty simple to use. Let us see how you can actually use it to perform the required task,

Installation

You can simply install it using pip with the following command:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

pip install pyjokes

Usage

Now to use it, we need to import the installed library in our python Script using the following command:



import pyjokes

Before, moving further towards our python script, it is necessary to get familiar with the two functions of Pyjokes library, namely get_joke() and get_jokes(). 

Functions

  • get_joke() 

Syntax:

get_joke(language,category)

As the name suggests, this function is used to actually return a single joke from a certain category and in a particular language, (Categories and languages will be introduced later in this article). 

  • get_jokes()

Syntax:

get_jokes(language,category)

It is similar to the get_joke() function, the only difference lies in the fact that instead of returning a single joke, it returns a list of random jokes from a certain category and in a particular language.

Parameters

Language and category are the two parameters of get_joke() and get_jokes() functions. 

Language specifies in which language you want the joke(s) to be displayed. By default, it is set to “en” that returns jokes in English. All other possible values for language parameter are described below:

LanguageValues
enEnglish
deGerman
esSpanish
itItalian
glGalician
eu Basque

Similarly, the category parameter specifies the category in which you want joke(s) to be displayed. By default, it is set to “neutral”. All other possible values for the category parameter are described below:



CategoryValues
neutralNeutral geeky jokes
twisterTongue-twister
allAll types of joke

Below are some examples to understand better:

Example 1: Using get_joke() to generate a single joke

Python3




# importing installed library
import pyjokes
  
# using get_joke() to generate a single joke
#language is english
#category is neutral
My_joke = pyjokes.get_joke(language="en", category="neutral")
  
print(My_joke)

Output:

Example 2: Using get_jokes() to generate a list of jokes

Python3




import pyjokes
  
# using get_jokes() to generate a whole list of jokes
# language is german
# category is twister
list_of_jokes = pyjokes.get_jokes(language="de", category="twister")
  
# traversing through the generated list of jokes
# Range of i may change, depending on the number of jokes
# you want to display
for i in range(0, 4):
    print(list_of_jokes[i], sep='\n')

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :