Python Pyjokes Module to Add Jokes to Project

Looking to make your project more interesting? Now, you can add jokes in your project with just one line.

You can now get funny one-liner, mostly related to programming by using just importing a library known as pyjokes. You can add these jokes to your projects and you will get a different joke most of the time. 

For different languages, choose the language from the given set of languages and you can also choose the category of joke you want to include in your project. In this article, we will learn how to import this library and include some jokes from it in between our programs.  

Syntax of Pyjokes

For using the pyjokes library, we first need to import it. You can install it using – pip install pyjokes. You can choose from a variety of languages and the type of joke you want. Let us now see what languages and category are available. 

Languages supported by Pyjokes

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

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’). This mostly includes tongue twister.

Pyjokes Module Tutorial Video

Some Methods of pyjokes library

There are two methods in pyjokes- get_joke() and get_jokes().  

  1. get_joke()– It only returns one joke. We get random joke each time. 

Parameters – There are two parameters- language and category. You can choose from the language and category above.  

Return Type It return string type (str). 

  1. get_jokes() – Here, we get a list of jokes.  

Parameter– The parameters are same as above- language and category. 

Return typelist. 

Some Jokes from pyjokes library 

Let us now use the get_joke() method of pyjokes library to retrieve jokes. We will retrieve jokes in multiple languages.

import pyjokes
joke=pyjokes.get_joke(language='en', category= 'neutral')
print(joke)
"What's the object-oriented way to become wealthy?Inheritance."

Let us now change the category to ‘chuck’.

import pyjokes
joke=pyjokes.get_joke(language='en', category= 'chuck')
print(joke)
Chuck Norris can unit test entire applications with a single assertion, 'it works'.

Change the category to ‘all’

import pyjokes
joke1=pyjokes.get_joke(language='en', category= 'all')
print(joke1)
joke2=pyjokes.get_joke(language='en', category= 'all')
print(joke2)
What is Benoit B. Mandelbrot's middle name? Benoit B. Mandelbrot. Chuck Norris can instantiate an abstract class.

Let us now change the category to ‘twister’. For that, we have to change the language to ‘German‘.

import pyjokes
joke=pyjokes.get_joke(language='de', category= 'twister')
print(joke)
Das Schleimschwein schleimt schweinisch im Schleim, im Schleim schleimt schweinisch das Schleimschwein.

I know that most of us do not know the German language, so let me translate it for you into English.

The slime pig slimes piggy in the slime, the slime pig slimes piggy in the slime.

Let us now retrieve more than one joke, we will use the get_jokes() method for that.

import pyjokes
jokes=pyjokes.get_jokes(language='en', category= 'neutral')
print(jokes)

As you can see there are almost 100 jokes. We cannot print each and every one of them here. So let us run a for loop and print 10 jokes from them.

import pyjokes
jokes=pyjokes.get_jokes(language='en', category= 'neutral')
for i in range(10):
    print(i+1,".",jokes[i])
pyjokes
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.
6 . An SQL query goes into a bar, walks up to two tables and asks, 'Can I join you?'
7 . When your hammer is C++, everything begins to look like a thumb.
8 . If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl.
9 . To understand recursion you must first understand recursion.
10 . I suggested holding a 'Python Object Oriented Programming Seminar', but the acronym was unpopular.

We will now make a very small Virtual Assistant program and include jokes in it.

import wikipedia
import webbrowser
import datetime
import pyjokes

query=input("Please enter the command:")
query=query.lower()

if 'wikipedia' in query:
    try:
        print("Searching wikipedia")
        query=query.replace("wikipedia","")
        results=wikipedia.summary(query,sentences=2)
        print("According to wikipedia,")
        print(results)
    except Exception as e:
        print(f"Can not found {query}")
        
elif "open " in query:
        site=re.search(r'open (.*)',query)
        print(f"opening:{site.group(1)}.com")
        webbrowser.open(f"www.{site.group(1)}.com")

elif "joke" in query:
    joke=pyjokes.get_joke(language='en', category= 'all')
    print(joke)
    
elif "time" in query:
    print("The time is")
    current_time=datetime.datetime.now().strftime("%H:%M:%S")
    print(current_time)
Please enter the command:open microsoft
opening:microsoft.com
pyjokes
Please enter the command:what is the time now?
The time is
17:43:57
Please enter the command:Please tell me some jokes
Chuck Norris can install iTunes without installing Quicktime.
Please enter the command:search for Python Programming language on wikipedia 
Searching wikipedia
According to wikipedia, 
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace.

Must Read:

Conclusion

Pyjokes library has some really good jokes and how will you use it, totally depends on you. There is a huge room for improvement too. The number of languages and categories can be increased to make this library perfect.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments