Important Notice:

Syntax Error

Syntax Error

4 views 2 min read

Syntax Error (सिंटैक्स त्रुटि) :-

Syntax Error (सिंटैक्स त्रुटि) तब होती है जब Python Program लिखते समय उसके लिखने के नियम (Syntax Rules) का पालन नहीं किया जाता। Python सबसे पहले Program की Syntax को जांचता है। यदि कोई गलती मिलती है, तो Program Run होने से पहले ही रुक जाता है और SyntaxError प्रदर्शित करता है।

English:

A Syntax Error occurs when the rules (syntax) of the Python programming language are not followed. Python checks the syntax before executing the program. If it finds any mistake, the program stops before execution and displays a SyntaxError.

Syntax Error क्यों आती है? (Causes of Syntax Error) -

  • Colon (:) भूल जाना। — Missing colon (:).
  • Parentheses () पूरा न करना। — Missing parentheses ().
  • Quotes (" या ') बंद न करना। — Missing quotation marks (" or ').
  • Python Keyword गलत लिखना। — Misspelled Python keywords.
  • Comma (,) भूल जाना। — Missing comma (,).
  • Statement गलत लिखना। — Incorrect statement structure.

1. Colon (:) भूल जाना — Missing Colon

Wrong Code:

 
if age >= 18
    print("Eligible")
 

Output:

 
SyntaxError: expected ':'
 

Correct Code:

 if age >= 18:
    print("Eligible")
 

2. Parentheses () पूरा न करना — Missing Parentheses -

Wrong Code:

 
print("Hello"
 

Output:

 
SyntaxError: '(' was never closed
 

Correct Code:

 
print("Hello")
 

3. Quotes (" या ') बंद न करना — Missing Quotation Marks -

Wrong Code:

 
print("Hello)
 

Output:

 
SyntaxError: unterminated string literal
 

Correct Code:

 
print("Hello")
 

4. Python Keyword गलत लिखना — Misspelled Python Keyword -

Wrong Code:

 
iff age >= 18:
    print("Eligible")
 

Output:

 
SyntaxError: invalid syntax
 

Correct Code:

 
if age >= 18:
    print("Eligible")
 

5. Comma (,) भूल जाना — Missing Comma -

Wrong Code:

 
print("Python", "Programming"
 

Output:

 
SyntaxError: '(' was never closed
 

Correct Code:

 
print("Python", "Programming")
 

6. Statement गलत लिखना — Incorrect Statement Structure -

Wrong Code:

 
x = 10 +
 

Output:

 
SyntaxError: invalid syntax
 

Correct Code:

 
x = 10 + 5
print(x)
 

Output:

 
15

Related Notes