Python libraries and their examples
- User
Python libraries and their examples
- Assistant
Python libraries and their examples
Sure, here are examples of some popular Python libraries along with usage examples:
A fundamental package for numerical computing in Python, providing support for arrays.
Installation:
pip install numpy
Example:
import numpy as np
# Create an array
a = np.array([1, 2, 3, 4, 5])
print("Array:", a)
# Perform operations
print("Mean:", np.mean(a))
print("Sum:", np.sum(a))
A powerful data manipulation and analysis library, particularly useful for data in tabular form.
Installation:
pip install pandas
Example:
import pandas as pd
# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [24, 27, 22]}
df = pd.DataFrame(data)
print(df)
# Perform operations
print("Average Age:", df['Age'].mean())
A plotting library for creating static, animated, and interactive visualizations in Python.
Installation:
pip install matplotlib
Example:
import matplotlib.pyplot as plt
# Data for plotting
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create a plot
plt.plot(x, y)
plt.title("Simple Plot")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
A machine learning library that provides simple and efficient tools for data mining and data analysis.
Installation:
pip install scikit-learn
Example:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Load iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train a classifier
clf = RandomForestClassifier()
clf.fit(X_train, y_train)
# Make predictions
y_pred = clf.predict(X_test)
# Evaluate accuracy
print("Accuracy:", accuracy_score(y_test, y_pred))
An open-source library for numerical computation and large-scale machine learning.
Installation:
pip install tensorflow
Example:
import tensorflow as tf
# Create a constant tensor
hello = tf.constant('Hello, TensorFlow!')
# Create a TensorFlow session
tf.print(hello)
A lightweight WSGI web application framework.
Installation:
pip install Flask
Example:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
# Run the server
if __name__ == '__main__':
app.run(debug=True)
An HTTP library for making requests to web services.
Installation:
pip install requests
Example:
import requests
# Make a GET request
response = requests.get('https://api.github.com')
# Print response content
print(response.text)
A library for web scraping purposes to pull the data out of HTML and XML files.
Installation:
pip install beautifulsoup4
Example:
from bs4 import BeautifulSoup
import requests
# Fetch content from URL
url = 'http://example.com'
response = requests.get(url)
# Parse HTML content
soup = BeautifulSoup(response.content, 'html.parser')
# Extract title
title = soup.title.text
print("Title:", title)
These are just a few of the many libraries available in Python. Each library has extensive documentation that can provide you with more in-depth details and additional examples.