DEV Community

Kate Galushko
Kate Galushko

Posted on • Updated on

10 Python Libraries that Every Beginner should Know

As we all know python has numerous in-built libraries that can be used to do different computations without writing the specific code again from the scratch. The new libraries are continuously developed by authors across the globe to enhance python language usage.

10 Main Python Libraries that every coder must know

  • import datetime

This can be considered as one of the main libraries that is often used to get all values related to the current date, time, and year. Let’s us discuss how can we use it

import datetime

var1 = datetime.datetime.now()

print(var1)
2021-06-26 09:08:37.165221
Enter fullscreen mode Exit fullscreen mode

Here is one more example of how to get yesterday date in Python.

  • import os

Another important library used to interact with the Operating System. It is used to find the path of the files or directories or read or write files from the system.

import os

var1 = os.getcwd()

print(var1)
C:\Users\Admin\AppData\Local\Programs\Python\Python37-32
Enter fullscreen mode Exit fullscreen mode
  • import random

It is used to select random character or string from a collection such as a list, tuple, dictionary

import random

listt = ['apple','mango','banana','orange']

print(random.choice(listt))
Enter fullscreen mode Exit fullscreen mode

Here is one more example of how to generate random passwords in python.

  • import requests

This library is used to sent HTTP request externally on a server

import requests

url = "https://www.google.com"

response = requests.get(url)

print(response.status)
Enter fullscreen mode Exit fullscreen mode

Here is how to parse a dynamic web page in Python?

  • import pandas

It is often used to create or read from the data frame or do operations on dataframes

import pandas as pd
Enter fullscreen mode Exit fullscreen mode

Here you can find how to install pandas in python?

  • import BeautifulSoup

Beautiful Soup is a library that makes it easy to scrape information from web pages. It sits atop an HTML or XML parser, providing Pythonic idioms for iterating, searching, and modifying the parse tree.

from bs4 import BeautifulSoup

url = "https://www.google.com"

response= requests.get(url)

soup = BeautifulSoup(response.txt)

print(soup)
Enter fullscreen mode Exit fullscreen mode
  • import JSON

Python has a built-in package called json, which can be used to work with JSON data

import json

# some JSON:
x =  '{ "name":"John", "age":30, "city":"New York"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"])
Enter fullscreen mode Exit fullscreen mode

Here you can find one more example of how to convert string to JSON in python.

  • import pytest

The pytest the framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries

  • import CSV
import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
Enter fullscreen mode Exit fullscreen mode

Here is one more example of how to read a CSV files in Python.

  • import math

This module provides access to the mathematical functions defined by the C standard.

#Import math Library
import math

#Return factorial of a number
print(math.factorial(9))
print(math.factorial(6))
print(math.factorial(12))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)