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:
# Python 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() |
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
from emoji import emojize print (emojize( ":thumbs_up:" )) |
Use the emoji cheat sheet to find your favorite emoji.
Alternatively, encode()
function can be used from emojis module to convert Unicode to emojis:
import emojis emojified = emojis.encode( "There is a :snake: in my boot !" ) print (emojified) |
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.
howdoi make trees in Python howdoi commit in git |
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!
howdoi use Howdoi in Python |
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:
import wikipedia result = wikipedia.page( "Lazyroar" ) print (result.summary) |
If you wish to get a particular number of sentences from the summary, just pass that as an argument to the summary()
function:
import wikipedia print (wikipedia.summary( "Debugging" , sentences = 2 )) |
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.
# 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) |
Output:
<class '__main__.NewType'> hello newtype
The above code is same as:
# 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) |
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.
# 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) |
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:
import antigravity |
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.
# 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) |
If the input is :
"Geeky" "Lazyroar" "exit"
The output will be :
Type 'exit' to exit You typed Geeky Type 'exit' to exit You typed Lazyroar 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
# This will import urlopen # class from urllib module from urllib.request import urlopen print (page.headers) |
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:
# This will import urlopen # class from urllib module from urllib.request import urlopen # Fetches the code # of the web page content = page.read() print (content) |
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.
# 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() |
Output: