Python Variables: naming, dynamic declaration, comments, multiple assignment, global variables

Python variables data के लिए लचीले name-tags की तरह काम करते हैं, आपको उन्हें तुरंत declare करने, कई values एक साथ link करने, या functions में globally साझा करने देते हैं।

11 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

ग़ायब होता Data Type Declaration

आपके Semester 1 BCA104 C language labs में, int total = 100; लिखना अनिवार्य था। अगर आप बाद में total = "Overspent"; assign करने की कोशिश करते, compiler भड़क जाता। Python आपको balance = 500 तुरंत बिना किसी datatype prefix के लिखने क्यों देता है, और फिर उसी variable को अगली ही line पर एक शब्द या एक decimal में साफ़-सुथरा बदलने देता है? आइए उजागर करें कि Python में variables पर्दे के पीछे कैसे काम करते हैं।

Theory

Sticky Labels बनाम Ice Trays

C में एक variable को एक ice tray slot की तरह सोचिए: इसका एक fixed size और आकार सीधे memory में ढला है। आप इसमें सिर्फ़ पानी (integers) डाल सकते हैं। एक Python variable एक sticky paper label की तरह है जिस पर एक नाम लिखा है। आप label को एक whole number block से छील सकते हैं और इसे सीधे एक text string block पर चिपका सकते हैं। data का एक type होता है, पर label ख़ुद किसी भी चीज़ पर चिपक सकता है!

Theory

Name Game के नियम

एक Python variable computer memory में एक object की ओर इशारा करता एक named reference है। Dynamic declaration का मतलब है एक variable ठीक उसी पल बनता है जब आप इसे = operator इस्तेमाल करके एक value assign करते हैं, पहले से कोई data type निर्दिष्ट किए बिना। Python variables सख़्त identifier naming नियमों का पालन करते हैं: उन्हें एक letter या एक underscore से शुरू होना होगा, एक digit से शुरू नहीं हो सकते, और बहुत case-sensitive हैं (balance और Balance अलग हैं)।

At a glance

Table 1: Python में ज़रूरी variable syntax और scope modifiers।

Variable Syntax FeaturePython में यह कैसे काम करता हैPocketMoney उदाहरण
Dynamic Declarationकोई type initialization नहीं, assignment पर बनता है।balance = 1000
Multiple Assignmentएक value कई को assign करें, या कई को एक साथ unpack करें।init_bal, reward = 500, 50
Commentsdocumentation के लिए hash symbol # इस्तेमाल करता है।# Updates monthly expense
Global Keywordएक local block variable को top scope का घोषित करता है।global balance

Theory

एक Worked Example: PocketMoney Ledger सँभालना

आइए अपने चलते project data पर multiple assignments और functional modifications लागू करें। हम values एक साथ initialize करेंगे, साफ़ comments इस्तेमाल करते हुए अपने कामों को document करेंगे, और एक functional scope के अंदर एक global tracker modify करेंगे।

Practical

PocketMoney Variables और Scope Ledger

# Initialize multi-variable transactions on one line
samosa_cost, chai_cost = 30, 15
balance = 500  # Top-level global balance

def record_spend():
    global balance  # Gain permission to modify top-level variable
    total_spend = samosa_cost + chai_cost
    balance = balance - total_spend

record_spend()
print(balance)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Variable Modifications को ट्रेस करें

ऊपर के Python script का मन में विश्लेषण कीजिए। execution के अंत में balance variable के अंदर कौन सी बिल्कुल value रहती है? tap करने से पहले lines ट्रेस कीजिए।

Show the answer

output होगा:

455

क्यों? Line 2 samosa_cost को 30 और chai_cost को 15 assign करती है। Line 3 global balance को 500 पर बनाती है। function के अंदर, total_spend 45 बन जाता है (30 + 15)। 'global balance' keyword भीतरी balance modification को सीधे बाहरी variable से जोड़ता है, 500 - 45 को update करके 455 छोड़ते हुए।

Quiz

university exam मानकों के अनुसार इनमें से कौन सा एक अमान्य variable identifier नाम है?

  1. _monthly_spend
  2. pocketMoney2026
  3. 2way_split
  4. Total_Amount
Show the answer

2way_split

Variable names में numbers हो सकते हैं पर कभी एक digit से शुरू नहीं होने चाहिए। इसलिए, '2way_split' naming नियमों का उल्लंघन करता है और एक syntax error पैदा करता है।

Quiz

क्या होता है अगर आप पहले 'global' keyword लिखे बिना एक function के अंदर एक बाहरी variable modify करते हैं?

  1. Python बाहरी variable को सामान्य रूप से बदलता है।
  2. Python उसी नाम का एक नया local variable बनाता है, बाहरी variable को अपरिवर्तित छोड़ते हुए।
  3. script शुरू होने से पहले एक compiler crash trigger करती है।
  4. variable अपने-आप एक string type में बदल जाता है।
Show the answer

Python उसी नाम का एक नया local variable बनाता है, बाहरी variable को अपरिवर्तित छोड़ते हुए।

global keyword के बिना, Python मानता है कि एक function के अंदर assign किया कोई भी variable एक बिल्कुल नया local variable है। बाहरी variable पूरी तरह अछूता रहता है, जो अक्सर logic bugs की ओर ले जाता है।

Watch out

Classic जाल: Unbound Local Errors

university laboratory exams में एक बड़ी marks-गँवाने वाली ग़लती इस तरह code लिखना है: balance = 500 और फिर एक def block के अंदर global balance घोषित किए बिना balance = balance - 50 लिखना। Python assignment देखता है, balance को उस function के लिए local mark करता है, और फिर एक UnboundLocalError फेंकता है क्योंकि यह local value को initialize होने से पहले पढ़ने की कोशिश करता है!

Theory

Variables को Semester 3 Modules से जोड़ना

Dynamic assignment शक्तिशाली है पर साफ़ आदतें माँगता है। Semester 3 (BCA303) में, जब Python loops के ज़रिए एक SQL query से dataset rows unpack करते हैं, आप date, category, price = row जैसे multiple assignments इस्तेमाल करेंगे ताकि जटिल data points को तुरंत एक ही line पर साफ़ single variables में parse करें।

Summary

Key takeaways

  • Variables को स्पष्ट type declarations की ज़रूरत नहीं होती और assignment पर बनते हैं।
  • Identifiers case-sensitive हैं और कभी numeric digits से शुरू नहीं होने चाहिए।
  • कई variables एक ही line पर commas इस्तेमाल करके एक साथ initialize किए जा सकते हैं।
  • hash character # single-line descriptions बनाता है जिन्हें interpreter पूरी तरह अनदेखा करता है।
  • user-defined functions के अंदर global variables modify करने के लिए एक स्पष्ट global declaration चाहिए।
  • Memory Hook: Labels सचेत रूप से scopes बदलते हैं!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Fundamentals

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Python Variables: naming, dynamic declaration, comments, multiple assignment, global variables · Programming Skills · Gri-Learn