मुख्य कंटेंट पर जाएं
Python Syntax in Hindi
PYTHON PROGRAMMING 12 मिनट पढ़ें 3 बार पढ़ा गया

Python Syntax in Hindi

Lavkush Chakrawarti

परिचय

Python Syntax क्या है?

उदाहरण (सही Syntax)

उदाहरण (गलत Syntax)

Python की मुख्य Syntax Rules

Python Case Sensitive Language है

उदाहरण

Python Statement क्या है?

उदाहरण

Multiple Statements

Python Block क्या होता है?

उदाहरण

Indentation क्या है?

सही उदाहरण

गलत उदाहरण

Python में कितने Spaces का उपयोग करना चाहिए?

Tab और Spaces

Python Comments क्या हैं?

Single Line Comment

उदाहरण

Inline Comment

Multi-Line Comment

Python Keywords क्या हैं?

Python Keywords की विशेषताएँ

Identifiers क्या हैं?

Identifier बनाने के नियम

सही Identifier

गलत Identifier

Naming Convention

Variable

Constant

Class

Function

Common Syntax Errors

1. Missing Colon

2. Missing Quotes

3. Missing Parenthesis

4. Wrong Indentation

5. Misspelled Keyword

Best Practices

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

Frequently Asked Questions (FAQs)

प्रश्न 1. Python Syntax क्या है?

प्रश्न 2. Python में Block कैसे बनाया जाता है?

प्रश्न 3. Python में Single Line Comment कैसे लिखते हैं?

प्रश्न 4. क्या Keyword को Variable Name बनाया जा सकता है?

प्रश्न 5. क्या Python Case Sensitive Language है?

Quick Revision

अगला अध्याय

 

परिचय

किसी भी 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 को विस्तार से समझेंगे।

 

Python Syntax क्या है?

Syntax उन नियमों (Rules) का समूह है जिनके अनुसार Python Program लिखा जाता है।

यदि Program इन नियमों के अनुसार लिखा गया है, तो Python Interpreter उसे Execute कर देगा। यदि नियमों का पालन नहीं किया गया, तो Syntax Error आएगी।

उदाहरण (सही Syntax)

print("Welcome to Python")

Output

Welcome to Python

 

उदाहरण (गलत Syntax)

print("Welcome to Python"

Output

SyntaxError: '(' was never closed

ऊपर दिए गए Program में Closing Parenthesis ) नहीं है, इसलिए Syntax Error आती है।

 

Python की मुख्य Syntax Rules

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 हैं।

 

Python Statement क्या है?

Program में लिखे गए प्रत्येक निर्देश (Instruction) को Statement कहा जाता है।

उदाहरण

x = 20

y = 30

print(x + y)

यहाँ तीन अलग-अलग Statements हैं।

 

Multiple Statements

Python में एक ही लाइन में एक से अधिक Statements लिखी जा सकती हैं, लेकिन इसकी अनुशंसा नहीं की जाती क्योंकि इससे Code पढ़ना कठिन हो जाता है।

a = 10; b = 20; print(a+b)

बेहतर तरीका—

a = 10

b = 20

print(a+b)

 

Python Block क्या होता है?

Python में समान Indentation वाले Statements मिलकर एक Block बनाते हैं।

उदाहरण

age = 18

 

if age >= 18:

    print("Eligible")

    print("You can vote")

 

print("Program Finished")

यहाँ if के अंदर दोनों Statements एक Block का भाग हैं।

 

Indentation क्या है?

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")

 

Tab और Spaces

Python में Tab और Spaces दोनों का उपयोग किया जा सकता है, लेकिन दोनों को मिलाकर उपयोग करना उचित नहीं है।

सबसे अच्छा तरीका है—

✔ केवल 4 Spaces का उपयोग करें।

 

Python Comments क्या हैं?

Comments वे Statements होते हैं जिन्हें Python Execute नहीं करता।

इनका उपयोग Program को समझाने या Notes लिखने के लिए किया जाता है।

 

Single Line Comment

Single Line Comment लिखने के लिए # का उपयोग किया जाता है।

उदाहरण

# This is a comment

print("Hello")

 

Inline Comment

print("Python")  # Printing Message

 

Multi-Line Comment

Python में Multi-Line Comment के लिए सामान्यतः Triple Quotes (''' या """) का उपयोग Documentation या Multi-line String के रूप में किया जाता है।

"""

This is

a multi-line

comment

"""

ध्यान दें: तकनीकी रूप से Triple Quotes एक Multi-line String बनाते हैं। इन्हें अक्सर Documentation (Docstrings) के लिए उपयोग किया जाता है।

 

Python Keywords क्या हैं?

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

 

Python Keywords की विशेषताएँ

  • Reserved Words होते हैं।
  • पहले से निर्धारित अर्थ रखते हैं।
  • Variable Name नहीं बन सकते।
  • सभी छोटे अक्षरों (Lowercase) में होते हैं (कुछ अपवाद जैसे True, False, None)।

 

Identifiers क्या हैं?

Program में Variable, Function, Class या Object के नाम को Identifier कहा जाता है।

उदाहरण—

student_name = "Ravi"

age = 20

marks = 95

यहाँ—

  • student_name
  • age
  • marks

तीनों Identifiers हैं।

 

Identifier बनाने के नियम

✔ अक्षर (A-Z, a-z) से शुरू हो सकता है।

✔ Underscore (_) से शुरू हो सकता है।

✔ बाद में Numbers का उपयोग किया जा सकता है।

✔ Special Characters (@, #, %, &) का उपयोग नहीं किया जा सकता।

✔ Keyword का उपयोग नहीं किया जा सकता।

 

सही Identifier

name

student_name

age1

_total

marks2025

 

गलत Identifier

1name

student-name

class

for

user@name

 

Naming Convention

बेहतर Code लिखने के लिए निम्न Naming Convention अपनाएँ—

Variable

student_name

total_marks

Constant

PI = 3.14

MAX_SIZE = 100

Class

Student

EmployeeDetails

Function

calculate_total()

print_result()

 

Common Syntax Errors

  1. Missing Colon

if x > 10

❌ Error

सही—

if x > 10:

 

  1. Missing Quotes

print(Hello)

सही—

print("Hello")

 

  1. Missing Parenthesis

print("Python"

 

  1. Wrong Indentation

if True:

print("Hello")

 

  1. Misspelled Keyword

iff x > 10:

 

Best Practices

  • हमेशा 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 माने जाते हैं।

 

Quick Revision

  • 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) को उदाहरण सहित विस्तार से समझेंगे।

 

निष्कर्ष (Conclusion)

Python Syntax in Hindi की पूरी जानकारी। Python के नियम, Keywords, Identifiers, Comments, Indentation, Statements, Blocks, Case Sensitivity और Syntax Errors को उदाहरण सहित विस्तार से सीखें।