BCA / B.Tech 8 min read

What are Identifiers in Python? Rules और Examples (Hindi)

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Python Notes in Hindi | पायथन नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: What are Identifiers in Python? Rules और Examples (Hindi) - General Topics
  • • अवलोकन: Quick Answer: Python में identifier वह नाम है जो आप किसी variable, function, class या module को देते हैं, ताकि program में उसे पहचाना जा सके। जैसे total...
  • • मुख्य अवधारणाएं: Python identifiers, Naming rules conventions Hindi, Variables functions classes,Python identifiers, Naming rules conventions, Variables functions classes
Quick Answer: Python में identifier वह नाम है जो आप किसी variable, function, class या module को देते हैं, ताकि program में उसे पहचाना जा सके। जैसे total_marks = 450 में total_marks एक identifier है। Identifier letter या underscore (_) से शुरू होता है, उसमें letters, digits और underscore हो सकते हैं, वह keyword नहीं हो सकता, और Python में यह case-sensitive होता है।

What are Identifiers in Python? (Identifier क्या है)

"Identifier" शब्द "identify" (पहचानना) से बना है। जैसे class में हर student का एक नाम होता है जिससे teacher उसे बुलाती है, वैसे ही program में हर variable, function, class, module और object का एक नाम होता है। यही नाम identifier है।

student_name = "Amit"        # variable का identifier: student_name

def calculate_total(a, b):   # function का identifier: calculate_total
    return a + b

class Student:               # class का identifier: Student
    pass

import math                  # module का identifier: math

ऊपर student_name, calculate_total, a, b, Student और math, सब identifiers हैं। def, return, class, pass और import identifiers नहीं, बल्कि keywords हैं।

Identifier सबसे ज़्यादा variables के नाम में दिखते हैं। Variable क्या है, यह detail में Variables in Hindi में पढ़ें।

Advertisement

Rules for Identifiers in Python (Identifier बनाने के नियम)

ये rules Python खुद लागू करता है। Rule टूटा तो program चलेगा ही नहीं, SyntaxError आएगा।

  1. Allowed characters: Letters (a–z, A–Z), digits (0–9) और underscore (_)।
  2. Digit से शुरू नहीं हो सकता: name1 सही, 1name गलत।
  3. Underscore से शुरू हो सकता है: _name, _count सही हैं।
  4. Special symbols नहीं: @, $, %, #, - और space नहीं चलते। my-name और my name दोनों गलत।
  5. Keyword नहीं हो सकता: if, for, class, def, while जैसे reserved words नाम नहीं बन सकते।
  6. Case-sensitive: marks, Marks और MARKS तीन अलग identifiers हैं।
  7. Length की कोई limit नहीं: नाम कितना भी लंबा हो सकता है, पर पढ़ने लायक रखें।

एक extra बात (Python 3): Python 3 में letters का मतलब सिर्फ English letters नहीं, Unicode letters भी हैं। यानी नाम = "Ravi" भी चल जाता है। पर exam और असली projects में English नाम ही लिखें।

Identifiers in Python Example: Valid और Invalid

IdentifierValid / Invalidकारण
total_marksValidLetters और underscore
_countValidUnderscore से शुरू हो सकता है
name1ValidDigit बीच या आखिर में चल सकता है
ClassValidKeyword class है (small c); Class अलग नाम है
1nameInvalidDigit से शुरू
@nameInvalidSpecial symbol @
my-nameInvalidHyphen (-) को Python minus समझता है
my nameInvalidSpace नहीं चलता
forInvalidKeyword है

Program 1: Valid identifiers

name = "Ram"
_age = 25
address1 = "Delhi"
myVariable = 10
total_marks = 450
print(name, _age, address1, myVariable, total_marks)

Output:

Ram 25 Delhi 10 450

Program 2: Invalid identifiers से Error

नीचे की हर line अलग से चलाने पर Python 3.12 में यह error आता है:

1name = "Ram"    # SyntaxError: invalid decimal literal
@name = "Shiv"   # SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
for = 10         # SyntaxError: invalid syntax
my-name = 5      # SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?

Error का exact message Python के version के हिसाब से थोड़ा अलग हो सकता है, पर हर case में SyntaxError ही आता है।

Program 3: Case-sensitivity

marks = 80
Marks = 95
print(marks)
print(Marks)

Output:

80
95

दोनों अलग variables हैं, इसलिए दोनों की value अलग-अलग print होती है।

Program से Check करें कि Identifier Valid है या नहीं

String का isidentifier() method बताता है कि नाम rules के हिसाब से सही है या नहीं। ध्यान दें: यह keyword को check नहीं करता। "for".isidentifier() भी True देता है। इसलिए keyword check के लिए keyword module साथ में लगाएँ:

import keyword

def is_valid(name):
    return name.isidentifier() and not keyword.iskeyword(name)

for n in ["total_marks", "_count", "2nd_value", "for", "student-name", "Class"]:
    print(n, "->", is_valid(n))

Output:

total_marks -> True
_count -> True
2nd_value -> False
for -> False
student-name -> False
Class -> True

Python Keywords की List

Python 3.12 में keyword.kwlist में 35 keywords हैं। इनमें से कोई भी identifier नहीं बन सकता:

False  None  True  and  as  assert  async  await  break  class
continue  def  del  elif  else  except  finally  for  from  global
if  import  in  is  lambda  nonlocal  not  or  pass  raise
return  try  while  with  yield

आप अपने system पर import keyword; print(keyword.kwlist) चलाकर यह list देख सकते हैं।

Worked Example: Program में Identifiers पहचानिए

Exam में अक्सर एक छोटा program देकर पूछा जाता है कि इसमें कौन-कौन से identifiers हैं। नीचे का program देखिए:

import math

MAX_MARKS = 100

class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def percentage(self):
        return self.marks / MAX_MARKS * 100

s1 = Student("Priya", 87)
print(s1.name, s1.percentage())
print(math.ceil(s1.percentage()))

Output:

Priya 87.0
87

Solution: इस program के identifiers और वे किसके नाम हैं:

  • math: module का नाम
  • MAX_MARKS: constant की तरह use होने वाला variable (सब capital, PEP 8 के हिसाब से)
  • Student: class का नाम (CapWords style)
  • __init__: special (dunder) method का नाम, जो object बनते समय अपने आप चलता है
  • percentage: method (function) का नाम
  • self, name, marks: parameters के नाम
  • s1: object (variable) का नाम
  • print और ceil: built-in और module function के नाम। ये भी identifiers हैं, बस पहले से बने हुए हैं।

इस program के keywords हैं: import, class, def और return। ये नाम नहीं, instructions हैं, इसलिए इन्हें identifier नहीं गिना जाता। "Priya", 87 और 100 literals (values) हैं, identifiers नहीं।

Advertisement

Identifier और Keyword में अंतर

PointIdentifierKeyword
कौन बनाता हैProgrammer खुदPython में पहले से तय
कामVariable, function, class आदि को नाम देनाPython को खास instruction देना (loop, condition, function बनाना)
संख्याजितने चाहें उतनेFixed (Python 3.12 में 35)
Exampletotal, Student, get_dataif, for, def, class

Underscore वाले Identifiers का मतलब

PatternExampleमतलब (convention)
Single leading underscore_value"Internal use के लिए", यानी बाहर से use न करें। from module import * करने पर ऐसे नाम import नहीं होते।
Double leading underscore (class के अंदर)__salaryName mangling: Python इसका नाम बदलकर _ClassName__salary कर देता है, ताकि subclass में गलती से overwrite न हो।
दोनों तरफ double underscore__init__, __name__Python के special (dunder) नाम। अपने नए नाम ऐसे न बनाएँ।
Trailing underscoreclass_Keyword जैसा नाम चाहिए तो आखिर में _ लगा दें।

अच्छे Identifier चुनने के Tips (Naming Conventions)

ये Python की style guide PEP 8 की सलाह है। इन्हें तोड़ने पर error नहीं आता, पर code पढ़ना मुश्किल हो जाता है।

  • Meaningful नाम: x की जगह total, average, student_count।
  • Variables और functions: small letters और underscore (snake_case): employee_name, get_total()।
  • Classes: हर शब्द का पहला अक्षर capital (CapWords): StudentRecord।
  • Constants: सब capital: MAX_MARKS = 100।
  • Built-in नाम पर variable न बनाएँ: list, print, sum, str keywords नहीं हैं, इसलिए Python इन्हें नाम बनाने देता है, पर इससे असली function छिप जाता है।

Common Mistake: Built-in function का नाम use करना

list = [1, 2]
print(list((3, 4)))

Output (error की आखिरी line):

TypeError: 'list' object is not callable

पहली line में list अब एक variable बन गया, इसलिए list() function काम नहीं करता। नाम numbers या marks_list रखें। Python का type कैसे runtime पर तय होता है, यह Dynamic Typing in Hindi में पढ़ें।

अच्छे Identifiers के फायदे

  • Readability: नाम से ही पता चल जाता है कि variable या function क्या करता है।
  • Easy debugging और maintenance: बड़े code में सही चीज़ जल्दी मिलती है और बदलना आसान होता है।
  • Teamwork: दूसरे programmers आपका code बिना पूछे समझ लेते हैं।
  • Customizability: Programmer अपनी ज़रूरत के हिसाब से नाम रख सकता है।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Python में identifier क्या है? Example दीजिए।
  2. _marks, 2marks, marks_2, while में से कौन valid है? (_marks और marks_2)
  3. Python identifiers case-sensitive हैं, इसका क्या मतलब है?
  4. Identifier और keyword में दो अंतर लिखिए।

Long Questions (5–10 marks)

  1. Python में identifier बनाने के सभी rules valid और invalid examples के साथ लिखिए।
  2. Identifiers के लिए naming conventions (PEP 8) समझाइए। Underscore वाले identifiers का मतलब बताइए।
  3. एक program लिखिए जो check करे कि कोई नाम valid identifier है या नहीं।

FAQs

Python में identifier की maximum length कितनी है?

Python में कोई limit नहीं है। फिर भी नाम छोटा और साफ़ रखें, जैसे student_count।

क्या identifier में $ लगा सकते हैं?

नहीं। Python में identifier में सिर्फ letters, digits और underscore चलते हैं। $ लगाने पर SyntaxError आता है।

क्या print या list को variable का नाम बना सकते हैं?

बना सकते हैं, क्योंकि ये keywords नहीं बल्कि built-in functions हैं। पर ऐसा करने से असली function उस program में काम नहीं करेगा, इसलिए यह गलत practice है।

Class में लिखा self keyword है या identifier?

self एक identifier है, keyword नहीं। यह बस एक convention है कि method का पहला parameter self नाम से लिखा जाए। आप कोई और नाम भी रख सकते हैं और program चलेगा, पर हर Python programmer self ही लिखता है, इसलिए आप भी वही लिखें।

Identifier और variable एक ही चीज़ हैं?

नहीं। Variable memory में रखी एक value का नाम है, और वह नाम identifier है। पर identifier सिर्फ variable का नहीं, function, class और module का नाम भी होता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Python में identifier वह नाम है जो आप किसी variable, function, class या module को देते हैं, ताकि program में उसे पहचाना जा सके।
Python Notes in Hindi | पायथन नोट्स हिंदी में के पाठ्यक्रम में What are Identifiers in Python? Rules और Examples (Hindi) एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको What are Identifiers in Python? Rules और Examples (Hindi) के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement