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
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
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)
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'}
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)
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
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
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?
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
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?
Extremely inspired! Everything is extremely open and clear illumination of issues. It contains really certainties. Your site is extremely important. Much obliged for sharing. printing priority for postcards
I am continually hunting on the web down articles that can help me. There is clearly a great deal to think about this. I think you made some great focuses in Features too. Continue working, extraordinary job ! hydra onion
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!!!! Laffaz
Extremely inspired! Everything is extremely open and clear illumination of issues. It contains really certainties. Your site is extremely important. Much obliged for sharing. New Year Family Quotes
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!!!! breadmachine brands
This is also a fair post which I really savored the experience of scrutinizing. It isn't every day that I have the probability to see something like this.. dark market url
Some comments may only be visible to logged-in visitors. Sign in to view all comments.