किसी भी Programming Language को सीखने का पहला नियम उसके Syntax (सिंटैक्स) को समझना होता है। यदि Syntax सही नहीं होगी, तो Program Execute नहीं होगा और Error दिखाई देगी।
Python की सबसे बड़ी विशेषता यह है कि इसकी Syntax बहुत सरल और पढ़ने में आसान होती है। अन्य Programming Languages जैसे C, C++ या Java की तुलना में Python में कम Code लिखकर अधिक कार्य किया जा सकता है।
Python में Curly Braces {} की जगह Indentation (Spaces) का उपयोग किया जाता है, जो इसे अन्य भाषाओं से अलग बनाता है। इसलिए Python सीखते समय Syntax के साथ-साथ Indentation को समझना भी बहुत आवश्यक है।
इस अध्याय में हम Python Syntax, Keywords, Identifiers, Comments, Statements, Indentation और Common Syntax Errors को विस्तार से समझेंगे।
Syntax उन नियमों (Rules) का समूह है जिनके अनुसार Python Program लिखा जाता है।
यदि Program इन नियमों के अनुसार लिखा गया है, तो Python Interpreter उसे Execute कर देगा। यदि नियमों का पालन नहीं किया गया, तो Syntax Error आएगी।
print("Welcome to Python")
Output
Welcome to Python
print("Welcome to Python"
Output
SyntaxError: '(' was never closed
ऊपर दिए गए Program में Closing Parenthesis ) नहीं है, इसलिए Syntax Error आती है।
Python में Program लिखते समय निम्न नियमों का पालन करना आवश्यक है—
- प्रत्येक Statement सही Syntax के अनुसार लिखें।
- Python Case Sensitive Language है।
- Indentation का सही उपयोग करें।
- Keywords को Variable Name के रूप में उपयोग नहीं किया जा सकता।
- Variable Names उचित नियमों के अनुसार रखें।
- Brackets, Quotes और Colon (:) का सही उपयोग करें।
Python Case Sensitive Language है
Python में बड़े (Uppercase) और छोटे (Lowercase) अक्षरों को अलग-अलग माना जाता है।
name = "Ravi"
Name = "Mohan"
print(name)
print(Name)
Output
Ravi
Mohan
यहाँ name और Name दो अलग-अलग Variables हैं।
Program में लिखे गए प्रत्येक निर्देश (Instruction) को Statement कहा जाता है।
x = 20
y = 30
print(x + y)
यहाँ तीन अलग-अलग Statements हैं।
Python में एक ही लाइन में एक से अधिक Statements लिखी जा सकती हैं, लेकिन इसकी अनुशंसा नहीं की जाती क्योंकि इससे Code पढ़ना कठिन हो जाता है।
a = 10; b = 20; print(a+b)
बेहतर तरीका—
a = 10
b = 20
print(a+b)
Python में समान Indentation वाले Statements मिलकर एक Block बनाते हैं।
age = 18
if age >= 18:
print("Eligible")
print("You can vote")
print("Program Finished")
यहाँ if के अंदर दोनों Statements एक Block का भाग हैं।
Python में Indentation का अर्थ है किसी Statement के पहले दिए गए Spaces या Tab।
अन्य भाषाओं में Block बनाने के लिए {} का उपयोग किया जाता है, लेकिन Python में Block बनाने के लिए Indentation का उपयोग किया जाता है।
age = 20
if age >= 18:
print("Adult")
age = 20
if age >= 18:
print("Adult")
Output
IndentationError
Python में कितने Spaces का उपयोग करना चाहिए?
PEP 8 (Python Style Guide) के अनुसार—
- एक Level Indentation के लिए 4 Spaces का उपयोग करना चाहिए।
उदाहरण—
if True:
print("Python")
Python में Tab और Spaces दोनों का उपयोग किया जा सकता है, लेकिन दोनों को मिलाकर उपयोग करना उचित नहीं है।
सबसे अच्छा तरीका है—
✔ केवल 4 Spaces का उपयोग करें।
Comments वे Statements होते हैं जिन्हें Python Execute नहीं करता।
इनका उपयोग Program को समझाने या Notes लिखने के लिए किया जाता है।
Single Line Comment लिखने के लिए # का उपयोग किया जाता है।
# This is a comment
print("Hello")
print("Python") # Printing Message
Python में Multi-Line Comment के लिए सामान्यतः Triple Quotes (''' या """) का उपयोग Documentation या Multi-line String के रूप में किया जाता है।
"""
This is
a multi-line
comment
"""
ध्यान दें: तकनीकी रूप से Triple Quotes एक Multi-line String बनाते हैं। इन्हें अक्सर Documentation (Docstrings) के लिए उपयोग किया जाता है।
Keywords वे Reserved Words होते हैं जिनका विशेष अर्थ होता है।
इनका उपयोग Variable या Function Name के रूप में नहीं किया जा सकता।
कुछ प्रमुख Keywords—
- if
- else
- elif
- for
- while
- break
- continue
- pass
- return
- class
- def
- import
- try
- except
- finally
- True
- False
- None
- and
- or
- not
- lambda
- global
- nonlocal
- Reserved Words होते हैं।
- पहले से निर्धारित अर्थ रखते हैं।
- Variable Name नहीं बन सकते।
- सभी छोटे अक्षरों (Lowercase) में होते हैं (कुछ अपवाद जैसे True, False, None)।
Program में Variable, Function, Class या Object के नाम को Identifier कहा जाता है।
उदाहरण—
student_name = "Ravi"
age = 20
marks = 95
यहाँ—
- student_name
- age
- marks
तीनों Identifiers हैं।
✔ अक्षर (A-Z, a-z) से शुरू हो सकता है।
✔ Underscore (_) से शुरू हो सकता है।
✔ बाद में Numbers का उपयोग किया जा सकता है।
✔ Special Characters (@, #, %, &) का उपयोग नहीं किया जा सकता।
✔ Keyword का उपयोग नहीं किया जा सकता।
name
student_name
age1
_total
marks2025
1name
student-name
class
for
user@name
बेहतर Code लिखने के लिए निम्न Naming Convention अपनाएँ—
student_name
total_marks
PI = 3.14
MAX_SIZE = 100
Student
EmployeeDetails
calculate_total()
print_result()
if x > 10
❌ Error
सही—
if x > 10:
print(Hello)
सही—
print("Hello")
print("Python"
if True:
print("Hello")
iff x > 10:
- हमेशा 4 Spaces का उपयोग करें।
- Meaningful Variable Names रखें।
- उचित Comments लिखें।
- Keywords को Variable Name न बनाएँ।
- Code को साफ़ और Readable रखें।
- PEP 8 Coding Style का पालन करें।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- Python एक Case Sensitive Language है।
- Block बनाने के लिए Indentation का उपयोग होता है।
- Standard Indentation 4 Spaces है।
- Single Line Comment के लिए # का उपयोग किया जाता है।
- Keywords Reserved Words होते हैं।
- Variable Name Keyword नहीं हो सकता।
- Python में Curly Braces {} की आवश्यकता नहीं होती।
Frequently Asked Questions (FAQs)
प्रश्न 1. Python Syntax क्या है?
Syntax उन नियमों का समूह है जिनके अनुसार Python Program लिखा जाता है।
प्रश्न 2. Python में Block कैसे बनाया जाता है?
Indentation (आमतौर पर 4 Spaces) की सहायता से।
प्रश्न 3. Python में Single Line Comment कैसे लिखते हैं?
# का उपयोग करके।
प्रश्न 4. क्या Keyword को Variable Name बनाया जा सकता है?
नहीं।
प्रश्न 5. क्या Python Case Sensitive Language है?
हाँ, Name और name अलग-अलग Variables माने जाते हैं।
- Syntax = Program लिखने के नियम
- Python Case Sensitive Language है।
- Block = समान Indentation वाले Statements
- Standard Indentation = 4 Spaces
- # = Single Line Comment
- Keywords = Reserved Words
- Identifiers = Variable, Function, Class आदि के नाम
- Keywords को Identifier नहीं बनाया जा सकता।
Python Variables (वेरिएबल), Constants और Data Types — इसमें Variables क्या हैं, Variable Declaration, Naming Rules, Constants, Dynamic Typing, type() Function, Type Conversion तथा सभी Built-in Data Types (int, float, complex, bool, str, list, tuple, set, dictionary) को उदाहरण सहित विस्तार से समझेंगे।