DEV Community

Cover image for Python: Explore Standard Libraries
dev0928
dev0928

Posted on • Edited on

Python: Explore Standard Libraries

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

Top comments (45)

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
 
nomankh03866169 profile image
nomankh03866169

Really appreciate this wonderful post that you have provided for us.Great site and a great topic as well i really get amazed to read this. Its really good. Al-Zaytoonah University

Collapse
 
johnkmulle profile image
John K. Muller

A new form of event hosting has made an appearance in a blended form. A blended event is an event held partially in person and partially online. Simply defined, it sounds too similar to a hybrid event. A hybrid event is a combination of a “live” in-person event with a “virtual” online component. What is the difference between the two?

Want to Read More: Hybrid Events Vs. Blended Events: What’s the Difference?

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
 
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

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

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

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

Collapse
 
kelvindj8 profile image
kelvindj

Extremely inspired! Everything is extremely open and clear illumination of issues. It contains really certainties. Your site is extremely important. Much obliged for sharing. quick printing for color copies at 55printing

Collapse
 
kelvindj8 profile image
kelvindj

Wow what a Great Information about World Day its incredibly charming instructive post. an obligation of appreciation is all together for the post. Mittel gegen Pilzinfektion kaufen

Some comments may only be visible to logged-in visitors. Sign in to view all comments.