Python to Generate Random Jokes with Source Code

In this article, you’ll learn how to generate random jokes using python


  1. Python Basics
  2. pyjokes module

Install Necessary Modules:

Open your  Prompt  and type and run the following command (individually):

pip install pyjokes

pyjokes is a python library for one line jokes for programmers (jokes as a service). You can get a funny one-liners, mostly related to programming. This is a fun python library. It returns a random joke from the given category in the given language.

Supported Languages By Pyjokes

  • English – ‘en’
  • Spanish – ‘es’
  • Italian – ‘it’
  • German – ‘de’
  • Galician – ‘gl’
  • Basque – ‘eu’

Categories Included In Pyjokes

  • For geeky jokes -’neutral’ (It is chosen by default)
  • For Chris Norris Jokes – ‘chuck’.
  • If you want all type of jokes – ‘all’
  • There is one more category known as ‘twister’ which only works for the German Language (‘de’) and mostly includes tongue twister.

Once Installed now we can import it inside our python code.

Python Program to Generate Random Jokes

There are two methods in pyjokes:

Method 1:

  • Function: It only returns one joke at a time. It is generated randomly.
  • Parameters: It has two parameters- language and category.
  • Return Type: It returns string type (str).
Method 2:

  • Function: It returns a list of jokes.
  • Parameters: The parameters are the same as above- language and category.
  • Return Type: It returns a list.

Source Code:

Python Program to Generate Random Jokes

# Import the necessary package
import pyjokes

# Fetch the joke
joke1 = pyjokes.get_joke(language="en", category="all")

# Display the joke

# Different category
joke2 = pyjokes.get_joke(language="en", category="neutral")

# Display the joke

# Fetch multiple jokes
jokes = pyjokes.get_jokes(language="en", category="neutral")

for i in range(5):


How many programmers does it take to kill a cockroach? Two: one holds, the other installs Windows on it. 

What's the object-oriented way to become wealthy? Inheritance. 

1 . Complaining about the lack of smoking shelters, the nicotine addicted Python programmers said there ought to be 'spaces for tabs'. 

2 . Ubuntu users are apt to get this joke. 

3 . Obfuscated Reality Mappers (ORMs) can be useful database tools. 

4 . Asked to explain Unicode during an interview, Geoff went into detail about his final year university project. He was not hired. 

5 . Triumphantly, Beth removed Python 2.7 from her server in 2030. 'Finally!' she said with glee, only to see the announcement for Python 4.4. Code language: PHP (php)

Leave a Comment