Python을 사용하여 인도 핀코드 검증하는 방법

발행: (2026년 4월 20일 PM 05:38 GMT+9)
2 분 소요
원문: Dev.to

Source: Dev.to

파이썬으로 인도 핀코드 검증하기 (지역, 하위 지역 및 구 찾기)

다음 스크립트는 6자리 인도 핀코드를 검증하고 해당 지역, 하위 지역, 그리고 를 반환합니다.

# Mapping of first digit to region
regions = {
    1: "Delhi",
    2: "Uttar Pradesh & Uttarakhand",
    3: "Rajasthan & Gujarat",
    4: "Maharashtra",
    5: "Andhra Pradesh & Telangana",
    6: "Tamil Nadu & Kerala",
    7: "West Bengal & North East",
    8: "Bihar & Jharkhand",
    9: "Army Postal Service"
}

# Mapping of first two digits to sub‑region
sub_regions = {
    11: "Delhi Region",
    40: "Mumbai Region",
    60: "Chennai Region",
    70: "Kolkata Region"
}

# Mapping of first three digits to district
districts = {
    110: "Delhi",          111: "Delhi (North)",      112: "Delhi (South)",
    400: "Mumbai",         401: "Mumbai (Suburban)",  402: "Mumbai (West)",
    600: "Chennai",        601: "Tiruvallur",         602: "Kanchipuram",
    700: "Kolkata",        701: "Kolkata (North)",    702: "Kolkata (South)"
}

def get_pincode_details(pincode: str) -> None:
    """
    Validate the pincode and print its region, sub‑region, and district.
    """
    if not pincode.isdigit() or len(pincode) != 6:
        print("Invalid pincode. Enter exactly 6 digits.")
        return

    num = int(pincode)
    first_digit = num // 100_000          # e.g., 4 for 400001
    first_two   = num // 10_000           # e.g., 40 for 400001
    first_three = num // 1_000            # e.g., 400 for 400001

    print("Region:     ", regions.get(first_digit,   "Unknown"))
    print("Sub‑region: ", sub_regions.get(first_two, "Unknown"))
    print("District:   ", districts.get(first_three, "Unknown"))

# Example usage
pincode = input("Enter 6‑digit Pincode: ").strip()
get_pincode_details(pincode)
0 조회
Back to Blog

관련 글

더 보기 »

한 줄에 리스트 작성하기 (List Comprehensions)

소개 파이썬에서는 루프를 사용해 리스트를 만들 수 있지만, 리스트 컴프리헨션을 사용하면 같은 작업을 한 줄의 가독성 좋은 코드로 할 수 있습니다. python numbers = 1, 2, 3,...