Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 23, 2020 12:53 am GMT

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, lets 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 copylst = [[1, 2, 3], ['a', 'b']]# Standard assignment dup_lst = lst# id object is used to check whether objects are pointing the same referenceprint(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 copyprint(id(lst[0]) == id(dup_lst[0]))  # True#Use copy module's shallow copydup_lst = copy.deepcopy(lst)print(id(lst) == id(dup_lst))   # False#Contents of the object are duplicated with deep copyprint(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 enumclass DaysOfWeek(enum.Enum):    SUNDAY = 1    MONDAY = 2    TUESDAY = 3    WEDNESDAY = 4    THURSDAY = 5    FRIDAY = 6    SATURDAY = 7print(f'Member name of Sunday: {DaysOfWeek.SUNDAY.name}')   # Member name of Sunday: SUNDAYprint(f'Member value of Friday: {DaysOfWeek.FRIDAY.value}')  # Member value of Friday: 6for 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 users default browser as a new window, in a new tab or open it in a specific named browser.

import webbrowserurl = 'https://google.com' # Open URL in user's default browser and bring the browser window to the frontwebbrowser.open(url)# Open URL in a new tab, if a browser window is already openwebbrowser.open_new_tab(url)

pprint

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

import pprintbook = {    '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

Original Link: https://dev.to/dev0928/python-explore-standard-libraries-2g2n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To