Python Keywords और Identifiers
In this page:
Keywords
Keywords वे शब्द हैं जिन्हें Python interpreter अपनी syntax के लिए आरक्षित रखता है — if, class, for, def और लगभग 30 अन्य — और इनमें से किसी को variable नाम बनाने पर SyntaxError उठता है, क्योंकि interpreter आपके इरादे और अपने व्याकरण में फ़र्क नहीं कर पाता।
उदाहरण: Keywords
# 'class' is a reserved keyword
# class = 5 # SyntaxError: invalid syntax
print("keywords cannot be used as variable names")
Identifiers
Identifier वह कोई भी नाम है जो आप variable, function या class के लिए चुनते हैं। जो नाम बताएँ कि वे क्या रखते हैं (x के बजाय user_count), उनसे code खुद अपनी व्याख्या करता है, और वे comments कम हो जाते हैं जो आपको वरना लिखने पड़ते।
उदाहरण: Identifiers
user_count = 42
print(user_count)
नामकरण के नियम
Identifiers किसी अक्षर या underscore से शुरू होने चाहिए और उसके बाद केवल अक्षर, अंक और underscores हो सकते हैं — कोई space, hyphen, या $ या @ जैसे symbols नहीं, जिन्हें Python पूरी तरह दूसरी syntax के लिए आरक्षित रखती है।
उदाहरण: 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)
नामों में Case Sensitivity
Python page और Page को दो बिलकुल अलग नाम मानती है, क्योंकि पूरी भाषा में identifier की मिलान case-sensitive होती है।
एक अकेली गलत capitalization NameError bugs का आम स्रोत है, जिसे आँख से पकड़ना उलझन भरा हो सकता है।
उदाहरण: Case Sensitivity in Names
page = "lowercase" # lowercase variable name
Page = "uppercase" # capitalized name is a completely different variable
print(page)
print(Page)
नामों के सर्वोत्तम तरीके
छोटे-मोटे संक्षिप्त रूपों या एक अक्षर वाले नामों की जगह पूरे, वर्णनात्मक शब्द चुनिए (केवल बहुत अल्पकालिक loop counters जैसे i को छोड़कर), और variables तथा functions के लिए Python की snake_case परंपरा का पालन कीजिए ताकि आपका code बाकी ecosystem के साथ एकसमान पढ़ा जाए।
उदाहरण: Best Practices for Names
total_price = 19.99 # descriptive snake_case name for a price value
for i in range(3): # short name "i" is fine for a throwaway loop counter
print(i)
print(total_price)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: