<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abhijeet kumar</title>
    <description>The latest articles on Forem by Abhijeet kumar (@cleverzone).</description>
    <link>https://forem.com/cleverzone</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F754560%2F8979a2a6-0cd4-4585-9293-e5fed5a5335f.jpg</url>
      <title>Forem: Abhijeet kumar</title>
      <link>https://forem.com/cleverzone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cleverzone"/>
    <language>en</language>
    <item>
      <title>A comprehensive guide to create first FastAPI Rest API</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Thu, 06 Mar 2025 17:49:04 +0000</pubDate>
      <link>https://forem.com/cleverzone/a-comprehensive-guide-to-create-first-fastapi-rest-api-576c</link>
      <guid>https://forem.com/cleverzone/a-comprehensive-guide-to-create-first-fastapi-rest-api-576c</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3aq17z3h02yfb71j8iaz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3aq17z3h02yfb71j8iaz.png" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog post, we’ll explore how to create a simplified FastAPI project using SQLAlchemy ORM and an SQLite database to manage a basic newsletter subscription system, including subscribing, unsubscribing, and retrieving the subscriber list.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Create Virtual Enviromint and FastAPI project —&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After creating our project directory we need to be create &lt;a href="https://cleverzone.medium.com/unravelling-virtual-environment-of-python-1fb1d13a877d" rel="noopener noreferrer"&gt;virtual environment&lt;/a&gt; then we will install required library by executing below command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install fastapi[standard] uvicorn sqlalchemy aiomysql pydantic_settings 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;fastapi[standard]&lt;/strong&gt; — Installs FastAPI along with extra dependencies for full functionality (like pydantic, httpx).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;uvicorn&lt;/strong&gt; — ASGI server for running FastAPI applications efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;sqlalchemy&lt;/strong&gt; — ORM for interacting with relational databases using Python.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;aiomysql&lt;/strong&gt; — Asynchronous MySQL driver for SQLAlchemy to enable async database operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;pydantic_settings&lt;/strong&gt; — Manages application settings using .env files and environment variables with Pydantic, then generate a requirements.txt file which hold all dependencies details.&lt;/p&gt;

&lt;p&gt;pip freeze &amp;gt; requirements.txt&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once installation is completed then we have to create a main.py which is responsive for manage our FastAPI project, over all structure will look like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_fastapi_project/
├── main.py
├── database/
│   ├── config.py
│   ├── database.py
├── newsletter/models.py
│   ├── models.py
│   ├── routes.py
│   ├── schemas.py
│   ├── utils.py
├── .env
├── requirements.txt
├── venv/ (virtual environment)
└── vercel.json
├── .gitignore
└── README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let break down each file importance in details.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;main.py —&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In FastAPI, main.py is the entry point of the application where the FastAPI instance is created, routes (endpoints) are defined, and the app is run using a server like Uvicorn.&lt;/p&gt;

&lt;p&gt;It serves as the starting file for handling API requests and responses.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from database.database import get_db , engine , Base
import os
import uvicorn
from newsletter.routes import router as newsletter_route
app = FastAPI()
Base.metadata.create_all(bind=engine)
app.include_router(newsletter_route)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        reload_dirs=[os.path.dirname(os.path.abspath(__file__))],
        reload_excludes=[
            "*/.git/*",
            "*/__pycache__/*",
            "*.pyc",
            "*/.pytest_cache/*",
            "*/.vscode/*",
            "*/.idea/*"
        ],
        reload_delay=1,
        reload_includes=["*.py", "*.html", "*.css", "*.js"]
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Break down —&lt;/p&gt;

&lt;p&gt;This FastAPI application is structured to handle newsletters and is configured with middleware, database connections, and automatic reloading. Below is a breakdown of each component.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Imports —&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;FastAPI— The core framework for building APIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CORSMiddleware— Middleware to handle Cross-Origin Resource Sharing (CORS).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;get_db, engine, Base— Database-related imports from a custom module.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;os— Used for handling file paths.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;uvicorn— ASGI server to run the FastAPI application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;router as newsletter_route— Imports newsletter-related routes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Application Initialization —&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;app = FastAPI()— Creates an instance of the FastAPI application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Database Setup&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base.metadata.create_all(bind=engine)— Ensures all database tables are created using SQLAlchemy's ORM.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Including Routers —&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;app.include_router(newsletter_route) — Registers the newsletter routes with the FastAPI app.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. CORS Middleware Configuration —&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;allow_origins=["*"]— Allows requests from any origin.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;allow_credentials=True— Allows sending credentials (cookies, authorization headers).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;allow_methods=["*"] — Allows all HTTP methods.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;allow_headers=["*"]— Allows all headers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. Application Execution —&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;main&lt;/strong&gt; block ensures that the application runs when executed directly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;uvicorn.run("main:app", ...) starts the FastAPI server with&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;host="0.0.0.0": Listens on all network interfaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;port=8000— Runs on port 8000.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;reload=True — Enables automatic reload on code changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;reload_dirs — Specifies which directories to watch for changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;reload_excludes— Excludes unnecessary files and directories from reloading.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;reload_includes — Specifies file types to trigger reloads.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. config.py —&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    DATABASE_URL: str 
    class Config:
        env_file = ".env"

settings = Settings()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This code defines a &lt;strong&gt;Pydantic Settings&lt;/strong&gt; class to manage environment variables. It inherits from BaseSettings, allowing automatic loading of configuration values from an .env file or system environment variables.&lt;/p&gt;

&lt;p&gt;The Config subclass specifies the .env file as the source. When an instance of Settings is created, it fetches the environment variables, enabling secure and flexible configuration management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. database.py —&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from .config import settings

SQLALCHEMY_DATABASE_URL = settings.DATABASE_URL 

#ensure .env file with DATABASE_URL="database_name" or put static database name 

engine = create_engine(
    SQLALCHEMY_DATABASE_URL, 
    connect_args={"check_same_thread": False}  
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This code sets up &lt;strong&gt;SQLAlchemy&lt;/strong&gt; with a database connection using environment-based configuration.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Import necessary SQLAlchemy components&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;create_engine— Establishes a connection to the database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;declarative_base— Creates a base class for defining ORM models.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;sessionmaker — Manages database sessions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Load database URL dynamically&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It imports settings from config to fetch DATABASE_URL from environment variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Create the database engine&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) initializes a connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The connect_args parameter ensures SQLite allows multiple threads to interact with the database.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Session Management&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) transactions variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Base Class for ORM Models&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base = declarative_base() is used to define database models.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. Dependency for Database Session&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;get_db() provides a session instance for database operations using a generator.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It ensures that the session is properly closed after use.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. models.py —&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy import Column, Integer, String, Boolean, DateTime
from database.database import Base
from datetime import datetime
class NewsletterSubscription(Base):
    __tablename__ = "newsletter_subscriptions"
    id = Column(Integer, primary_key=True, index=True)
    email = Column(String(255) , unique=True , nullable=True)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.utcnow)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is Newsletter Table model with id , email , is_active , create_at columns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. routes.py —&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from database.database import get_db
from .schemas import NewsletterSubscriptionBase , NewsletterSubscriptionCreate , NewsletterSubscriptionResponse
from .utils import create_subscription , get_all_subscriptions , delete_subscription
router = APIRouter(prefix="/newsletter", tags=["newsletter"])

@router.post("/subscribe", response_model=NewsletterSubscriptionResponse)
def subscribe(
    subscription: NewsletterSubscriptionCreate,
    db: Session = Depends(get_db)
):

    return create_subscription(db, subscription)

@router.get("/subscriptions", response_model=list[NewsletterSubscriptionResponse])
def get_subscriptions(
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db)
):
    return get_all_subscriptions(db, skip, limit)

@router.delete("/unsubscribe/{email}")
def unsubscribe(email: str, db: Session = Depends(get_db)):
    if delete_subscription(db, email):
        return {"message": "Successfully unsubscribed"}
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Subscription not found"
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This file routes.py perfoming actions while its using utils function by passing parameter in functions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;subscribe (POST /subscribe)&lt;/strong&gt; — Adds a new subscription to the newsletter route using utils function for subscribe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;get_subscriptions (GET /subscriptions)&lt;/strong&gt; — Retrieves a list of all newsletter subscriptions with optional pagination (skip and limit) route using utils function for subscribe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;unsubscribe (DELETE /unsubscribe/{email})&lt;/strong&gt; — Removes a subscription using an email; returns an error if the email is not found route using utils function for subscribe.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. schemas.py —&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic import BaseModel, EmailStr
from datetime import datetime
from typing import Optional

class NewsletterSubscriptionBase(BaseModel):
    email: EmailStr

class NewsletterSubscriptionCreate(NewsletterSubscriptionBase):
    pass

class NewsletterSubscriptionResponse(NewsletterSubscriptionBase):
    id: int
    is_active: bool
    created_at: datetime

    class Config:
        from_attributes = True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This setup ensures &lt;strong&gt;data validation, serialization, and deserialization&lt;/strong&gt;, making it useful in APIs (e.g., FastAPI) for handling &lt;strong&gt;newsletter subscriptions&lt;/strong&gt; efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let break down —&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;BaseModel — The base class for creating Pydantic models.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;EmailStr — A built-in type from Pydantic that validates email formats.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;datetime — Used for handling timestamps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optional — Allows fields to be optional.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Define a Base Model (NewsletterSubscriptionBase)&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Contains a single field, email, which must be a valid email address (EmailStr).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This acts as a &lt;strong&gt;base class&lt;/strong&gt; for other models to inherit.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Define a Create Model (NewsletterSubscriptionCreate)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Inherits from NewsletterSubscriptionBase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No additional fields, meaning it has the same structure as the base model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Used specifically for handling user input when subscribing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Define a Response Model (NewsletterSubscriptionResponse)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Extends NewsletterSubscriptionBase to include additional fields:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;id — A unique identifier for the subscription.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;is_active — A boolean flag indicating if the subscription is active.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;created_at— A timestamp showing when the subscription was created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Config class includes:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;from_attributes = True— Allows automatic conversion from ORM models to Pydantic models.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;7. utils.py —&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy.orm import Session
from . import models, schemas
from fastapi import HTTPException, status

def create_subscription(db: Session, subscription: schemas.NewsletterSubscriptionCreate):
    db_subscription = models.NewsletterSubscription(
        email=subscription.email
    )
    try:
        db.add(db_subscription)
        db.commit()
        db.refresh(db_subscription)
        return db_subscription
    except Exception as e:
        db.rollback()
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Email already subscribed"
        )

def get_subscription(db: Session, email: str):
    return db.query(models.NewsletterSubscription).filter(
        models.NewsletterSubscription.email == email
    ).first()

def get_all_subscriptions(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.NewsletterSubscription).offset(skip).limit(limit).all()

def delete_subscription(db: Session, email: str):
    subscription = get_subscription(db, email)
    if subscription:
        db.delete(subscription)
        db.commit()
        return True
    return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;These utility functions are responsible for handling subscription creation, retrieval by email, listing, and deletion.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;create_subscription&lt;/strong&gt; — Adds a new email subscription to the database. If the email is already subscribed, it rolls back the transaction and raises an error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;get_subscription&lt;/strong&gt; — Retrieves a subscription by email.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;get_all_subscriptions&lt;/strong&gt; — Fetches all subscriptions with optional pagination (skip and limit).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;delete_subscription&lt;/strong&gt; — Deletes a subscription by email if it exists; returns False if not found.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion: **FastAPI makes building APIs **fast, efficient, and developer-friendly&lt;/strong&gt; with automatic validation, type hints, and asynchronous support.&lt;/p&gt;

&lt;p&gt;By following this guide, you’ve set up your first FastAPI application, defined endpoints, and tested them using the interactive Swagger UI.&lt;/p&gt;

&lt;p&gt;With its simplicity and performance, FastAPI is a great choice for modern web APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;GitHub repo : &lt;a href="https://github.com/abhijitkumarIN/deployment" rel="noopener noreferrer"&gt;https://github.com/abhijitkumarIN/deployment&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Live URL : &lt;a href="https://deployment-vund.onrender.com/docs" rel="noopener noreferrer"&gt;https://deployment-vund.onrender.com/docs&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your appreciation or feedback on this insight would be greatly valued, whether it’s a small follow up or leaving a note.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And connect with me on&lt;/strong&gt;* &lt;a href="https://x.com/abhijeet__in" rel="noopener noreferrer"&gt;X handler*** &lt;/a&gt;for the tech insights.&lt;br&gt;
Looking forward to engaging with you!**&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>restapi</category>
      <category>python</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>How to Deploy FastAPI to Render</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Thu, 06 Mar 2025 17:48:03 +0000</pubDate>
      <link>https://forem.com/cleverzone/how-to-deploy-fastapi-to-render-2njm</link>
      <guid>https://forem.com/cleverzone/how-to-deploy-fastapi-to-render-2njm</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F86sbbwmjg57raeh7j6ih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F86sbbwmjg57raeh7j6ih.png" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog, we’ll guide you through deploying a FastAPI application to &lt;strong&gt;Render&lt;/strong&gt;, a cloud platform designed for easy and scalable web hosting.&lt;/p&gt;

&lt;p&gt;**Render — &lt;br&gt;
**Render provides an easy and seamless platform for deploying web applications and services.&lt;/p&gt;

&lt;p&gt;It’s similar to other cloud platforms, but it simplifies the deployment process and makes it more accessible to developers.&lt;/p&gt;

&lt;p&gt;Whether you’re deploying a web service, a background worker, or a database, Render offers an intuitive user interface and automatic scaling.&lt;/p&gt;

&lt;p&gt;Before we start, make sure you have the following —&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;FastAPI application — *&lt;em&gt;If you don’t have an existing &lt;a href="https://fastapi.tiangolo.com/" rel="noopener noreferrer"&gt;FastAPI &lt;/a&gt;project, or if you’d like, you can match the configuration of the *&lt;/em&gt;&lt;em&gt;main.py&lt;/em&gt; *&lt;em&gt;and *&lt;/em&gt;*config.py&lt;/strong&gt;* global variable configuration using &lt;a href="https://pypi.org/project/pydantic-settings/" rel="noopener noreferrer"&gt;***pydantic_settings &lt;/a&gt;***setting with the our existing &lt;a href="https://cleverzone.medium.com/a-comprehensive-guide-to-create-first-fastapi-rest-api-72e31b63f5e8" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;FastAPI app&lt;/em&gt;&lt;/strong&gt; &lt;/a&gt;provided in the blog tutorial.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GitHub repository&lt;/strong&gt; for your FastAPI app (Render will deploy your app from your repository).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once project configuration is okay as given , push the all code into your &lt;a href="https://github.com/" rel="noopener noreferrer"&gt;GitHub account&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Render account&lt;/strong&gt; — If you don’t have one, create a free account on &lt;a href="https://render.com" rel="noopener noreferrer"&gt;Render&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now we ready to go —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fes97dl7w6ikxitkprst7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fes97dl7w6ikxitkprst7.png" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creation , we have to visit your render account &lt;a href="https://dashboard.render.com/" rel="noopener noreferrer"&gt;dashboard &lt;/a&gt;and have to click into Deploy a web service.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpc8vz2ul23qzzpnwwbz3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpc8vz2ul23qzzpnwwbz3.png" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then you can select you Github repository and provide project name.&lt;/p&gt;

&lt;p&gt;Here our project name is deployment and can provide language &amp;amp; its version however its detect automatically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2yj0ixuagl7w20x2uuk2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2yj0ixuagl7w20x2uuk2.png" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We would be also provide few command which install required package for FastAPI app.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uvicorn main:app --host 0.0.0.0 --port 10000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command uvicorn main:app --host 0.0.0.0 --port 10000 does the following —&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;uvicorn&lt;/strong&gt;— Starts the Uvicorn ASGI server to run your FastAPI app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;main:app&lt;/strong&gt; — Specifies the FastAPI app to run. main is the filename (main.py), and app is the FastAPI instance inside that file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;--host 0.0.0.0&lt;/strong&gt;— Makes the app accessible externally by binding it to all available network interfaces.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;--port 10000&lt;/strong&gt; — Sets the port number to 10000 for the app to listen on.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmyx8tooz70ca9cpccyw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmyx8tooz70ca9cpccyw1.png" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’re just starting out, you can choose the free deployment option, which provides 1 CPU initially. However, it later scales down to 0.5 CPU, which may cause slower API responses and reduced performance.&lt;/p&gt;

&lt;p&gt;Here, we are selecting the free version and providing all environment variables that we include in the .env file. Currently, we only have one environment variable, DATABASE_URL, which we've configured.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzml7fg7mozav0nuyy3x9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzml7fg7mozav0nuyy3x9.png" width="800" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have done almost everything just once it all configuration is done you can verified all once again and click on deploy web service.&lt;/p&gt;

&lt;p&gt;Finally, our FastAPI app has been successfully deployed. After deployment, a live instance link will be generated at the top, allowing you to check your app’s live instance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F16kvtptz8dlxuqinai5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F16kvtptz8dlxuqinai5t.png" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;**Conclusion — **Deploying a &lt;a href="https://fastapi.tiangolo.com/" rel="noopener noreferrer"&gt;FastAPI &lt;/a&gt;application to &lt;a href="https://render.com/" rel="noopener noreferrer"&gt;Render &lt;/a&gt;is a straightforward and efficient process. With just a few configuration steps, you can easily go from development to a live, scalable web service.&lt;/p&gt;

&lt;p&gt;Render’s seamless deployment system ensures that your application is up and running in no time, allowing you to focus on building and improving your project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;GitHub repo : &lt;a href="https://github.com/abhijitkumarIN/deployment" rel="noopener noreferrer"&gt;https://github.com/abhijitkumarIN/deployment&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Live URL : &lt;a href="https://deployment-vund.onrender.com/docs" rel="noopener noreferrer"&gt;https://deployment-vund.onrender.com/docs&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your appreciation or feedback on this insight would be greatly valued, whether it’s a small follow up or leaving a note.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And connect with me on&lt;/strong&gt;* &lt;a href="https://x.com/abhijeet__in" rel="noopener noreferrer"&gt;X handler*** &lt;/a&gt;for the tech insights.&lt;br&gt;
Looking forward to engaging with you!**&lt;/p&gt;

</description>
      <category>programming</category>
      <category>fastapi</category>
      <category>softwareengineering</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>A beginner Guide to Upload Images in a REST API with Django</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Thu, 06 Mar 2025 17:46:28 +0000</pubDate>
      <link>https://forem.com/cleverzone/a-beginner-guide-to-upload-images-in-a-rest-api-with-django-opf</link>
      <guid>https://forem.com/cleverzone/a-beginner-guide-to-upload-images-in-a-rest-api-with-django-opf</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsegmmjk3l3yp4xcbanlm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsegmmjk3l3yp4xcbanlm.png" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In modern development, handling image uploads via REST APIs is a common requirement.&lt;/p&gt;

&lt;p&gt;Whether you’re building a social media platform, an e-commerce site, or a blogging platform, enabling users to upload images is crucial for any type of applications.&lt;/p&gt;

&lt;p&gt;In this tutorial, we’ll go through the process of implementing image uploading functionality in a Django REST API.&lt;/p&gt;

&lt;p&gt;We’ll cover everything from setting up Django, creating models, serializers, views, to handling image uploads.&lt;/p&gt;

&lt;p&gt;Our mainly concern about to explore the how to setup the Django project to upload images via rest API.&lt;/p&gt;

&lt;p&gt;If you need to explore basic Django project setup you can follow this article d&lt;a href="https://medium.com/@cleverzone/a-comprehensive-guide-to-setting-up-a-django-project-49b9d264a78a" rel="noopener noreferrer"&gt;jango project setup&lt;/a&gt; in which we have covered basic setup for Django project.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Now let ‘s see each step with example.&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We have created our Django project named &lt;strong&gt;image_upload&lt;/strong&gt; and have set up a &lt;a href="https://cleverzone.medium.com/unravelling-virtual-environment-of-python-1fb1d13a877d" rel="noopener noreferrer"&gt;virtual environment&lt;/a&gt;, along with a*** requirements.txt file*** additionally, we have activated our &lt;a href="https://cleverzone.medium.com/unravelling-virtual-environment-of-python-1fb1d13a877d" rel="noopener noreferrer"&gt;virtual environment &lt;/a&gt;named &lt;strong&gt;&lt;em&gt;myenv&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we have created out app module with image name and which we have added into our &lt;strong&gt;INSTALLED_APP&lt;/strong&gt; in our project’s &lt;strong&gt;setting.py&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;py manage.py startapp image&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6hxml22yuroy84ebg8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa6hxml22yuroy84ebg8o.png" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Next, we installed Pillow and Django Rest Framework we need to add rest_framework to the INSTALLED_APPS list in the settings.py file&lt;/p&gt;

&lt;p&gt;pip install pillow djangorestframework&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And it summarized final terminal commands in which we have performed its all &lt;em&gt;( djangorestframework installation missing).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlt9yhfj9k3j99ininzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlt9yhfj9k3j99ininzq.png" width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In this step we have to work on our already create image app module for respectively on &lt;strong&gt;models.py, serializers.py , urls.py , admin.py&lt;/strong&gt; and &lt;strong&gt;views.py&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let ‘s break down of each of these files.&lt;/p&gt;

&lt;p&gt;In image app module ‘s &lt;strong&gt;models.py&lt;/strong&gt; we have added two field with caption of image as CharField and another with ImageField having directory name as mentioned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5aayw8ilrhhs0w6fs8m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5aayw8ilrhhs0w6fs8m.png" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Likewise in image app module ‘s serializers.py we have include all field like id , image with caption however we can added it with (“&lt;strong&gt;all&lt;/strong&gt;”) as commented snippet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjk6uhaikiw8akazdq0n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjk6uhaikiw8akazdq0n.png" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also add the out &lt;strong&gt;&lt;em&gt;UploadImageModel&lt;/em&gt;&lt;/strong&gt; in out image app module ‘s &lt;strong&gt;admin.py&lt;/strong&gt; so that it will reflect in Django admin user interface for test.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbbqobm6697yjkrz4e6o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkbbqobm6697yjkrz4e6o.png" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is our &lt;strong&gt;image&lt;/strong&gt; app module ‘s &lt;strong&gt;views.py&lt;/strong&gt; in which have done two method for post and get respectively using APIView.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcwux42w1jv6o7jz5etf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcwux42w1jv6o7jz5etf.png" width="800" height="1142"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have created urls.py for in image app module for and import the view and gave the api name &lt;strong&gt;&lt;em&gt;media_upload&lt;/em&gt;&lt;/strong&gt; that have post and get both methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztoyj8z7gtsx0hjnqxb3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fztoyj8z7gtsx0hjnqxb3.png" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In our last step we have to configure directory to store the image and make it accessible for get its images.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Django, &lt;strong&gt;MEDIA_URL&lt;/strong&gt; and &lt;strong&gt;MEDIA_ROOT&lt;/strong&gt; are settings used to handle media files, such as images, videos, or documents uploaded by users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MEDIA_URL&lt;/strong&gt;: &lt;strong&gt;MEDIA_URL&lt;/strong&gt; is a setting that defines the base URL for media files. When a user uploads a file, Django serves it through this URL. In your example, &lt;strong&gt;MEDIA_URL&lt;/strong&gt; &lt;strong&gt;= ‘/media/’&lt;/strong&gt; sets the base URL for media files to be accessed at **/media/ **on your website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MEDIA_ROOT&lt;/strong&gt;: &lt;strong&gt;MEDIA_ROOT&lt;/strong&gt; is the filesystem path where uploaded media files are stored. It’s the location on your server’s disk where Django will save uploaded files. &lt;br&gt;
In your example &lt;strong&gt;MEDIA_ROOT = os.path.join(BASE_DIR, ‘media/’)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;BASE_DIR refers to the directory where your Django project is located, and &lt;strong&gt;‘media/’&lt;/strong&gt; indicates that media files will be stored in a directory named &lt;strong&gt;‘media’&lt;/strong&gt; within your project directory.&lt;/p&gt;

&lt;p&gt;So, when a user uploads a file, Django will save it to the &lt;strong&gt;MEDIA_ROOT&lt;/strong&gt; directory, and it will be accessible via the URL specified in &lt;strong&gt;MEDIA_URL&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, if a user uploads an image named &lt;strong&gt;example.jpg&lt;/strong&gt;, it will be saved to *&lt;em&gt;‘media/example.jpg’ **on your server, and it will be accessible via the URL *&lt;/em&gt;‘/media/example.jpg’ **on your website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqdvu0363xur7s0a0w4bj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqdvu0363xur7s0a0w4bj.png" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the project is running in debug mode (DEBUG is set to True in settings.py), this line dynamically adds URL patterns for serving media files.&lt;/p&gt;

&lt;p&gt;Django will serve media files directly from MEDIA_ROOT when the server is running in debug mode.&lt;/p&gt;

&lt;p&gt;This means that during development, you can access uploaded media files directly through their URLs without having to configure a separate web server to serve them.&lt;/p&gt;

&lt;p&gt;However, this line should not be used in production. In production, you typically serve media files using a dedicated web server &lt;strong&gt;(like Nginx or Apache) *&lt;em&gt;or a cloud storage service *&lt;/em&gt;(like Amazon S3 or Google Cloud Storage)&lt;/strong&gt; for better performance and security.&lt;/p&gt;

&lt;p&gt;Instead, in production, you would configure your web server to serve media files directly from the specified location &lt;strong&gt;(MEDIA_ROOT)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1u2uhfi12ddes93fb1k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1u2uhfi12ddes93fb1k.png" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, we have hit these two commands respectively for our database table field creation.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py makemigrations #  to create migration files

python manage.py migrate   #   to apply those migrations to your database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is our respectively post and get sample response via postman .&lt;/p&gt;

&lt;p&gt;POST —&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwz0z128bymvmqna1l89d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwz0z128bymvmqna1l89d.png" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GET —&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4o76tvniw9mu81au69ia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4o76tvniw9mu81au69ia.png" width="800" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; — Creating a Django REST API for image uploads has been a learning experience. We’ve covered setup, making things efficient, keeping it safe, and testing, giving developers a clear path.&lt;/p&gt;

&lt;p&gt;Whether you’re starting out or seasoned, this guide helps you make reliable image upload APIs with Django projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your appreciation or feedback on this insight would be greatly valued, whether it’s a small follow up or leaving a note.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And connect with me on&lt;/strong&gt;* &lt;a href="https://x.com/abhijeet__in" rel="noopener noreferrer"&gt;X handler*** &lt;/a&gt;for the tech insights.&lt;br&gt;
Looking forward to engaging with you!**&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Deploy FastAPI to Vercel</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Thu, 06 Mar 2025 17:44:49 +0000</pubDate>
      <link>https://forem.com/cleverzone/how-to-deploy-fastapi-to-vercel-5b64</link>
      <guid>https://forem.com/cleverzone/how-to-deploy-fastapi-to-vercel-5b64</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7dts9qepsweqkz0s2n2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7dts9qepsweqkz0s2n2.png" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog, we’ll guide you through deploying a FastAPI application to &lt;strong&gt;Vercel&lt;/strong&gt;, a cloud platform designed for fast and serverless web hosting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Vercel —
&lt;/h3&gt;

&lt;p&gt;Vercel provides an easy and seamless platform for deploying web applications and APIs with minimal configuration.&lt;/p&gt;

&lt;p&gt;It’s optimized for &lt;strong&gt;serverless functions&lt;/strong&gt;, making it an excellent choice for hosting FastAPI without worrying about infrastructure.&lt;/p&gt;

&lt;p&gt;Whether you’re deploying a simple API or a complex backend service, Vercel offers &lt;strong&gt;automatic scaling, global CDN, and quick deployments&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before we start, make sure you have the following —&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;FastAPI application — *&lt;em&gt;If you don’t have an existing &lt;a href="https://fastapi.tiangolo.com/" rel="noopener noreferrer"&gt;FastAPI &lt;/a&gt;project, or if you’d like, you can match the configuration of the *&lt;/em&gt;&lt;em&gt;main.py&lt;/em&gt; *&lt;em&gt;and *&lt;/em&gt;*config.py&lt;/strong&gt;* global variable configuration meanly &lt;a href="https://pypi.org/project/pydantic-settings/" rel="noopener noreferrer"&gt;***pydantic_settings &lt;/a&gt;&lt;strong&gt;&lt;em&gt;with the our existing &lt;a href="https://cleverzone.medium.com/a-comprehensive-guide-to-create-first-fastapi-rest-api-72e31b63f5e8" rel="noopener noreferrer"&gt;***FastAPI &lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;in the blog tutorial.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GitHub repository&lt;/strong&gt; for your FastAPI app (Vercel will deploy your app from your repository).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once project configuration is okay as given, now we need to add a file &lt;em&gt;**vercel.json *&lt;/em&gt;*in root file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "builds": [
      {
        "src": "main.py",
        "use": "@vercel/python"
      }
    ],
    "routes": [
      {
        "src": "/(.*)",
        "dest": "main.py"
      }
    ]

  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Push the all code into your &lt;a href="https://github.com/" rel="noopener noreferrer"&gt;GitHub account&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Vercel account&lt;/strong&gt; — If you don’t have one, create a free account on &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now we ready to go —&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Configured .env variable &amp;amp; Repository selection for deployment —&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once got is pushed into GitHub now we have to visit at vercel account and updated the .env variable as we have a env variables DATABASE_URL.&lt;/p&gt;

&lt;p&gt;Now we have to select the our repository for deployment as given below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2w0kssfpkmfvexx6ktc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2w0kssfpkmfvexx6ktc.png" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Deployment —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Our chosen repository is &lt;strong&gt;vercel-deployment&lt;/strong&gt;. Now, click the &lt;strong&gt;Deploy&lt;/strong&gt; button to initiate the deployment process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc21decq2kb1nl5pjv4e9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc21decq2kb1nl5pjv4e9.png" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Access the deployment dashboard.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally we have deployed the FastAPI into vercel after click on below continue to dashboard another dashboard deployment open.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxcqq9bzfgwnuogqjpgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxcqq9bzfgwnuogqjpgy.png" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can access the deployed instance into deployed dashboard as attached below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnhu1l9ph7b1dfyi2hynk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnhu1l9ph7b1dfyi2hynk.png" width="800" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/abhijitkumarIN/vercel-deployment" rel="noopener noreferrer"&gt;***GitHub — https://github.com/abhijitkumarIN/vercel-deployment&lt;/a&gt;***&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Vercel deployed link : &lt;a href="https://vercel-deployment-blond.vercel.app/docs" rel="noopener noreferrer"&gt;https://vercel-deployment-blond.vercel.app/docs&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion—&lt;/strong&gt; Deploying FastAPI on Vercel is a quick and efficient way to make your application live.&lt;/p&gt;

&lt;p&gt;With just a few steps — selecting a repository, configuring settings, and clicking &lt;strong&gt;Deploy&lt;/strong&gt; — your FastAPI app can be up and running with ease.&lt;/p&gt;

&lt;p&gt;Vercel’s seamless deployment process, automatic scaling, and built-in CI/CD make it a great choice for hosting lightweight APIs.&lt;/p&gt;

&lt;p&gt;Now, your FastAPI project is ready to serve users globally with minimal effort.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your appreciation or feedback on this insight would be greatly valued, whether it’s a small follow up or leaving a note.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And connect with me on&lt;/strong&gt;* &lt;a href="https://x.com/abhijeet__in" rel="noopener noreferrer"&gt;X handler*** &lt;/a&gt;for the tech insights.&lt;br&gt;
Looking forward to engaging with you!**&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>softwareengineering</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Level Up your python skills with ultimate trick tips</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Sun, 17 Mar 2024 06:29:03 +0000</pubDate>
      <link>https://forem.com/cleverzone/level-up-your-python-skills-with-ultimate-trick-tips-31aa</link>
      <guid>https://forem.com/cleverzone/level-up-your-python-skills-with-ultimate-trick-tips-31aa</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2054%2F1%2APybDJe1gyA9lZ_Ayrrk3Hg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2054%2F1%2APybDJe1gyA9lZ_Ayrrk3Hg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python, known for its versatility and readability, offers a plethora of tips and tricks to enhance your coding experience.&lt;/p&gt;

&lt;p&gt;In this blog post, we’ll explore 25+ invaluable tips and tricks that can significantly boost your coding efficiency and empower you to write more concise and powerful code.&lt;/p&gt;

&lt;p&gt;Each tip is accompanied by illustrative examples, and some include external or internal links to enhance comprehension and facilitate application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lets explore its all spectrum —&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Harness *args and **kwargs for Flexible Function Arguments —&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2ATDfmujyWnUhunuLPdWV3CA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2ATDfmujyWnUhunuLPdWV3CA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Use &lt;a href="https://www.w3schools.com/python/ref_func_zip.asp" rel="noopener noreferrer"&gt;zip( )&lt;/a&gt; for Parallel Iteration —&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AB_4uBTBnyxTLR6A0I2ahBw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AB_4uBTBnyxTLR6A0I2ahBw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Unpacking Sequences for Cleaner Assignments —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ab_fzfpb71aE9T9gOgvEWQQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ab_fzfpb71aE9T9gOgvEWQQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. List &lt;a href="https://medium.com/@cleverzone/harnessing-the-power-of-for-loop-comprehension-in-python-de7f7ca27614" rel="noopener noreferrer"&gt;Comprehensions&lt;/a&gt; for Concise Iteration —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AOMRP6iNSwNj8h8x7RulBXQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AOMRP6iNSwNj8h8x7RulBXQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Swap Values with One Line —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2ArORsHGBzlLGC659tQMOdbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2ArORsHGBzlLGC659tQMOdbg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Multiple Assignment with a Single Value —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AL8TZ8C1gwofCptWDl7Clxw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AL8TZ8C1gwofCptWDl7Clxw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Use &lt;a href="https://www.programiz.com/python-programming/methods/built-in/enumerate" rel="noopener noreferrer"&gt;enumerate()&lt;/a&gt; for Index-Value Pairs —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ax-SBQRT6p5Fzf5-F9PWiJA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ax-SBQRT6p5Fzf5-F9PWiJA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Use &lt;a href="https://www.freecodecamp.org/news/python-any-and-all-functions-explained-with-examples/" rel="noopener noreferrer"&gt;any()&lt;/a&gt; and &lt;a href="https://www.freecodecamp.org/news/python-any-and-all-functions-explained-with-examples/" rel="noopener noreferrer"&gt;all()&lt;/a&gt; for Logical Checks —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AVnd9etV1GwSVCXq0oVe7_Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AVnd9etV1GwSVCXq0oVe7_Q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. &lt;a href="https://docs.python.org/3/library/collections.html#collections.Counter" rel="noopener noreferrer"&gt;collections.Counter &lt;/a&gt;for Counting Items —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A7T3qayCTQKjvy0ByKkjeNg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A7T3qayCTQKjvy0ByKkjeNg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Simultaneous Filtering and Transforming with Generator Expressions —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AhgaxKQLaUapxJTDx1xjk2Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AhgaxKQLaUapxJTDx1xjk2Q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Traditional Method —&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First, a list &lt;strong&gt;&lt;em&gt;squares&lt;/em&gt;&lt;/strong&gt; is generated using a list comprehension, where each element is the square of the corresponding number in the range from 0 to 9.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, another list comprehension is used to filter out the even squares from the &lt;em&gt;**squares *&lt;/em&gt;&lt;em&gt;list, and the result is stored in the list *&lt;/em&gt;&lt;em&gt;even_squares&lt;/em&gt;** .&lt;br&gt;
The &lt;strong&gt;&lt;em&gt;even_squares&lt;/em&gt;&lt;/strong&gt; list will contain &lt;strong&gt;[0, 4, 16, 36, 64]&lt;/strong&gt; .&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using Generator Expressions —&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This code snippet uses a generator expression to achieve the same task.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the generator expression, another generator expression*** (i ** 2 for i in range(10))*** generates the squares of numbers from &lt;strong&gt;&lt;em&gt;0 to 9&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, each square is filtered for evenness, and only even squares are yielded.&lt;br&gt;
The &lt;em&gt;**even_squares *&lt;/em&gt;&lt;em&gt;generator will produce the even squares *&lt;/em&gt;[0, 4, 16, 36, 64]** when iterated over.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;11. &lt;a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" rel="noopener noreferrer"&gt;collections.defaultdict &lt;/a&gt;for Default Values in Dictionaries —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AX0P-0RUO28Y3pp25-6HBKg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AX0P-0RUO28Y3pp25-6HBKg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;12. &lt;a href="https://docs.python.org/3/library/collections.html#collections.namedtuple" rel="noopener noreferrer"&gt;collections.namedtuple&lt;/a&gt; for Readable Data Structures —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ak775zUU93gVymg4L06JR3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ak775zUU93gVymg4L06JR3w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13. Use with Statement for Automatic Resource Management —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AltFox1NyMXvp1WpC2IOsxA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AltFox1NyMXvp1WpC2IOsxA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The with statement in Python is used to ensure proper handling of resources. In this case, it ensures that the file is properly opened and closed, even if an error occurs during the process. It automatically takes care of closing the file once the block of code inside it is executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14. Chained Comparison for Multiple Conditions —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ash_2NSjmMK29mX-iS3enZA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Ash_2NSjmMK29mX-iS3enZA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;15. The += Operator for In-Place Addition —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A1saAwgfiDWMUUlQEKfuEWA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A1saAwgfiDWMUUlQEKfuEWA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;16. Using &lt;strong&gt;doc&lt;/strong&gt; for Accessing Docstrings —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AVk8UsnCIUtc1sZHN3fWUow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AVk8UsnCIUtc1sZHN3fWUow.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;17. * Unpacking in Function Calls —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A4Dv8xIq-PFGzBSeSA7TC4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A4Dv8xIq-PFGzBSeSA7TC4w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;18. Use &lt;a href="https://www.w3schools.com/python/ref_func_sorted.asp" rel="noopener noreferrer"&gt;sorted()&lt;/a&gt; with Custom Key Functions —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Au_QzOnTyXtyyY2eB_ktytw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2Au_QzOnTyXtyyY2eB_ktytw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;19. The Walrus Operator := for Assignment Expressions (Python 3.8+) —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AzA-c4023UFm5lawQ-uxllQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AzA-c4023UFm5lawQ-uxllQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;20. F-strings for Elegant String Formatting (Python 3.6+) —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A5uRJfGBaA7G6ASLu2u4MWQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A5uRJfGBaA7G6ASLu2u4MWQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;21. String &lt;a href="https://www.w3schools.com/python/python_string_formatting.asp" rel="noopener noreferrer"&gt;format( ) &lt;/a&gt;—&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A9Dm7uK3KvIbRt1hQZUialQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A9Dm7uK3KvIbRt1hQZUialQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;22.&lt;a href="https://docs.python.org/3/library/functools.html" rel="noopener noreferrer"&gt; functools.partial &lt;/a&gt;for Partial Function Application —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AxwVWH23D-0eMYIo6_LH9jA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AxwVWH23D-0eMYIo6_LH9jA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;23. Using &lt;a href="https://www.w3schools.com/python/python_try_except.asp" rel="noopener noreferrer"&gt;try-except-else&lt;/a&gt; Blocks for Clean Error Handling —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A2UqIjpAJlqnwB1tWc1WGpg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A2UqIjpAJlqnwB1tWc1WGpg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;24. Use is and is not for Identity Comparison —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AeKKv3A868OSrDd4TyUtVVg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AeKKv3A868OSrDd4TyUtVVg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25. List &lt;a href="https://www.w3schools.com/python/python_strings_slicing.asp" rel="noopener noreferrer"&gt;Slicing&lt;/a&gt; for Elegant Sub-setting —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A5VOwDjLl4REktCp79Nxccw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A5VOwDjLl4REktCp79Nxccw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;26. &lt;a href="https://www.w3schools.com/python/ref_func_reversed.asp" rel="noopener noreferrer"&gt;reversed()&lt;/a&gt; for Reversing Iterables —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AQl2QxaL2VOeyYE8scgFTRg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2AQl2QxaL2VOeyYE8scgFTRg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;27. Use &lt;a href="https://www.w3schools.com/python/python_sets.asp" rel="noopener noreferrer"&gt;set&lt;/a&gt; to Remove Duplicates —&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A4CucksUavESkl4tkM9NfTA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2940%2F1%2A4CucksUavESkl4tkM9NfTA.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; : By incorporating these tips and tricks into your python development workflow, you can enhance your productivity and write cleaner, more maintainable code.&lt;/p&gt;

&lt;p&gt;However, it’s important to note that these are just a few examples, and python offers a vast of features and techniques to explore.&lt;/p&gt;

&lt;p&gt;Keep learning and experimenting to become a more proficient python developer .&lt;/p&gt;

</description>
      <category>python</category>
      <category>softwareengineering</category>
      <category>softwaredevelopment</category>
      <category>coding</category>
    </item>
    <item>
      <title>Child ‘s function calling from parent component | ReactJS | NextJS</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Sat, 09 Mar 2024 21:49:12 +0000</pubDate>
      <link>https://forem.com/cleverzone/child-s-function-calling-from-parent-component-reactjs-nextjs-23b1</link>
      <guid>https://forem.com/cleverzone/child-s-function-calling-from-parent-component-reactjs-nextjs-23b1</guid>
      <description>&lt;p&gt;Hello dev 👋 ,&lt;/p&gt;

&lt;p&gt;In this blog, we’ll explore to call the child component function from the parent component.&lt;/p&gt;

&lt;p&gt;but before moving further, make sure that you’re using React version 17 or above.&lt;/p&gt;

&lt;p&gt;So let’s get started.&lt;/p&gt;

&lt;p&gt;We are assuming that we have a Parent component ApplyFilter.jsx and another child component Home.jsx.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//ApplyFilter.jsx (Child component)

import React, { forwardRef, useImperativeHandle } from "react";

const ApplyFilter = forwardRef((props, ref) =&amp;gt; {
  useImperativeHandle(ref, () =&amp;gt; ({
    qtyIncrement: () =&amp;gt; qtyIncrement(),
    qtyDecrement: () =&amp;gt; qtyDecrement(),
    deleteHandler: () =&amp;gt;deleteHandler(),
  }));

  const qtyIncrement = () =&amp;gt; {
    console.log("qtyIncrement");
    // ...
  };
  const qtyDecrement = () =&amp;gt; {
    console.log("qtyDecrement");
    // ...
  };
  const deleteHandler = () =&amp;gt; {
    console.log("deleteHandler");
    // ...
  };
  return 
(&amp;lt;div&amp;gt;
&amp;lt;h2&amp;gt;{props?.title}&amp;lt;/h2&amp;gt;
&amp;lt;p&amp;gt;Other DOM&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;);
});

ApplyFilter.displayName = "ApplyFilter";
export default ApplyFilter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let ‘s break down .&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Imports:&lt;br&gt;
import React, { &lt;a href="https://react.dev/reference/react/forwardRef#forwardref"&gt;forwardRef&lt;/a&gt;, &lt;a href="https://react.dev/reference/react/useImperativeHandle"&gt;useImperativeHandle&lt;/a&gt; } from "react";&lt;br&gt;
This imports React and necessary hooks (forwardRef and useImperativeHandle) from the "react" library.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function Component Definition:&lt;br&gt;
const ApplyFilter = forwardRef((props, ref) =&amp;gt; {...});&lt;br&gt;
Defines a functional component named ApplyFilter.&lt;br&gt;
forwardRef() is used to forward the ref object from the parent component to this child component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useImperativeHandle Hook:&lt;br&gt;
useImperativeHandle(ref, () =&amp;gt; ({...}));&lt;br&gt;
This hook allows the child component to expose certain functions to the parent component through the forwarded ref.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the callback, an object is returned containing functions (qtyIncrement, qtyDecrement, deleteHandler) which will be accessible from the parent component when using the ref.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function Declarations:&lt;br&gt;
const qtyIncrement = () =&amp;gt; {...};, const qtyDecrement = () =&amp;gt; {...};, const deleteHandler = () =&amp;gt; {...};&lt;br&gt;
These are the functions declared within the component. which we have to call from parent which we have bind in a object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Return Statement:&lt;br&gt;
return (&lt;/p&gt;...);
This returns JSX elements representing the component’s UI.
In this case, it returns a  element containing an  element with the title prop passed from the parent component, and a &lt;p&gt; element with some placeholder text.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ApplyFilter.displayName:&lt;br&gt;
ApplyFilter.displayName = "ApplyFilter";This sets the displayName property of the component, which can be useful for debugging or displaying in React DevTools.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Export Statement:&lt;br&gt;
export default ApplyFilter;&lt;br&gt;
This exports the ApplyFilter component as the default export from this module, making it accessible for import in other files.&lt;/p&gt;&lt;/li&gt;


&lt;p&gt;&lt;strong&gt;Now&lt;/strong&gt; have to import this child component &lt;strong&gt;ApplyFilter.jsx&lt;/strong&gt; into Parent component and our parent component is &lt;strong&gt;Home.jsx&lt;/strong&gt; where we ‘ll be access to call the &lt;strong&gt;ApplyFilter&lt;/strong&gt; component ‘s functions.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// home.jsx (Parent component)

import React ,{useRef} from "react"
import ApplyFilter from './ApplyFilter'

export default function Home() {
  const childRef = useRef()
  return (
    &amp;lt;main&amp;gt;
      &amp;lt;ApplyFilter title={'Filter'} ref={childRef} /&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; {
        childRef.current.qtyIncrement()
      }}&amp;gt;+&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; {
        childRef.current.qtyDecrement()
      }}&amp;gt;-&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; {
        childRef.current.deleteHandler()
      }}&amp;gt;
        RESET
      &amp;lt;/button&amp;gt;
    &amp;lt;/main&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see, there are three functions: qtyIncrement, qtyDecrement, and deleteHandler, which we are accessing using the &lt;a href="https://react.dev/reference/react/useRef"&gt;useRef&lt;/a&gt; hook passed as props to the ApplyFilter component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; — we’ve explored the process of calling a child component function from a parent component in a React application. By leveraging the useImperativeHandle hook along with the forwardRef function, we've established a way for the child component to expose specific functions to the parent component through a forwarded ref.&lt;/p&gt;

&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Harnessing the Power of For Loop Comprehension in Python</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Sat, 09 Mar 2024 19:52:17 +0000</pubDate>
      <link>https://forem.com/cleverzone/harnessing-the-power-of-for-loop-comprehension-in-python-10e2</link>
      <guid>https://forem.com/cleverzone/harnessing-the-power-of-for-loop-comprehension-in-python-10e2</guid>
      <description>&lt;p&gt;&lt;a href="https://www.w3schools.com/python/"&gt;Python&lt;/a&gt; is a versatile and powerful programming language, known for its simplicity and readability. One of the language’s key features is the ability to leverage for loop comprehension, a concise syntax that allows you to create new lists, sets, or dictionaries based on existing ones. In this blog, we will explore the wonders of for loop comprehension in Python and illustrate its usefulness with practical examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is For Loop Comprehension?
&lt;/h3&gt;

&lt;p&gt;For loop comprehension, also known as list comprehension, is a compact and elegant way to generate new lists by applying filtering or transformation to an existing iterable. It combines the for loop and conditional statements into a single line of code, resulting in code that is both concise and expressive.&lt;/p&gt;

&lt;p&gt;Syntax of For Loop Comprehension: The syntax for for loop comprehension in Python is as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new_list = [expression for item in iterable if condition]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s break down the components of the syntax:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;expression represents the value you want to include in the new list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;item refers to the individual elements in the iterable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;iterable is the collection of items over which the loop iterates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;condition (optional) allows you to filter the items based on a specified condition.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Benefits of For Loop Comprehension:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability:&lt;/strong&gt; For loop comprehension simplifies code by condensing multiple lines into a single line. It enhances code readability by eliminating the need for explicit looping constructs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conciseness:&lt;/strong&gt; With for loop comprehension, you can achieve in one line what would otherwise require several lines of code. It reduces verbosity and makes your code more compact and expressive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Efficiency:&lt;/strong&gt; List comprehension performs better in terms of speed and memory usage compared to traditional for loops. It takes advantage of Python’s internal optimizations, resulting in improved performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Practical Examples:&lt;/strong&gt; Let’s explore some practical examples to illustrate the power of for loop comprehension.&lt;/p&gt;

&lt;p&gt;Example 1: Generating a Squared List Suppose we have a list of numbers and want to create a new list containing the squares of those numbers.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [1, 2, 3, 4, 5]
squared_numbers = [num ** 2 for num in numbers]
print(squared_numbers)

[1, 4, 9, 16, 25] #out put 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt; Filtering Even Numbers Let’s say we have a list of numbers and want to filter out the even numbers, creating a new list with only the odd numbers.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [1, 2, 3, 4, 5]
odd_numbers = [num for num in numbers if num % 2 != 0]
print(odd_numbers)

[1, 3, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt; Creating a Dictionary We can also use for loop comprehension to create dictionaries. In this example, let’s convert a list of names into a dictionary, with names as keys and their lengths as values.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;names = ["abhi", "kumar", "delhi", "area52"]
name_lengths = {name: len(name) for name in names}
print(name_lengths)

{'abhi': 5, 'kumar': 3, 'delhi': 7, 'area52': 5} # output 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;: For loop comprehension is a powerful technique in Python that allows you to write concise and expressive code. It simplifies your code, improves readability, and enhances performance. By leveraging the benefits of for loop comprehension, you can write elegant and efficient code, making your Python programming experience even more delightful.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>A Guide to : Set and Map</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Sat, 09 Mar 2024 19:49:44 +0000</pubDate>
      <link>https://forem.com/cleverzone/a-guide-to-set-and-map-53dg</link>
      <guid>https://forem.com/cleverzone/a-guide-to-set-and-map-53dg</guid>
      <description>&lt;p&gt;Hello &lt;em&gt;dev&lt;/em&gt; 👋,&lt;/p&gt;

&lt;p&gt;In this blog, we’ll thoroughly discuss JavaScript Set and Map, explaining their advantages and practical uses.&lt;/p&gt;

&lt;p&gt;Then, we’ll cover detailed explanations of Set, Map and finally, we’ll look into its in-build function with example.&lt;/p&gt;

&lt;p&gt;Let’s get start —&lt;/p&gt;

&lt;p&gt;Set —The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"&gt;Set&lt;/a&gt; data structure is essential in JavaScript for maintaining collections of unique data, regardless of their type whether strings numbers, objects, or any other and provide various kind of inbuild function to perform various type operation with data using set.&lt;/p&gt;

&lt;p&gt;Now let see its inbuild function —&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;add( )&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The add method is used with the Set data structure to add new elements to the set. This method ensures that only unique elements are added, preventing duplicates within the set. Here's how it works:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const team = new Set()
team.add('Airospace')
team.add('R&amp;amp;D')   // will not added
console.log(team) // { 'Airospace'} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3. has( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The has method is used to check provided values is exited in current set if the value is already existing at those time its return true otherwise its return false.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const team = new Set()
team.add('Airospace')
team.add('R&amp;amp;D')  

console.log(team.has("Airospace")); //true 
console.log(team.delete("Scientist")); // false 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;4. difference( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The difference() method of Set instances takes another set as an argument and returns a new set containing elements that are present in this set but not in the given set. &lt;em&gt;( new, supported in few browser )&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a = new Set([1, 3, 5, 7, 9]);
const b= new Set([1, 4, 9]);
console.log(a.difference(b)); // Set(3) { 3, 5, 7 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;5. Intersection( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;its return new Set object containing elements in both this set and the other set. &lt;em&gt;( new, supported in few browser )&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a= new Set([1, 3, 5, 7, 9]);
const b= new Set([1, 4, 9]);
console.log(a.intersection(b)); // Set(2) { 1, 9 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;6. isDisjointFrom( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The isDisjointFrom() method of Set instances takes another set as an argument and returns a boolean value indicating whether this set has no elements in common with the given set. &lt;em&gt;( new, supported in few browser )&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a= new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const b= new Set([1, 4, 9, 16]);
console.log(a.isDisjointFrom(b)); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;7. isSubsetOf( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The isSubsetOf() method of Set instances takes another set as an argument and returns a boolean value indicating whether all elements of this set are also present in the given set. &lt;em&gt;( new, supported in few browser )&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a= new Set([4, 8, 12, 16]);
const b = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
console.log(a.isSubsetOf(b)); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;8. isSupersetOf( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The isSupersetOf() method of Set instances takes another set as an argument and returns a boolean value indicating whether all elements of the given set are also present in this set. &lt;em&gt;( new, supported in few browser )&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a= new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
const b= new Set([4, 8, 12, 16]);
console.log(evens.isSupersetOf(b)); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;8. symmetricDifference( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The symmetricDifference() method of Set instances takes another set as an argument and returns a new set containing elements that are present in either this set or the given set, but not in both. &lt;em&gt;( new, supported in few browser )&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a= new Set([2, 4, 6, 8]);
const b= new Set([1, 4, 9]);
console.log(a.symmetricDifference(b)); // Set(5) { 1, 2, 6, 8, 9 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;9.union( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The union() method of Set instances takes another set as an argument and returns a new set containing elements that are present in either this set or the given set, or both. &lt;em&gt;( new, supported in few browser )&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a = new Set([2, 4, 6, 8]);
const b= new Set([1, 4, 9]);
console.log(a.union(b)); // Set(6) { 2, 4, 6, 8, 1, 9 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;10. delete( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The remove method is used to delete the existing value from the set if the value is not existing then its return false otherwise its return true after deletion of provided value.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const team = new Set()
team.add('Airospace')
team.add('R&amp;amp;D')
team.delete('R&amp;amp;D')
console.log(team) // {'Airospace'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;11. clear( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The clear() method removes all elements from the set, effectively making it empty.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const team = new Set()
team.add('Airospace')
team.add('R&amp;amp;D')
console.log(team.clear()) // { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;12. size&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The size property of a Set object returns the number of elements in the set. It provides a convenient way to determine the size of the set without needing to iterate over its elements manually.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const team = new Set()
team.add('Airospace')
team.add('R&amp;amp;D')
console.log(team.size) // 2 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Iteration of Set&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Using forEach() method: The forEach() method allows you to execute a provided function once for each element in the Set in insertion order.&lt;/p&gt;

&lt;p&gt;const mySet = new Set(["apple 🍏", "banana 🍌", "orange 🍊"]);&lt;/p&gt;

&lt;p&gt;mySet.forEach(element =&amp;gt; {&lt;br&gt;
    console.log(element);&lt;br&gt;
});&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;2&lt;/strong&gt;. Using a for...of loop: The for...of loop iterates over the values of an iterable object (like Set) in insertion order.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mySet = new Set(["apple 🍏", "banana 🍌", "orange 🍊"]);

mySet.forEach(element =&amp;gt; {
    console.log(element);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Converting to an array: You can convert the Set to an array using the Array.from() method or the spread operator (...) and then iterate over the array using any array iteration method (forEach(), map(), for...of, etc.).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mySet = new Set(["apple 🍏", "banana 🍌", "orange 🍊"]);
const myArray = Array.from(mySet);

myArray.forEach(element =&amp;gt; {
    console.log(element);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Using the entries, keys, or values iterator: Although Sets don’t have direct methods like keys() or entries(), you can use the iterator methods inherited from Map, but keep in mind that they would behave similarly to values() because sets don't have keys.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mySet = new Set(["apple 🍏", "banana 🍌", "orange 🍊"]);
const setIterator = mySet.values();

for (const value of setIterator) {
    console.log(value);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Real life use case of Set in JS —&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Shopping Cart Items:&lt;/strong&gt; In an e-commerce application, a Set can be used to store unique items in a user's shopping cart. This prevents duplicate entries and ensures efficient management of the cart.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Social Media Followers/Friends List:&lt;/strong&gt; When developing a social media platform, you might use a Set to manage a user's list of followers or friends. This ensures that each user is only added once, preventing duplicates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User Preferences/Settings:&lt;/strong&gt; Sets can be utilized to store unique user preferences or settings in web applications. For instance, you can use a set to manage a user’s selected language options or notification preferences.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Event Registrations: **When managing event registrations or RSVPs, a Set can be employed to store unique attendee IDs or email addresses. This helps in ensuring that each participant is registered only once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Browser Cookies: **In web development, sets can aid in managing browser cookies efficiently. You can use a set to store unique cookie names, ensuring that each cookie is set only once per session.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Search History: **Sets can be useful for managing a user’s search history in a web application. By using a set, you can ensure that each search query is stored only once, preventing duplicate entries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Favorite Items/Bookmarks:&lt;/strong&gt; When implementing a feature for users to bookmark favorite items or content, a Set can be employed to store unique identifiers or URLs. This ensures that each item is bookmarked only once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User Permissions/Roles:&lt;/strong&gt; Sets can be used to manage user permissions or roles in an application. You can use a set to store unique permission levels assigned to each user, ensuring that each permission is granted only once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Quiz/Questionnaire Responses:&lt;/strong&gt; When collecting responses to quizzes or questionnaires, sets can help in storing unique user answers. This prevents duplicate submissions and ensures accurate analysis of responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Email Subscription Lists:&lt;/strong&gt; Sets can be utilized to manage email subscription lists for newsletters or updates. By using a set, you can ensure that each email address is subscribed only once, avoiding duplicate subscriptions.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Absolutely, the use cases for sets are diverse and depend on specific requirements.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**Map — *&lt;/em&gt;*JavaScript, a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"&gt;Map&lt;/a&gt; is an object that stores key-value pairs while maintaining the original insertion order of the keys. Notably, Maps allow us to use both primitive and non-primitive data as keys.&lt;/p&gt;

&lt;p&gt;Let ‘s see its inbuild functions —&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;set( )&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Adds or updates a key-value pair in the Map .&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapDs = new Map()
mapDs.set(1 , "apple 🍎🍏")

console.log(mapDs) // 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. get( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Retrieves the value associated with the specified key.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapDs = new Map()
mapDs.set(1 , "apple 🍎🍏")

console.log(mapDs.get(1)) // "apple 🍎🍏"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3. delete( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Removes the key-value pair associated with the specified key and return true otherwise if those item is not exits then return false.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapDs = new Map()
mapDs.set(1 , "apple 🍎🍏")

console.log(mapDs.delete(1)) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;4. has( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Checks if a key exists in the Map and if exite then return true otherwise false.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapDs = new Map()
mapDs.set(1 , "apple 🍎🍏")

console.log(mapDs.has(1)) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;5. clear( )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Removes all key-value pairs from the Map and return true after deletion of all item from Map.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapDs = new Map()
mapDs.set(1 , "apple 🍎🍏")

console.log(mapDs.clear()) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Iterating Over a Map:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Maps in JavaScript preserve the insertion order of key-value pairs, making them ideal for scenarios where order matters. You can iterate over the keys, values, or entries (key-value pairs) of a Map using various methods like forEach, for...of loop, or entries, keys, and values methods.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Iterating over keys
for (let key of myMap.keys()) {
    console.log(key);
}

// Iterating over values
for (let value of myMap.values()) {
    console.log(value);
}

// Iterating over entries
for (let [key, value] of myMap.entries()) {
    console.log(key, value);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Real life use case of Map in JS —&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;**User Authentication and Authorization: **Store user information with usernames or user IDs as keys and user objects (containing details like username, password hash, access level) as values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Routing and Navigation: **Use Maps to store routes with locations as keys and corresponding route information (distance, waypoints, etc.) as values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Caching and Memoization:&lt;/strong&gt; Cache frequently accessed data or function results, improving performance by storing computed results with input parameters as keys.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Language Localization:&lt;/strong&gt; Store translations for different languages, with language keys and corresponding translated strings as values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error Handling and Logging&lt;/strong&gt;: Track error occurrences with error codes or descriptions as keys and additional error details as values, facilitating error reporting and debugging.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event Handling:&lt;/strong&gt; Manage event listeners by storing event types as keys and associated event handler functions as values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Data Processing Pipelines: **Use Maps to store intermediate results in data processing pipelines, with keys representing stages or steps and values containing processed data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Form Data Handling:&lt;/strong&gt; Store form field names as keys and corresponding input values as values, simplifying form data manipulation and validation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are just a few real-life use cases of Maps in JavaScript; however, the full extent of their application is not limited and wholly depends on the requirements at hand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; — Overall, Sets and Maps are versatile data structures that can greatly simplify your code and improve the efficiency of your applications. Whether you’re managing collections of unique elements or associating data with keys, Sets and Maps provide essential tools for working with data in JavaScript.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Guide to : toReversed, toSorted and toSpliced</title>
      <dc:creator>Abhijeet kumar</dc:creator>
      <pubDate>Sat, 24 Feb 2024 18:34:30 +0000</pubDate>
      <link>https://forem.com/cleverzone/-guide-to-toreversed-tosorted-and-tospliced-9ek</link>
      <guid>https://forem.com/cleverzone/-guide-to-toreversed-tosorted-and-tospliced-9ek</guid>
      <description>&lt;p&gt;In JavaScript, there are numerous built-in functions available, some of which are commonly used in everyday problem-solving scenarios, while others are less frequently utilized. Let’s explore some of these less commonly used built-in functions.&lt;/p&gt;

&lt;p&gt;We will thoroughly explore every aspect of those lesser-utilized built-in functions .&lt;/p&gt;

&lt;p&gt;Let’s exploration.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;toReversed —&lt;/strong&gt; this is similar to the reverse in JavaScript but different in between in that reverse perform of change the original array while toReversed make his own instance of array and transpose element and don’t effect of original array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example —&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// toReversed  's example 

let arr = [ '🤖', '👽','🤷‍♂️' 'whatever...']

console.log(arr.toRevered()) // it don't take any argument 

// Output - [ 'whatever...' ,'🤷‍♂️' , '👽' , '🤖']

console.log(arr)

// original array - [ '🤖', '👽','🤷‍♂️' 'whatever...']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use case with sparsely array : toReversed element should be sparse if that will at those time its return undefined .&lt;/p&gt;

&lt;p&gt;Example —&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// toReversed 's example with sparsely array 

let array = [ '🤖', '👽','🤷‍♂️' 'whatever...' , , ]
console.log(array.toRevered()) // it don't take any argument 
// Output - [ undefined , 'whatever...' ,'🤷‍♂️' , '👽' , '🤖']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you attempt to perform the transpose with toReversed operation on a non-array object, it iterates through each property of the object. For properties with integer keys, it reverses their values, while for non-integer keys, it returns undefined. This behaviour ensures consistency even when operating on non-array objects.&lt;/p&gt;

&lt;p&gt;Example —&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrayLike = {
  color: 'Carbon black',
  brand: 'Dassault Falcon 7X',
  2: 4,
  4:4,
};
console.log(Array.prototype.toReversed.call(arrayLike));
// [4, 4, undefined, undefined]
// here 0 , 1 index become undefined 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. toSorted —&lt;/strong&gt;The toSorted function works just like sort, but it's more careful. While sort messes with your original array, toSorted takes a copy of it, sorts the copy, and leaves your original array untouched. It's like having a tidy clone do the sorting while your original stays safe and sound.&lt;/p&gt;

&lt;p&gt;by default it is return a ascending sorted new array copy of given array but by providing compare argument we can change it as well.&lt;/p&gt;

&lt;p&gt;It is take argument as the compare function. which is optional by default it sort the array element in ascending order.&lt;/p&gt;

&lt;p&gt;Example —&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [ 'India' , 'USA' , 'Germany' ,'Russia']

let sorted = arr.toSorted() 

// Output - ['Germany', 'India', 'Russia', 'USA']

console.log(arr)

// Original array - [ 'India' , 'USA' , 'Germany' ,'Russia']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use case with sparsely array : As we already discussed that if there ‘ll be any spares array element at that will become as undefined .&lt;/p&gt;

&lt;p&gt;let see with example —&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// toSroted with sparse array 

console.log([, undefined, "a", "b" ,"c" ].toSorted()); 

// Output -  ["a", "b","c" , undefined, undefined]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When using toSorted with non-array objects, it behaves similarly to the function discussed earlier. In this case, keys that are integers will be sorted, while their corresponding values will also be sorted accordingly. However, non-integer keys will be treated as undefined.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrayLike = {
  length: 3,
  color: "mettalic black",
  0: 5,
  2: 4,
};

console.log(Array.prototype.toSorted.call(arrayLike));
// Output - [4, 5, undefined]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;**toSpliced — **we ‘re already familiar with splice ( inserting , replace , deletion ) in build function just like with toSpliced function we can also insert , replace and delete the elements from array but it don't modified the original array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And here we also about cover all operation which we can perform with toSpliced function.&lt;/p&gt;

&lt;p&gt;Syntax :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;toSpliced(start, deleteCount, item1, item2, item3 , ....itemN)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s break it down further to understand its intricacies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;start-&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The index at which changes in the array will begin is specified as a zero-based integer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If a negative index is provided, it counts backward from the end of the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the starting index is less than the negative length of the array or if it’s omitted, the operation will start from the beginning (index 0).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the starting index is equal to or greater than the length of the array, no elements will be deleted. Instead, elements will be added based on the provided values.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;deleteCount&lt;/em&gt;&lt;/strong&gt; (optional parameter )&lt;strong&gt;-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This parameter indicates the number of elements to remove from the array, starting from the specified index.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If deleteCount is not provided or is greater than or equal to the number of elements after the specified start index, all elements from the start index to the end of the array will be removed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you want to remove all elements after the start index and also pass additional items, you must use Infinity as the deleteCount.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If deleteCount is 0 or negative, no elements are removed. In such cases, you should add at least one new element.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;item1 …itemN **(optional parameter )&lt;/strong&gt;-**&lt;/p&gt;

&lt;p&gt;These are the elements that will be added to the array, starting from the specified index.&lt;/p&gt;

&lt;p&gt;in case we don’t specify any elements, toSpliced() will only remove elements from the array.&lt;/p&gt;

&lt;p&gt;Example —&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// insertion , deletion and replace using toSpliced

const countries = ["India", "USA", "UK", "Russia"];

// insertion an element at index 1

const newCountries = countries.toSpliced(1, 0, "China");

console.log(newCountries); // ["India","China", "USA", "UK", "Russia"]

// deletion of two elements starting from index 2

const countryDeleted = newCountries.toSpliced(2, 2);
console.log(countryDeleted); // ["India","China", "USA"]

// replace a element at index 1 with three new array element
const countryNameReplaced = countryDeleted.toSpliced(1, 1, "Japana", "South Korea");

console.log(countryNameReplaced); // ["India","Japana" , "South Korea", "USA"]

// there 're no changes in original array 

console.log(countries); // ["India", "USA", "UK", "Russia"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In toSpliced , sparsely and non array object behaviour are similar as we have discussed respectively in toReversed() and toSorted().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; — Each of these functions serves a specific role in helping developers to effectively arrange and change arrays in JavaScript. By grasping how to use them, we can improve their code’s capabilities and speed.&lt;/p&gt;

&lt;p&gt;Happy coding !!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
