Wednesday, 19 March 2025

Introduction to Built in Package in Python

 Python provides a vast collection of built-in packages (also known as standard libraries) that simplify coding and enhance functionality. These built-in packages help developers perform tasks without needing external dependencies, making Python a powerful and versatile programming language.

What Are Built-in Packages?

Built-in packages are pre-installed modules in Python that provide various functionalities, such as file handling, mathematical operations, system interactions, and web handling. You can import and use these modules without installing them separately.

Commonly Used Built-in Packages

Here are some of the most commonly used built-in packages in Python:

1. math - Mathematical Operations

The math module provides mathematical functions like square root, trigonometric functions, logarithms, and more.

import math

 

print(math.sqrt(25))  # Output: 5.0

print(math.pi)        # Output: 3.141592653589793

print(math.factorial(5))  # Output: 120

2. random - Generating Random Numbers

The random module is used for generating random numbers, selecting random elements, and shuffling sequences.

import random

 

print(random.randint(1, 10))  # Random number between 1 and 10

print(random.choice(["apple", "banana", "cherry"]))  # Random selection from a list

3. datetime - Working with Dates and Time

The datetime module provides functions to handle dates and time-related tasks.

import datetime

 

current_time = datetime.datetime.now()

print("Current Time:", current_time)

4. os - Interacting with the Operating System

The os module provides functionalities to interact with the operating system, such as file handling and directory management.

import os

 

print(os.getcwd())  # Get the current working directory

os.mkdir("new_folder")  # Create a new directory

5. sys - System-specific Functions

The sys module provides access to system-specific parameters and functions.

import sys

 

print(sys.version)  # Prints Python version

print(sys.platform)  # Prints the operating system platform

6. json - Handling JSON Data

The json module helps in encoding and decoding JSON data.

import json

 

data = {"name": "John", "age": 25}

json_data = json.dumps(data)

print(json_data)  # Convert dictionary to JSON string

7. re - Regular Expressions

The re module is used for pattern matching and working with regular expressions.

import re

text = "Hello, my number is 123-456-7890"

pattern = r"\d{3}-\d{3}-\d{4}"

match = re.search(pattern, text)

if match:

    print("Phone number found:", match.group())

8. collections - Advanced Data Structures

The collections module provides specialized container data types like Counter, defaultdict, and deque.

from collections import Counter

 

words = ["apple", "banana", "apple", "orange", "banana", "apple"]

word_count = Counter(words)

print(word_count)  # Output: Counter({'apple': 3, 'banana': 2, 'orange': 1})

Conclusion

Python's built-in packages make programming easier by providing ready-to-use functionalities. By mastering these modules, you can simplify your code and focus more on problem-solving rather than implementing low-level functionalities.

 

No comments:

Post a Comment

Desktop Virtualisation

Desktop Virtualization ( DV ) Desktop Virtualization ( DV ) is a technique that creates an illusion of a desktop provided to the user. It d...