To solidify Python skills, work on projects that incorporate:
# task_manager.py import json from typing import List, Dict class TaskManager: def __init__(self, filename: str = "tasks.json"): self.filename = filename self.tasks = self._load_tasks() def _load_tasks(self) -> List[Dict]: try: with open(self.filename, "r") as f: return json.load(f) except FileNotFoundError: return [] def _save_tasks(self): with open(self.filename, "w") as f: json.dump(self.tasks, f, indent=4) def add_task(self, title: str, priority: int = 1): self.tasks.append({"title": title, "priority": priority, "done": False}) self._save_tasks() def list_tasks(self): for idx, task in enumerate(self.tasks): status = "✓" if task["done"] else " " print(f"{idx + 1}. [{status}] {task['title']} (Priority: {task['priority']})") def mark_done(self, task_id: int): if 0 <= task_id < len(self.tasks): self.tasks[task_id]["done"] = True self._save_tasks() if __name__ == "__main__": manager = TaskManager() manager.add_task("Learn Python", priority=2) manager.add_task("Build a project") manager.list_tasks()
# Initialize repository git init # Check status git status # Add files git add . # Commit changes git commit -m "Add task manager CLI" # Push to remote (GitHub/GitLab) git remote add origin <repo-url> git push -u origin main
git checkout -b feature-auth
)Python's official style guide enforces consistency.
Category | Good ✅ | Bad ❌ |
---|---|---|
Indentation | 4 spaces | Tabs or 2 spaces |
Line Length | ≤ 79 chars | Long unbroken lines |
Imports | Group (stdlib, third-party, local) | Mixed imports |
Naming | snake_case for variables |
camelCase or PascalCase |
# Good PEP 8 compliant code import os from typing import List def calculate_average(numbers: List[float]) -> float: """Compute the arithmetic mean of a list.""" return sum(numbers) / len(numbers)
pip install pycodestyle pycodestyle script.py
# Bad if temperature > 100: alert() # Good BOILING_POINT = 100 if temperature > BOILING_POINT: alert()
MAX_RETRIES = 3 API_TIMEOUT = 30 # seconds
Describe what functions/classes do (following Google style):
def factorial(n: int) -> int: """Calculates the factorial of a non-negative integer. Args: n: Input number (must be ≥ 0). Returns: Factorial of n. Raises: ValueError: If n is negative. """ if n < 0: raise ValueError("n must be non-negative") return 1 if n == 0 else n * factorial(n - 1)
pip install pdoc pdoc --html my_module.py
pylint
for static analysis
pip install pylint pylint script.py
Type | Tools | Use Case |
---|---|---|
Scripts | Cron (Linux), Task Scheduler (Windows) | Automate repetitive tasks |
Web Apps | Flask: Heroku, Gunicorn + Nginx Django: AWS, DigitalOcean |
Full-stack applications |
APIs | FastAPI + Uvicorn | Microservices |
Data Pipelines | Apache Airflow | Scheduled jobs |
myapp/ ├── app.py ├── requirements.txt ├── Procfile └── runtime.txt
from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "Hello, World!" if __name__ == "__main__": app.run()
flask gunicorn
web: gunicorn app:app
heroku login heroku create my-flask-app git push heroku main
Isolate dependencies per project:
python -m venv venv source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows pip install -r requirements.txt
Area | Key Tools/Practices |
---|---|
Project Planning | GitHub Issues, Trello |
Version Control | Git, GitHub/GitLab |
Code Style | PEP 8, pycodestyle |
Documentation | Docstrings, pdoc |
Deployment | Heroku, Docker, AWS |
This guide equips you to develop, refine, and deploy Python projects professionally! 🚀