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