Python errors — Hindi में समझो
🌐 यह page Hindi में है · Read this in English →
6 सबसे common errorsहर एक के साथ practice problems
Error आए तो घबराना नहीं — पढ़ना सीखो
Python का error message डराने के लिए नहीं, मदद के लिए होता है। हमेशा नीचे से पढ़ो: आखिरी line बताती है कौन सा error है और क्यों आया। उसके ऊपर की lines बताती हैं कौन सी line पर आया। बस यही दो चीज़ें चाहिए।
IndexError: list index out of range
जब आप list से कोई ऐसी position माँगते हो जो exist ही नहीं करती, तो Python IndexError देता है। सबसे ज़रूरी बात: Python 0 से गिनती शुरू करता है। मतलब 3 items वाली list में positions हैं 0, 1, 2 — position 3 नहीं है!
fruits = ["aam", "kela", "seb"]
print(fruits[3]) # ❌ IndexError! आखिरी item fruits[2] है
याद रखने वाला rule: list में n items हैं तो आखिरी valid index n−1 है। आखिरी item चाहिए तो fruits[-1] लिखो — यह हमेशा safe है (जब तक list खाली न हो)।
Common galti: loop में range(len(list) + 1) लिखना — वह +1 एक extra round चला देता है और आखिरी round पर crash। range(len(list)) ही सही है।
🛠️ Practice (free, browser में)
- आखिरी item का गलत index — browser में ही fix करो
- Loop एक कदम ज़्यादा चल गया — browser में ही fix करो
- English deep-dive: पूरा traceback analysis →
NameError: name 'x' is not defined
इसका सीधा मतलब: आपने कोई variable use किया जो Python को मिला ही नहीं — या तो spelling गलत है, या उसे बनाने वाली line अभी तक चली ही नहीं।
naam = "Rahul"
print(nam) # ❌ NameError — 'nam' लिखा, 'naam' नहीं
Debug करने का तरीका: error message में जो नाम quotes में लिखा है, उसे copy करो और file में search करो। ज़्यादातर बार एक letter का फर्क निकलेगा — total vs totals जैसा। Python 3.10+ तो खुद suggest भी करता है: "Did you mean: 'naam'?" — यह suggestion सबसे पहले पढ़ो।
दूसरा common कारण: variable को use ऊपर की line में किया और बनाया नीचे की line में। Python file को ऊपर से नीचे चलाता है — पहले बनाओ, फिर use करो।
🛠️ Practice (free, browser में)
- Variable बनने से पहले use करना — browser में ही fix करो
- Function define होने से पहले call — browser में ही fix करो
- English deep-dive: पूरा traceback analysis →
TypeError: can only concatenate str (not "int") to str
Python कभी guess नहीं करता। "Score: " + 90 लिखने पर वह सोचता नहीं कि आपको "Score: 90" चाहिए — वह रुक जाता है, क्योंकि text + number का कोई एक पक्का मतलब नहीं है।
score = 90
print("Score: " + score) # ❌ TypeError
print(f"Score: {score}") # ✅ f-string खुद convert कर देता है
सबसे आसान fix: f-string use करो — f"Score: {score}"। यह number को अपने आप text बना देता है और code पढ़ने में भी साफ रहता है।
इसी family के और errors: 'int' object is not callable (आपने किसी variable के आगे brackets लगा दिए — शायद sum = sum(nums) जैसा kuch), और 'int' object is not iterable (for i in 5: लिखा — सही है for i in range(5):)।
🛠️ Practice (free, browser में)
- print में text + number — browser में ही fix करो
- Variable को function की तरह call — browser में ही fix करो
- English deep-dive: पूरा traceback analysis →
KeyError: 'age'
Dictionary से आपने ऐसी key माँगी जो उसमें है ही नहीं। अच्छी बात यह है कि error message में वही key छपती है जो आपने माँगी — तो आधा जवाब message में ही है।
student = {"naam": "Priya", "class": 10}
print(student["age"]) # ❌ KeyError: 'age'
ध्यान रखो: keys बिल्कुल exact match होती हैं। 'Naam', 'naam' और 'naam ' (space के साथ) — तीनों अलग keys हैं। Case और spelling दोनों check करो: print(list(student.keys())) लगाओ और error वाली key से मिलाओ।
कब कौन सा lookup: key पक्का होनी चाहिए तो student["age"] ही रखो (crash होना सही है — data की galti पकड़ी जाएगी)। Key optional है तो student.get("age", 0) — यह crash नहीं करता, default दे देता है।
🛠️ Practice (free, browser में)
- Missing dictionary key — browser में ही fix करो
- गलत dictionary key — browser में ही fix करो
- English deep-dive: पूरा traceback analysis →
ValueError: invalid literal for int() with base 10: 'abc'
यहाँ type सही था पर value गलत। int("abc") में string देना allowed है — पर "abc" के characters number नहीं बनते, इसलिए ValueError।
umar = int(input("Umar batao: ")) # user ने लिखा: "pachees"
# ❌ ValueError: invalid literal for int() with base 10: 'pachees'
int() सिर्फ यह मानता है: आगे-पीछे space (चलेगा), +/− sign, और digits। बस। int("12.5") भी fail होगा — decimal चाहिए तो पहले float() करो।
User input के लिए sahi pattern: conversion को try/except में लपेटो — पर सिर्फ conversion वाली line को, पूरे program को नहीं:
try:
umar = int(text)
except ValueError:
print("Number likho bhai!")
🛠️ Practice (free, browser में)
- गलत text को number बनाना — browser में ही fix करो
- Unpacking में values कम-ज़्यादा — browser में ही fix करो
- English deep-dive: पूरा traceback analysis →
ZeroDivisionError: division by zero
Math का rule: zero से divide नहीं कर सकते। पर असली programs में कोई 10 / 0 लिखता नहीं — zero computed होता है: खाली list का len(), कोई counter जो कभी बढ़ा नहीं।
def average(marks):
return sum(marks) / len(marks)
average([]) # ❌ नया student, marks अभी नहीं — len([]) = 0
सही fix: divide करने से पहले check करो और खुद decide करो कि empty case में क्या हो:
def average(marks):
if not marks:
return 0.0 # या None, या error — पर सोच कर!
return sum(marks) / len(marks)
Galti mat karna: except ZeroDivisionError: return 0 — इससे "data नहीं है" और "average सच में 0 है" एक जैसे दिखने लगते हैं। यह bug छुपाना है, fix करना नहीं।
🛠️ Practice (free, browser में)
- Zero check किए बिना division — browser में ही fix करो
- Empty input पर average crash — browser में ही fix करो
- English deep-dive: पूरा traceback analysis →
आगे क्या?
ये 6 errors समझ गए तो beginner-level के 80% crashes आप खुद fix कर सकते हो। अब learning paths से structured practice शुरू करो — Python Basics path सबसे सही शुरुआत है। और English में detail चाहिए तो error explainer hub पर हर error का पूरा breakdown है।