loading...
Cover image for Python: Explore Standard Libraries

Python: Explore Standard Libraries

dev0928 profile image dev0928 Updated on ・3 min read

Did you know that there are more than 200 standard library modules that come with Python? As standard libraries are already part of Python installation, they could be used right away without the need for explicit module installation using a package manager.

Getting comfortable with these standard libraries and knowing their capabilities would definitely help speed up the development process. In this article, let’s explore four of the standard libraries along with great resources for learning the rest of them.

copy

Like many other programming languages, objects are passed by reference in Python. For example consider the assignment statement obj2 = obj1. After this assignment, all of the changes made to the object obj2 will be reflected in obj1 as they are pointing to the same object. If we want to preserve the original contents of obj1, we could use the copy module and clone the object.

import copy


lst = [[1, 2, 3], ['a', 'b']]


# Standard assignment 
dup_lst = lst
# id object is used to check whether objects are pointing the same reference
print(id(lst) == id(dup_lst))  # True

#Use copy module's shallow copy 
dup_lst = copy.copy(lst)

print(id(lst) == id(dup_lst))   # False
#Contents of the object are not duplicated with shallow copy
print(id(lst[0]) == id(dup_lst[0]))  # True

#Use copy module's shallow copy
dup_lst = copy.deepcopy(lst)

print(id(lst) == id(dup_lst))   # False
#Contents of the object are duplicated with deep copy
print(id(lst[0]) == id(dup_lst[0]))  # False  
Enter fullscreen mode Exit fullscreen mode

enum

Enumeration provides a mechanism to define a set of related constants. These constants could be iterated. Enumerations are introduced in Python 3.4.

import enum


class DaysOfWeek(enum.Enum):
    SUNDAY = 1
    MONDAY = 2
    TUESDAY = 3
    WEDNESDAY = 4
    THURSDAY = 5
    FRIDAY = 6
    SATURDAY = 7



print(f'Member name of Sunday: {DaysOfWeek.SUNDAY.name}')   # Member name of Sunday: SUNDAY
print(f'Member value of Friday: {DaysOfWeek.FRIDAY.value}')  # Member value of Friday: 6

for day in DaysOfWeek:
    print(day)

# DaysOfWeek.SUNDAY
# DaysOfWeek.MONDAY
# DaysOfWeek.TUESDAY
# DaysOfWeek.WEDNESDAY
# DaysOfWeek.THURSDAY
# DaysOfWeek.FRIDAY
# DaysOfWeek.SATURDAY    
Enter fullscreen mode Exit fullscreen mode

webbrowser

Web browser module provides a mechanism to open URLs from Python. There are several options available to open the supplied URL ranging from opening in user’s default browser as a new window, in a new tab or open it in a specific named browser.

import webbrowser

url = 'https://google.com' 

# Open URL in user's default browser and bring the browser window to the front
webbrowser.open(url)

# Open URL in a new tab, if a browser window is already open
webbrowser.open_new_tab(url)
Enter fullscreen mode Exit fullscreen mode

pprint

As the name implies, the pprint module allows for pretty printing of complex data structures for better readability.

import pprint


book = {
    'title' : "Automate the Boring Stuff with Python: Practical Programming for Total Beginners",
    'author' : "Al Sweigart",
    'pub_year' : 2015
}

print(book)  

#{'title': 'Automate the Boring Stuff with Python: Practical Programming for Total Beginners', 'author': 'Al Sweigart', 'pub_year': 2015}


pp = pprint.PrettyPrinter(width=100, compact=True)
pp.pprint(book)  

# Pretty print arranged keys in alphabetical order here display width is controlled to 100 characters

# {'author': 'Al Sweigart',
#  'pub_year': 2015,
#  'title': 'Automate the Boring Stuff with Python: Practical Programming for Total Beginners'}
Enter fullscreen mode Exit fullscreen mode

Further Learning

  • Python Module of the Week is a great resource to learn more about Python standard libraries. What I like about this resource is that it explains standard modules through examples.
  • Of course, the best reference for getting up to date information on standard libraries is Python Documentation

Discussion

pic
Editor guide
Collapse
kelvindj8 profile image
kelvindj

This is a fabulous post I seen by virtue of offer it. It is genuinely what I expected to see look for in future you will continue subsequent to sharing such an extraordinary post. Criação de app

Collapse
kelvindj8 profile image
kelvindj

We specialise in security cameras installation, providing a full range of security cameras installation in Sydney Australia. From cost-effective solutions to advanced technology, we can customise security cameras to suit your needs. Whether you need security for your home or business, you can rely on us for our extensive experience in offering quality products and services in the security industry. camera installation Sydney

Collapse
kelvindj8 profile image
kelvindj

Three are normally modest Ralph Lauren accessible available to be purchased every last time you wish to purchase. Top 5 Amazon Listing Services Companies in India

Collapse
kelvindj8 profile image
kelvindj

กำจัดปลวกโคราช ( korat termite ) เป็นบริษัทกำจัดปลวกโคราช ฉีดปลวกโคราชราคาถูก เน้นความคุ้มค่า เพื่อลดค่าใช้จ่ายของลูกค้า. มีขั้นตอนการฉีดปลวกเหมือนกับบริษัทใหญ่ราคาแพง. กำจัดปลวกโคราช

Collapse
pubgapk2 profile image
pubgapk

This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post. Locksmith Brick

Collapse
pubgapk2 profile image
pubgapk

I think I have never seen such blogs ever before that has complete things with all details which I want. So kindly update this ever for us. Locksmith Tampa

Collapse
pubgapk2 profile image
pubgapk

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work. Locksmith Rio Rancho

Collapse
pubgapk2 profile image
pubgapk

I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot! Quick AndoverLocksmith

Collapse
pubgapk2 profile image
pubgapk

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! Complete Locksmith Ann Arbor

Collapse
nomankh03866169 profile image
nomankh03866169

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks record

Collapse
kelvindj8 profile image
kelvindj

Uncommon tips and clear. This will be to a great degree supportive for me when I get a chance to start my blog. Houston Embroidery Service

Collapse
kelvindj8 profile image
kelvindj

I require you to thank for your period of this radiant read!!! I definately value every last bit of it and I have you bookmarked to take a gander at new stuff of your blog an outright need read blog!!!! 13377X Search Engine

Collapse
pubgapk2 profile image
pubgapk

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! Locksmith Queen Creek

Collapse
farhank46520467 profile image
Farhan Khatri

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. How to Dunk a Basketball

Collapse
pubgapk2 profile image
pubgapk

Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on. Maricopa Locksmith

Collapse
kelvindj8 profile image
kelvindj

I require you to thank for your period of this radiant read!!! I definately value every last bit of it and I have you bookmarked to take a gander at new stuff of your blog an outright need read blog!!!! How to Download Voot App for PC

Collapse
pubgapk2 profile image
pubgapk

The blog and data is excellent and informative as well Brockton Locksmith

Collapse
pubgapk2 profile image
pubgapk

This post is very simple to read and appreciate without leaving any details out. Great work! ASAP Smyrna Locksmith

Collapse
pubgapk2 profile image
pubgapk

Well we really like to visit this site, many useful information we can get here. Locksmith Denver

Collapse
brendagarrett profile image
BrendaGarrett

I am also in the same row and I am also looking for some professional writer to write my assignment for me cheap work online. I urge you to keep sharing more. Thanks.