Python Variables के नाम
In this page:
valid_name = value # letters, digits, underscore; cannot start with a digit
_private_name = value
नामकरण के बुनियादी नियम
Python का वैध variable नाम किसी अक्षर (a-z, A-Z) या underscore (_) से शुरू होना चाहिए, और पहले के बाद हर character अक्षर, अंक या underscore हो सकता है -- नाम में कहीं भी space, hyphen या अन्य punctuation की अनुमति नहीं है।
उदाहरण: The Basic Naming Rules
_valid_name = 1 # starts with an underscore, which is allowed
name2 = 2 # letters followed by a digit, also allowed
print(_valid_name, name2)
नाम Reserved Keywords नहीं हो सकते
Python कुछ शब्द (जैसे if, else, for, while, class, def, return, और True/False/None) केवल भाषा की अपनी syntax के लिए आरक्षित रखती है -- इनमें से किसी को variable नाम बनाने की कोशिश SyntaxError देती है, क्योंकि Python नहीं बता सकती कि आपका मतलब keyword से है या variable से।
उदाहरण: Names Cannot Be Reserved Keywords
import keyword
print(keyword.iskeyword("class")) # True, "class" is reserved
class_ = "workaround" # trailing underscore avoids clashing with the keyword
print(class_)
Case Sensitivity
Python बड़े और छोटे अक्षरों को पूरी तरह अलग मानती है, यानी age, Age और AGE तीन अलग, स्वतंत्र variables हैं जो एक ही समय में अलग-अलग value रख सकते हैं -- यह ब्योरा bugs से बचने और नामकरण परंपराओं का लगातार पालन करने, दोनों के लिए मायने रखता है।
उदाहरण: Case Sensitivity
age = 25
Age = 30
AGE = 35 # age, Age, and AGE are three separate variables
print(age, Age, AGE)
Python नामकरण परंपराएँ (PEP 8)
सख्त वैधता के नियमों से आगे, Python की official style guide (PEP 8) विशिष्ट नामकरण परंपराएँ सुझाती है: variables और functions के लिए snake_case (underscores वाले छोटे अक्षर), constants के लिए UPPER_SNAKE_CASE, और class नामों के लिए PascalCase -- इनका पालन आपके code को व्यापक Python समुदाय के साथ तुरंत पहचानने योग्य और एकसमान बनाता है।
उदाहरण: Python Naming Conventions (PEP 8)
user_count = 5 # snake_case
MAX_RETRIES = 3 # UPPER_SNAKE_CASE
class UserAccount: # PascalCase
pass
print(user_count, MAX_RETRIES)
बहु-शब्द नाम: पठनीयता मायने रखती है
जब कोई variable एक से ज़्यादा अवधारणाएँ दर्शाता है (जैसे किसी उपयोगकर्ता के order की कुल कीमत), तो शब्दों को underscores से जोड़ना (total_order_price) बजाय उन्हें सटाकर लिखने (totalorderprice) या अस्पष्ट संक्षेप (torpr) बनाने के, नाम को वैध भी रखता है और बाद में code पढ़ने वाले के लिए सचमुच पठनीय भी, भविष्य के आपके लिए भी।
उदाहरण: Multi-Word Names: Readability Matters
total_order_price = 49.99 # clear
print(total_order_price)
- variable name को digit से शुरू करना (जैसे 2cats), जिसे Python सीधे SyntaxError के साथ reject कर देता है, क्योंकि names letter या underscore से शुरू होने चाहिए।
- किसी reserved keyword (जैसे class, if, या return) को variable name की तरह इस्तेमाल करना, जो भी SyntaxError produce करता है क्योंकि keywords खुद भाषा के लिए reserved हैं।
- यह भूल जाना कि Python variable names case-sensitive होते हैं -- age, Age, और AGE तीन पूरी तरह अलग variables हैं, एक ही नहीं।
- variable name letter या underscore से शुरू होना चाहिए, और उसके बाद सिर्फ letters, digits, और underscores रख सकता है।
- Variable names Python के reserved keywords (जैसे if, for, class, या True) में से कोई नहीं हो सकते।
- Variable names case-sensitive होते हैं, इसलिए myVar और myvar दो पूरी तरह अलग variables को refer करते हैं।
Python variable naming rules core language syntax का हिस्सा हैं और हर Python version और environment में समान रूप से लागू होते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: