Update tech_docs/python/Python_programming.md
This commit is contained in:
@@ -1,3 +1,189 @@
|
||||
Understanding where data comes from and how to handle it in real-world applications is crucial. In real-world scenarios, data often comes from a variety of sources, such as databases, APIs, files, and user inputs. Here's a breakdown of common data sources and how to handle them in Python:
|
||||
|
||||
### Common Data Sources
|
||||
|
||||
1. **Databases**
|
||||
2. **APIs**
|
||||
3. **Files (CSV, JSON, XML, etc.)**
|
||||
4. **User Input**
|
||||
5. **Web Scraping**
|
||||
6. **Sensor Data (IoT)**
|
||||
7. **In-memory Data Structures**
|
||||
|
||||
### 1. Databases
|
||||
|
||||
Databases are a primary source of data for many applications. Python supports interaction with databases like MySQL, PostgreSQL, SQLite, and NoSQL databases like MongoDB.
|
||||
|
||||
#### Example: Fetching Data from a SQL Database
|
||||
|
||||
```python
|
||||
import sqlite3
|
||||
|
||||
# Connect to the database
|
||||
conn = sqlite3.connect('example.db')
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Execute a query
|
||||
cursor.execute('SELECT * FROM books')
|
||||
|
||||
# Fetch all results
|
||||
books = cursor.fetchall()
|
||||
|
||||
# Close the connection
|
||||
conn.close()
|
||||
|
||||
# Convert data to a pandas DataFrame
|
||||
import pandas as pd
|
||||
df = pd.DataFrame(books, columns=['title', 'author', 'isbn'])
|
||||
print(df)
|
||||
```
|
||||
|
||||
### 2. APIs
|
||||
|
||||
APIs (Application Programming Interfaces) allow applications to fetch data from web services. RESTful APIs and GraphQL are common examples.
|
||||
|
||||
#### Example: Fetching Data from a RESTful API
|
||||
|
||||
```python
|
||||
import requests
|
||||
|
||||
# Send a GET request to the API
|
||||
response = requests.get('https://api.example.com/data')
|
||||
|
||||
# Parse the JSON response
|
||||
data = response.json()
|
||||
|
||||
# Convert data to a pandas DataFrame
|
||||
import pandas as pd
|
||||
df = pd.DataFrame(data)
|
||||
print(df)
|
||||
```
|
||||
|
||||
### 3. Files (CSV, JSON, XML, etc.)
|
||||
|
||||
Files are a common way to store and transfer data. Python's standard library and third-party libraries like pandas provide tools to read and write files.
|
||||
|
||||
#### Example: Reading a CSV File
|
||||
|
||||
```python
|
||||
import pandas as pd
|
||||
|
||||
# Read CSV file into DataFrame
|
||||
df = pd.read_csv('data.csv')
|
||||
print(df)
|
||||
```
|
||||
|
||||
#### Example: Reading a JSON File
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
# Load JSON data from a file
|
||||
with open('data.json') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Convert data to a pandas DataFrame
|
||||
df = pd.DataFrame(data)
|
||||
print(df)
|
||||
```
|
||||
|
||||
### 4. User Input
|
||||
|
||||
In interactive applications, data often comes from user input via command line, forms, or graphical user interfaces.
|
||||
|
||||
#### Example: Getting User Input
|
||||
|
||||
```python
|
||||
# Get user input from the command line
|
||||
name = input("Enter your name: ")
|
||||
age = int(input("Enter your age: "))
|
||||
|
||||
# Process the input
|
||||
print(f"Name: {name}, Age: {age}")
|
||||
```
|
||||
|
||||
### 5. Web Scraping
|
||||
|
||||
Web scraping involves extracting data from websites. Libraries like BeautifulSoup and Scrapy are commonly used for web scraping in Python.
|
||||
|
||||
#### Example: Web Scraping with BeautifulSoup
|
||||
|
||||
```python
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
# Fetch the webpage
|
||||
response = requests.get('https://example.com')
|
||||
soup = BeautifulSoup(response.content, 'html.parser')
|
||||
|
||||
# Extract data
|
||||
titles = soup.find_all('h1')
|
||||
for title in titles:
|
||||
print(title.get_text())
|
||||
```
|
||||
|
||||
### 6. Sensor Data (IoT)
|
||||
|
||||
In IoT applications, data often comes from sensors. This data can be streamed in real-time or batch processed.
|
||||
|
||||
#### Example: Reading Sensor Data
|
||||
|
||||
```python
|
||||
# This is a hypothetical example, as sensor libraries and methods vary widely
|
||||
import random
|
||||
|
||||
# Simulate reading data from a sensor
|
||||
sensor_data = [random.random() for _ in range(10)]
|
||||
print(sensor_data)
|
||||
```
|
||||
|
||||
### 7. In-memory Data Structures
|
||||
|
||||
Data can also be generated or maintained within the application using in-memory data structures like lists, dictionaries, and sets.
|
||||
|
||||
#### Example: Generating Data in-memory
|
||||
|
||||
```python
|
||||
# Generate a list of numbers
|
||||
numbers = [x for x in range(10)]
|
||||
print(numbers)
|
||||
|
||||
# Generate a dictionary of squares
|
||||
squares = {x: x**2 for x in range(10)}
|
||||
print(squares)
|
||||
```
|
||||
|
||||
### Integrating Data from Various Sources
|
||||
|
||||
Often, real-world applications need to integrate data from multiple sources. Here’s an example that combines data from a CSV file and an API.
|
||||
|
||||
#### Example: Integrating CSV and API Data
|
||||
|
||||
```python
|
||||
import pandas as pd
|
||||
import requests
|
||||
|
||||
# Read CSV file into DataFrame
|
||||
df_csv = pd.read_csv('data.csv')
|
||||
|
||||
# Fetch data from API
|
||||
response = requests.get('https://api.example.com/data')
|
||||
data_api = response.json()
|
||||
|
||||
# Convert API data to DataFrame
|
||||
df_api = pd.DataFrame(data_api)
|
||||
|
||||
# Merge data on a common column
|
||||
merged_df = pd.merge(df_csv, df_api, on='common_column')
|
||||
print(merged_df)
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
Understanding where data comes from and how to handle it is crucial for developing real-world applications. Python's extensive library ecosystem makes it easy to fetch, process, and integrate data from various sources, whether it's from a database, an API, a file, user input, or web scraping. By mastering these techniques, you can build robust and versatile data-driven applications.
|
||||
|
||||
---
|
||||
|
||||
Object-oriented programming (OOP) is a paradigm that organizes software design around data, or objects, rather than functions and logic. The key concepts of OOP are encapsulation, abstraction, inheritance, and polymorphism. Understanding how to relate OOP to real-world problems involves thinking about the entities involved as objects with properties (attributes) and behaviors (methods). Here’s a guide to help you understand and apply OOP principles effectively.
|
||||
|
||||
### Key Concepts of OOP
|
||||
|
||||
Reference in New Issue
Block a user