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,
You can simply install it using pip with the following command:
pip install pyjokes
Now to use it, we need to import the installed library in our python Script using the following command:
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().
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).
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.
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:
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:
|neutral||Neutral geeky jokes|
|all||All types of joke|
Below are some examples to understand better:
Example 1: Using get_joke() to generate a single joke
Example 2: Using get_jokes() to generate a list of jokes
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.