10 Interesting modules in Python to play with

Python is a high level, interpreted and general-purpose dynamic programming language that focuses on code readability. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management. It is one of the world’s most popular, in-demand programming languages. This is for many reasons:

  • It’s easy to learn.
  • It’s super versatile.
  • It has a huge range of modules and libraries.

The fact that it can support the vast majority of third-party modules, it acts as a cherry on the cake. There are some quite interesting modules that are considered worthy of sharing with others. In this article, some of the modules are discussed which will come in handy no matter if you are a beginner or a professional. As most of them are third party modules, they don’t come in-built with Python and are needed to install. Installation of third party modules can be seen here.

NOTE: Some of these code may not work in Python 2. Hence I strongly recommend you to try them in Python 3.



Pyperclip

This module was created to enable cross-platform copy-pasting in Python which was earlier absent. The pyperclip module has copy() and paste() functions that can send text to and receive text from your computer’s clipboard. Sending the output of your program to the clipboard will make it easy to paste it on an email, word processor, or some other software.

Pyperclip does not come with Python. To install it, follow the directions for installing third party modules. After installing the module, enter the following into IDE:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Pyhton program to
# demonstrate pyperclip module
  
  
# This will import pyperclip
import pyperclip
pyperclip.copy("Hello world !")
pyperclip.paste()
  
pyperclip.copy("Isn't pyperclip interesting?")
pyperclip.paste()

chevron_right


Of course, if something outside your program changes the clipboard contents, the paste() function will return it. For example, if this sentence is copied to the clipboard and then paste() is called, the output would look like this:

‘For example, if this sentence is copied to the clipboard and then paste() is called, the output would look like this:’

Emoji

Emojis have become a way to express and to enhance simple boring texts. Now, the same gems can be used in Python programs too. Yes, really! You now have the ultimate power to use emojis in your code. For this, emoji module is needed to be installed.

In terminal. Use:

pip install emoji 

To upgrade to the latest packages of emojis. Here’s how it can be done:

pip install emoji --upgrade
filter_none

edit
close

play_arrow

link
brightness_4
code

from emoji import emojize
print(emojize(":thumbs_up:"))

chevron_right


Use the emoji cheat sheet to find your favorite emoji.
Alternatively, encode() function can be used from emojis module to convert Unicode to emojis:

filter_none

edit
close

play_arrow

link
brightness_4
code

import emojis
emojified = emojis.encode("There is a :snake: in my boot !")
print(emojified)

chevron_right


Hope you try it!

Howdoi

Stuck on a coding problem? Wish to visit StackOverflow without leaving the terminal? With howdoi, you can do it!


Install the howdoi module by:

pip install howdoi

Or install from Python by:

python setup.py install

Ask whatever question you have and it will try it’s best to answer it.

filter_none

edit
close

play_arrow

link
brightness_4
code

howdoi make trees in Python
howdoi commit in git

chevron_right


From now, you don’t need to open those browsers for a quick search and get those hefty amounts of ads and distractions. Just howdoi!

filter_none

edit
close

play_arrow

link
brightness_4
code

howdoi use Howdoi in Python

chevron_right


Wikipedia

As if howdoi wasn’t enough, we can now import the entire Wikipedia! Yes, We can now import Wikipedia in Python using Wikipedia module. Use the incessant flow of knowledge with Python for daily needs.
Install it as:

pip install wikipedia

And use it as:

filter_none

edit
close

play_arrow

link
brightness_4
code

import wikipedia
result = wikipedia.page("GeeksforGeeks")
print(result.summary)

chevron_right


If you wish to get a particular number of sentences from the summary, just pass that as an argument to the summary() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

import wikipedia
print(wikipedia.summary("Debugging", sentences = 2))

chevron_right


New types at runtime

This can create new types in a fully dynamic way. It’s the same as creating a class but something new which you can show to your friends.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to
# create new type object
  
  
# Creates a new type object
NewType = type("NewType", (object, ), {"attr": "hello newtype"})
New = NewType()
  
# Print the type of object
print(type(New))
  
# Print the attribute of object
print(New.attr)

chevron_right


Output:

<class '__main__.NewType'>
hello newtype

The above code is same as:


filter_none

edit
close

play_arrow

link
brightness_4
code

# Creates a class
class NewType:
    attr = "hello newtype"
  
# Initialize an object
New = NewType()
  
# Print the type of object
print(type(New))
  
# Print the attribute of object
print(New.attr)

chevron_right


Output:

<class '__main__.NewType'>
hello newtype

Probably not the best module but still worth a try!

Disassemble Python

Ever wondered what python does under the hood? With the standard library module dis, you can look easily.

filter_none

edit
close

play_arrow

link
brightness_4
code

# This will import
# dis module
import dis
  
  
def test(number):
    return (str(number)+str(number))
  
def newFunc(string):
    print("Hello", string)
  
# This will display the
# disassembly of test():
dis.dis(test)
  
# This will display the
# disassembly of newFunc()
dis.dis(newFunc)

chevron_right


Output:

Result:
  8           0 LOAD_GLOBAL              0 (str)
              3 LOAD_FAST                0 (number)
              6 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
              9 LOAD_GLOBAL              0 (str)
             12 LOAD_FAST                0 (number)
             15 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             18 BINARY_ADD
             19 RETURN_VALUE
  

  3           0 LOAD_GLOBAL              0 (print)
              3 LOAD_CONST               1 ('Hello')
              6 LOAD_FAST                0 (string)
              9 CALL_FUNCTION            2 (2 positional, 0 keyword pair)
             12 POP_TOP
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE

That’s overwhelming and also amazing!

Antigravity

The reason this module is here is because this is quite fun! It’s basically an easter egg in Python 3 which is used in Google App Engines. It was added to Google App Engines just as a medium to amuse the users.

Install it with:

pip install antigravity

And then type this in your IDE to see the magic:

filter_none

edit
close

play_arrow

link
brightness_4
code

import antigravity

chevron_right


This opens up a page in your web browser which contains a comical abstract of Python developed for your delight. Congratulations! You know have the ability to fly or for now the ability to visit this link https://xkcd.com/353/.

sys.exit()

You may have used the sys module before but did you know you could exit your program early using it? We can cause the program to terminate by calling the sys.exit() function. Since this function is in the sys module, firstly, the sys module should be imported. This is not a third party module and comes built-in with Python so there is no need to install it.


filter_none

edit
close

play_arrow

link
brightness_4
code

# This will import 
# sys module
import sys
  
while True:
    print("Type 'exit' to exit")
    response = input()
    if response == "exit":
        print("Exiting the program")
        sys.exit()
    print("You typed", response)

chevron_right


If the input is :

"Geeky"
"GeeksforGeeks"
"exit"

The output will be :

Type 'exit' to exit
You typed Geeky

Type 'exit' to exit
You typed GeeksforGeeks

Type 'exit' to exit
Exiting the program

urllib

Urllib module is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.

Urllib is a package that collects several modules for working with URLs, such as:

  • urllib.request for opening and reading.
  • urllib.parse for parsing URLs
  • urllib.error for the exceptions raised
  • urllib.robotparser for parsing robot.txt files
filter_none

edit
close

play_arrow

link
brightness_4
code

# This will import urlopen
# class from urllib module
from urllib.request import urlopen
  
  
page = urlopen("http://geeksforgeeks.org/")
print(page.headers)

chevron_right


The output would be:

Server: Apache
Strict-Transport-Security: max-age=3600; includeSubDomains
Access-Control-Allow-Credentials: true
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=UTF-8
X-Akamai-Transformed: 9 - 0 pmb=mRUM,3
Vary: Accept-Encoding
Cache-Control: must-revalidate, max-age=3, s-maxage=21600
Date: Fri, 04 Oct 2019 04:57:37 GMT
Transfer-Encoding: chunked
Connection: close
Connection: Transfer-Encoding
Server-Timing: cdn-cache; desc=HIT
Server-Timing: edge; dur=1

You can also see the coding of the website by using read() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

# This will import urlopen
# class from urllib module
  
  
from urllib.request import urlopen
page=urlopen("http://geeksforgeeks.org/")
  
# Fetches the code 
# of the web page
content = page.read()
  
print(content)

chevron_right


Output:

Turtle

Yes, a turtle can be imported. Don’t worry it’s not slow. Turtle is a Python module to draw. It has a huge application and a number of methods which you can learn about in here. But with just a few basics, pretty cool stuff can be done. This module comes built-in with Python so there is no need to install it.

filter_none

edit
close

play_arrow

link
brightness_4
code

# This will import turtle module
import turtle
  
  
myTurtle = turtle.Turtle()
myWin = turtle.Screen()
  
# Turtle to draw a spiral
def drawSpiral(myTurtle, linelen):
    myTurtle.forward(linelen)
    myTurtle.right(90)
    drawSpiral(myTurtle, linelen-10)
  
drawSpiral(myTurtle, 80)
myWin.exitonclick()

chevron_right


Output:
<spiral with turtle



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.