Python: Explore Standard Libraries
Tags: python, beginners, codenewbie
Published: 2020-07-23 00:53:35

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.

<h3>copy</h3>
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.
```python
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  
```
<h3>enum</h3>
Enumeration provides a mechanism to define a set of related constants. These constants could be iterated. Enumerations are introduced in Python 3.4.   
```python
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    
```

<h3>webbrowser</h3>
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.
```python
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)
```

<h3>pprint</h3>
As the name implies, the `pprint` module allows for pretty printing of complex data structures for better readability.
```python
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'}
```

<h3>Further Learning</h3>
* <a href="https://pymotw.com/3/index.html">Python Module of the Week</a> 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 <a href="https://docs.python.org/3/library/index.html">Python Documentation</a>