रुपेश पवार
IT Professional 5+ वर्षांचा अनुभव
प्रकाशित
22 Feb’२०२६
वाचण्याचा वेळ
13 मिनिटे
स्तर
Beginner
Python शिकायला सुरुवात करायची असेल तर Variables हा पहिला आणि सर्वात महत्त्वाचा topic आहे! Variable म्हणजे काय, कसे बनवतात, Naming Rules, Dynamic Typing, Local vs Global Scope, Constants, Multiple Assignment – सर्व काही real-world projects सह मराठीत शिका. Variables समजले म्हणजे Python चा पाया पक्का झाला!
Table of Contents
Python Variable म्हणजे काय? | Actual Python Variable Meaning Marathi
कल्पना करा की तुमच्या घरात एक कपाट आहे. त्या कपाटाच्या प्रत्येक खणाला एक नाव दिले आहे — “Books”, “Clothes”, “Documents”. जेव्हा तुम्हाला books हवे असतात तेव्हा त्या खणाचे नाव सांगता. Computer मध्ये Variables हे असेच काम करतात!
Variable म्हणजे काय?
Python Variable म्हणजे computer च्या memory (RAM) मध्ये data store करण्यासाठी दिलेले नाव (label). Variable एक container आहे जो value ठेवतो. त्या value ला नाव देऊन आपण नंतर कधीही त्या नावाने value मिळवू शकतो, बदलू शकतो, किंवा calculations करू शकतो.
वास्तविक जीवनाशी तुलना
| वास्तविक जीवन | Python Variable | Code |
|---|---|---|
| विद्यार्थ्याचे नाव | string variable | नाव = “राहुल” |
| वय | integer variable | वय = 21 |
| सरासरी marks | float variable | सरासरी = 87.5 |
| Active member आहे का? | boolean variable | is_active = True |
| आवडती फळे | list variable | फळे = [“आंबा”,”केळे”] |
| Bank balance | float variable | balance = 15250.75 |
Variables का वापरतात? | 5 महत्त्वाची कारणे
1. Data Store करणे: User input, calculations results, file data — सर्व temporary memory मध्ये ठेवण्यासाठी variables जरुरी.
2. Reuse करणे: एकदा variable मध्ये value ठेवली की program मध्ये कुठेही त्याचा वापर करता येतो
3. Code Readable करणे: tax_rate = 0.18 हे 0.18 पेक्षा जास्त readable आणि maintainable आहे.
4. Calculations: Variables मध्ये values ठेवून mathematical operations, comparisons करता येतात.
5. Easy Update: एका ठिकाणी variable बदलला की पूर्ण program मध्ये बदल होतो — प्रत्येक ठिकाणी manually बदलायची गरज नाही.
6.Program Flexible: Variables मुळे program different inputs साठी वेगळ्या प्रकारे काम करू शकतो.
Variable Declare कसे करावे?
Python मध्ये variable बनवणे खूप सोपे आहे — फक्त नाव = value इतकेच! C, Java सारख्या languages मध्ये type सांगावा लागतो (int x = 10;) पण Python मध्ये तसे करावे लागत नाही.
# Python मध्ये Variable बनवणे — खूप सोपे!
नाव = "रुपेश पवार" # String variable
वय = 28 # Integer variable
उंची = 5.8 # Float variable
is_developer = True # Boolean variable
skills = ["Python", "Django", "SQL"] # List variable
# Variable वापरणे
print(f"नाव: {नाव}")
print(f"वय: {वय} वर्षे")
print(f"उंची: {उंची} फूट")
print(f"Developer: {is_developer}")
print(f"Skills: {', '.join(skills)}")
# Variable ची value बदलता येते
वय = 29 # वाढदिवस! वय update केले
print(f"\nआता वय: {वय} वर्षे")नाव: रुपेश पवार वय: 28 वर्षे उंची: 5.8 फूट Developer: True Skills: Python, Django, SQL आता वय: 29 वर्षे
Python Variable Declaration चे 3 विशेष गुण:
1. No type declaration — int, string असे आधी सांगायची गरज नाही
2. Dynamic — एकाच variable मध्ये वेगळ्या types च्या values ठेवता येतात
3. Simple syntax — फक्त variable_name = value
Variable Naming Rules आणि Best Practices
Variable चे नाव ठेवताना Python चे काही नियम पाळावे लागतात. हे Interview मध्ये खूप विचारले जातात!
1. Valid Variable Names
नाव— Marathi letter, student_name— underscore, marks1 — letter + number, _private— underscore first, totalMarks— camelCase, MAX_SIZE— constants, x— single letter.
2. Invalid Variable Names
1name— number first, my-name— hyphen allowed नाही, my name— space allowed नाही, if— keyword वापरू नका, for— keyword, my@name— special char, class— keyword
# ✅ Valid — चांगल्या naming conventions
student_name = "प्रिया" # snake_case (recommended)
totalMarks = 450 # camelCase
MAX_STUDENTS = 60 # UPPER_CASE (constants)
_internal_var = "private" # underscore = private convention
# Case Sensitive! — हे तिन्ही वेगळे variables आहेत
count = 10
Count = 20
COUNT = 30
print(count, Count, COUNT) # 10 20 30
# Python Variable Naming Styles
# snake_case → Python recommended (PEP 8)
first_name = "राहुल"
last_name = "पाटील"
total_marks = 485
# f-string वापरून print करा
print(f"विद्यार्थी: {first_name} {last_name}")
print(f"Marks: {total_marks}/500")10 20 30 विद्यार्थी: राहुल पाटील Marks: 485/500
Case sensitivity लक्षात ठेवा!
Python मध्ये name, Name, आणि NAME हे तीन वेगळे variables आहेत! Beginners ची सर्वात सामान्य चूक म्हणजे case चुकीचा वापरणे — NameError: name ‘Name’ is not defined असा error येतो.
Dynamic Typing म्हणजे काय? | Python ची खासियत
Python ही dynamically typed language आहे. याचा अर्थ असा की variable बनवताना त्याचा type (int, string, float) आधी सांगायची गरज नाही — Python आपोआप ओळखतो. शिवाय एकाच variable मध्ये वेगळ्या types च्या values ठेवता येतात.
# Dynamic Typing — एकाच variable मध्ये वेगळे types
x = 10
print(f"x = {x}, type = {type(x)}") # int
x = "Hello"
print(f"x = {x}, type = {type(x)}") # str
x = 3.14
print(f"x = {x}, type = {type(x)}") # float
x = [1, 2, 3]
print(f"x = {x}, type = {type(x)}") # list
# तुलना: Java/C मध्ये type fix असतो
# int x = 10; → x = "Hello" → ERROR!
# Python मध्ये असे होत नाही — flexible आहे!
print("\n--- Python vs C/Java ---")
print("Python: type आपोआप ठरतो (Dynamic)")
print("Java: type आधीच सांगावा लागतो (Static)")x = 10, type = <class 'int'> x = Hello, type = <class 'str'> x = 3.14, type = <class 'float'> x = [1, 2, 3], type = <class 'list'> --- Python vs C/Java --- Python: type आपोआप ठरतो (Dynamic) Java: type आधीच सांगावा लागतो (Static)
Variable Types – Data Types सह
Variable मध्ये कोणत्या प्रकारची value आहे त्यावर त्याचा type ठरतो. Python मध्ये 8 मुख्य data types आहेत:
# 1. Integer (int) — पूर्ण संख्या
वय = 22
students = 1500
temperature = -5
# 2. Float — दशांश संख्या
marks_percentage = 87.5
pi = 3.14159
price = 299.99
# 3. String (str) — मजकूर
नाव = "रुपेश पवार"
शहर = 'पुणे'
message = f"नमस्कार {नाव}!"
# 4. Boolean (bool) — True/False
is_pass = True
is_admin = False
# 5. List — बदलता येणारी यादी
विषय = ["Python", "Math", "Science"]
# 6. Tuple — fix यादी
gps = (18.52, 73.85)
# 7. Dictionary — key-value
student = {"नाव": "प्रिया", "marks": 92}
# 8. Set — unique values
unique_cities = {"पुणे", "मुंबई", "नाशिक"}
# सर्वांचे types print करा
for var_name, var_val in [
("वय", वय), ("percentage", marks_percentage),
("नाव", नाव), ("is_pass", is_pass),
("विषय", विषय), ("gps", gps)
]:
print(f"{var_name}: {type(var_val).__name__}")वय: int percentage: float नाव: str is_pass: bool विषय: list gps: tuple
Multiple Assignment – एकाच वेळी अनेक Variables
Python मध्ये एकाच line मध्ये अनेक variables assign करता येतात. हे code लहान आणि readable करते.
# 1. Multiple values — एकाच वेळी
x, y, z = 10, 20, 30
print(x, y, z) # 10 20 30
# 2. Same value — सर्वांना एकच value
a = b = c = 100
print(a, b, c) # 100 100 100
# 3. Swap — दोन variables ची values बदलणे
नाव1 = "राहुल"
नाव2 = "प्रिया"
print(f"Swap आधी: {नाव1}, {नाव2}")
नाव1, नाव2 = नाव2, नाव1 # Python magic swap!
print(f"Swap नंतर: {नाव1}, {नाव2}")
# 4. Unpacking — list/tuple मधून values
student_info = ["अमित", 21, "Engineering"]
नाव, वय, शाखा = student_info
print(f"\n{नाव} | {वय} वर्षे | {शाखा}")
# 5. Augmented assignment
score = 0
score += 10 # score = score + 10
score += 25
score -= 5
print(f"\nFinal Score: {score}")10 20 30 100 100 100 Swap आधी: राहुल, प्रिया Swap नंतर: प्रिया, राहुल अमित | 21 वर्षे | Engineering Final Score: 30
Variable Scope – Local vs Global
Variable Scope म्हणजे variable कुठे वापरता येतो याची मर्यादा. Python मध्ये मुख्यतः दोन scopes असतात — Local (function च्या आत) आणि Global (सर्वत्र).
🔵 Local Variable : Function च्या आत बनवलेला variable. फक्त त्याच function मध्ये वापरता येतो. Function संपल्यावर नाहीसा होतो. उदा: def func(): x = 10
🟢 Global Variable: Function च्या बाहेर बनवलेला variable. पूर्ण program मध्ये कुठेही वापरता येतो. Program संपेपर्यंत जगतो. उदा: x = 10 # globally
# Global variable — सर्वत्र available
school_name = "ज्ञानदीप हायस्कूल" # Global
total_students = 500 # Global
def class_info():
# Local variable — फक्त या function मध्ये
class_name = "10th A" # Local
students_in_class = 45 # Local
# Global variable वाचता येतो
print(f"शाळा: {school_name}")
print(f"वर्ग: {class_name} ({students_in_class} विद्यार्थी)")
def update_count(नवी_संख्या):
# Global variable बदलायचे असेल तर 'global' keyword
global total_students
total_students = नवी_संख्या
print(f"Updated: {total_students} students")
class_info()
print(f"\nएकूण विद्यार्थी: {total_students}")
update_count(520)
print(f"Updated Total: {total_students}")
# class_name वापरायचा try केला तर NameError येईल
# print(class_name) → NameError: name 'class_name' not defineशाळा: ज्ञानदीप हायस्कूल वर्ग: 10th A (45 विद्यार्थी) एकूण विद्यार्थी: 500 Updated: 520 students Updated Total: 520
Constants – न बदलणाऱ्या Values
Constants म्हणजे ज्या values program मध्ये कधीच बदलत नाहीत. Python मध्ये technically constants नाहीत, पण UPPER_CASE naming convention वापरून developers constants दाखवतात.
# Constants — UPPER_CASE convention
PI = 3.14159265358979
MAX_STUDENTS = 60
MIN_AGE = 18
WEBSITE_URL = "https://lgrow.in"
TAX_RATE = 0.18 # 18% GST
PASSING_MARKS = 35
# Constants वापरून calculations
product_price = 1000
gst = product_price * TAX_RATE
total = product_price + gst
print(f"Product: ₹{product_price}")
print(f"GST ({TAX_RATE*100:.0f}%): ₹{gst:.0f}")
print(f"Total: ₹{total:.0f}")
# Circle area using PI constant
radius = 7
area = PI * radius ** 2
print(f"\nवर्तुळाचे क्षेत्रफळ (r={radius}): {area:.2f}")
Product: ₹1000 GST (18%): ₹180 Total: ₹1180 वर्तुळाचे क्षेत्रफळ (r=7): 153.94
type() आणि id() – Variable तपासणे
# type() — variable चा data type
variables = [42, 3.14, "Python", True, [1,2], (3,4), {"a":1}]
print("=== type() Function ===")
for v in variables:
print(f" {str(v):20} → {type(v).__name__}")
# id() — variable चा memory address
a = 1000
b = a # b हे a चाच reference
c = 1000 # c हे वेगळे object
print("\n=== id() Function ===")
print(f"a = {a}, id = {id(a)}")
print(f"b = {b}, id = {id(b)}") # a सारखाच
print(f"c = {c}, id = {id(c)}") # वेगळा
# isinstance() — type check करणे
print("\n=== isinstance() Function ===")
print(f"42 int आहे का: {isinstance(42, int)}")
print(f"3.14 float आहे का: {isinstance(3.14, float)}")
print(f"'Hi' str आहे का: {isinstance('Hi', str)}")
print(f"42 str आहे का: {isinstance(42, str)}")=== type() Function ===
42 → int
3.14 → float
Python → str
True → bool
[1, 2] → list
(3, 4) → tuple
{'a': 1} → dict
=== id() Function ===
a = 1000, id = 140234567890
b = 1000, id = 140234567890
c = 1000, id = 140234567920
=== isinstance() Function ===
42 int आहे का: True
3.14 float आहे का: True
'Hi' str आहे का: True
42 str आहे का: FalseReal-World Project – Student Report System
आपण शिकलेले सर्व Variable concepts एकत्र वापरून एक Student Report Management System बनवूया:
# === Student Report System ===
# Constants
SCHOOL_NAME = "ज्ञानदीप हायस्कूल, पुणे"
MAX_MARKS = 100
PASSING_MARKS = 35
SCHOLARSHIP_CUTOFF = 85.0
TOTAL_SUBJECTS = 5
# Global variable
total_students_processed = 0
def generate_report(नाव, roll_no, marks_list):
global total_students_processed # Global update
# Local variables
एकूण = sum(marks_list)
सरासरी = एकूण / TOTAL_SUBJECTS
सर्व_pass = all(m >= PASSING_MARKS for m in marks_list)
# Multiple assignment
सर्वोच्च, सर्वात_कमी = max(marks_list), min(marks_list)
# Conditional variables
निकाल = "PASS" if सर्व_pass else "FAIL"
scholarship = सरासरी >= SCHOLARSHIP_CUTOFF and सर्व_pass # Boolean
if सरासरी >= 90: grade = "A+"
elif सरासरी >= 80: grade = "A"
elif सरासरी >= 70: grade = "B"
elif सरासरी >= 60: grade = "C"
else: grade = "F"
total_students_processed += 1 # Global update
print(f"\n{'='*45}")
print(f" {SCHOOL_NAME}")
print(f"{'='*45}")
print(f" नाव: {नाव:<20} Roll: {roll_no}")
print(f"{'-'*45}")
विषय_नावे = ["गणित", "विज्ञान", "इंग्रजी", "मराठी", "इतिहास"]
for विषय, marks in zip(विषय_नावे, marks_list):
status = "✓" if marks >= PASSING_MARKS else "✗"
print(f" {status} {विषय:<10}: {marks}/{MAX_MARKS}")
print(f"{'-'*45}")
print(f" एकूण: {एकूण}/{MAX_MARKS*TOTAL_SUBJECTS} | सरासरी: {सरासरी:.1f}%")
print(f" Grade: {grade} | निकाल: {निकाल}")
print(f" Scholarship: {'✓ पात्र' if scholarship else '✗ अपात्र'}")
# Function calls
generate_report("प्रिया शर्मा", 101, [92, 88, 95, 87, 90])
generate_report("राहुल पाटील", 102, [78, 65, 72, 80, 68])
print(f"\nएकूण Reports: {total_students_processed}")============================================= ज्ञानदीप हायस्कूल, पुणे ============================================= नाव: प्रिया शर्मा Roll: 101 --------------------------------------------- ✓ गणित : 92/100 ✓ विज्ञान : 88/100 ✓ इंग्रजी : 95/100 ✓ मराठी : 87/100 ✓ इतिहास : 90/100 --------------------------------------------- एकूण: 452/500 | सरासरी: 90.4% Grade: A+ | निकाल: PASS Scholarship: ✓ पात्र ============================================= ज्ञानदीप हायस्कूल, पुणे ============================================= नाव: राहुल पाटील Roll: 102 --------------------------------------------- ✓ गणित : 78/100 ✓ विज्ञान : 65/100 ✓ इंग्रजी : 72/100 ✓ मराठी : 80/100 ✓ इतिहास : 68/100 --------------------------------------------- एकूण: 363/500 | सरासरी: 72.6% Grade: B | निकाल: PASS Scholarship: ✗ अपात्र एकूण Reports: 2
Beginners च्या सामान्य चुका
1. Number ने Variable सुरू करणे: 1name = “राहुल” → SyntaxError! Variable नेहमी letter किंवा underscore ने सुरू झाला पाहिजे. name1 ✓
2. Case Sensitivity विसरणे: Name आणि name वेगळे variables! Name = “राहुल” define केले आणि print(name) लिहिले → NameError येतो.
3. Undefined Variable वापरणे: Variable define केल्याशिवाय वापरणे → NameError. Variable नेहमी आधी define करा, मग वापरा.
4. Global Variable चुकीचे Update: Function मध्ये global variable बदलायचे असेल तर global keyword जरुरी. नाहीतर local variable बनतो आणि global बदलत नाही.
5. Keywords वापरणे: if = 5, for = 10 → SyntaxError! Python keywords variable नाव म्हणून वापरता येत नाहीत. keyword.iskeyword(“word”) वापरून check करा.
6. Best Practice:
Meaningful names वापरा: x = 22 ऐवजी student_age = 22. snake_case convention वापरा. Constants साठी UPPER_CASE वापरा.
Practice Problems – स्वतः सोडवा
1. Personal Profile Card
तुमची स्वतःची Personal Profile Card बनवा. Variables वापरून नाव, वय, शहर, शिक्षण, आवडी (list), skills (list), आणि contact (dictionary) store करा. सर्व f-strings वापरून formatted print करा.
2. Temperature Converter
Variables वापरून temperature converter बनवा. Constants: CELSIUS_TO_FAHRENHEIT = 9/5, OFFSET = 32. User कडून Celsius घेऊन Fahrenheit, Kelvin, आणि Rankine मध्ये convert करा. Multiple assignment वापरा.
3. Scope Experiment
3 functions बनवा: (1) global variable read करणारी, (2) global variable update करणारी (global keyword), (3) local variable बनवणारी. प्रत्येकानंतर global variable print करा आणि बघा काय होते.
4. Variable Type Tracker
एक program बनवा जो user कडून 5 values घेतो (input() वापरून). प्रत्येक value साठी type() वापरून data type print करा. Int convert करण्याचा प्रयत्न करा — ValueError handle करा. isinstance() वापरून validation करा.
Python Variable – सामान्य प्रश्न (FAQ)
Q1. Python Variable Declare कसे करावे?
Ans: Python मध्ये variable declare करणे खूप सोपे आहे: फक्त variable_name = value लिहा. C किंवा Java सारखे type सांगायची गरज नाही. उदाहरणे: वय = 22, नाव = “राहुल”, price = 299.99, is_active = True. Python automatically type ओळखतो — याला Dynamic Typing म्हणतात.
Q2. Python Variable Naming Rules काय आहेत?
Ans: Python variable naming साठी 5 मुख्य rules: (1) Letter किंवा underscore (_) ने सुरू झाला पाहिजे — number ने नाही. (2) Letters, numbers, underscores allowed — spaces, hyphens, @, # नाहीत. (3) Python keywords (if, for, while, class) variable नाव म्हणून वापरता येत नाहीत. (4) Python case-sensitive आहे — name, Name, NAME हे तीन वेगळे variables. (5) snake_case convention recommended — student_name, total_marks.
Q3. Dynamic Typing म्हणजे काय?
Ans: Dynamic Typing म्हणजे Python मध्ये variable चा type आधी declare करायची गरज नसते — Python आपोआप value बघून type ठरवतो. शिवाय एकाच variable मध्ये वेगळ्या types च्या values ठेवता येतात: x = 10; x = “Hello”; x = [1,2,3] — हे सर्व valid आहे. Java/C मध्ये type fix असतो — int x = 10; x = “Hello” → Error!
Q4. Local आणि Global Variable मध्ये काय फरक आहे?
Ans: Local Variable: Function च्या आत define केलेला — फक्त त्याच function मध्ये available. Function संपल्यावर नाहीसा होतो. Global Variable: Function च्या बाहेर define केलेला — पूर्ण program मध्ये available.
Function मध्ये global variable read करता येतो, पण बदलायचा असेल तर global variable_name लिहावे लागते. जास्त global variables वापरणे avoid करा — code complex होतो.
Q5. Python मध्ये Constants कसे बनवतात?
Ans: Python मध्ये technically constants नाहीत — सर्व variables बदलता येतात. पण convention म्हणून UPPER_CASE naming वापरतो: PI = 3.14159, MAX_SIZE = 100, TAX_RATE = 0.18. UPPER_CASE म्हणजे “हे बदलू नका” असा signal इतर developers ना मिळतो. Python 3.8+ मध्ये Final type hint वापरून constant-like behavior मिळवता येते.
Q6. Variable interview मध्ये काय विचारतात?
Ans: Interview मध्ये या प्रश्नांची तयारी करा: (1) Variable म्हणजे काय? (2) Dynamic vs Static typing फरक, (3) Variable naming rules, (4) Local vs Global scope, (5) global keyword कधी वापरावे, (6) Multiple assignment कसे करतात, (7) type() vs isinstance() फरक, (8) Variable swap without temp variable कसे करतात, (9) Constants म्हणजे काय, (10) Python मध्ये variable delete कसे करतात (del keyword). MCQ साठी आमचे Python MCQ page visit करा!
पुढे काय शिकाल?


