Answer :

please make me a brainlist answer

Answer:

Sure, here are some interesting Python code examples that demonstrate various capabilities of the language:

### 1. Web Scraping with BeautifulSoup

```python

import requests

from bs4 import BeautifulSoup

url = 'https://example.com'

response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')

for link in soup.find_all('a'):

print(link.get('href'))

```

### 2. Basic Data Analysis with Pandas

```python

import pandas as pd

data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],

'Age': [28, 24, 35, 32],

'City': ['New York', 'Paris', 'Berlin', 'London']}

df = pd.DataFrame(data)

print(df.describe())

```

### 3. Creating a Simple REST API with Flask

```python

from flask import Flask, jsonify, request

app = Flask(__name__)

data = [{'id': 1, 'name': 'John'}, {'id': 2, 'name': 'Anna'}]

@app.route('/api/data', methods=['GET'])

def get_data():

return jsonify(data)

@app.route('/api/data', methods=['POST'])

def add_data():

new_data = request.json

data.append(new_data)

return jsonify(new_data), 201

if __name__ == '__main__':

app.run(debug=True)

```

### 4. Data Visualization with Matplotlib

```python

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

y = [2, 3, 5, 7, 11]

plt.plot(x, y, marker='o')

plt.title('Simple Line Plot')

plt.xlabel('x-axis')

plt.ylabel('y-axis')

plt.show()

```

### 5. Machine Learning with Scikit-Learn

```python

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 dataset

iris = load_iris()

X, y = iris.data, iris.target

# Split the dataset

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train the model

clf = RandomForestClassifier(n_estimators=100)

clf.fit(X_train, y_train)

# Predict and evaluate

y_pred = clf.predict(X_test)

print(f'Accuracy: {accuracy_score(y_test, y_pred):.2f}')

```

### 6. Asynchronous Programming with Asyncio

```python

import asyncio

async def say_hello(delay, message):

await asyncio.sleep(delay)

print(message)

async def main():

task1 = asyncio.create_task(say_hello(1, 'Hello'))

task2 = asyncio.create_task(say_hello(2, 'World'))

await task1

await task2

asyncio.run(main())

```

### 7. Reading and Writing Files

```python

# Writing to a file

with open('example.txt', 'w') as file:

file.write('Hello, world!')

# Reading from a file

with open('example.txt', 'r') as file:

content = file.read()

print(content)

```

These examples cover a range of applications from web scraping, data analysis, creating REST APIs, data visualization, machine learning, asynchronous programming, to file handling. Each example demonstrates a different facet of Python's versatility and utility.

Other Questions