Programming में Variable सबसे महत्वपूर्ण अवधारणाओं (Concepts) में से एक है। किसी भी Program में यदि हमें कोई संख्या, नाम, पता, अंक, मूल्य या अन्य जानकारी Store करनी हो, तो उसके लिए Variable का उपयोग किया जाता है।
Python में Variable बनाना बहुत आसान है क्योंकि इसमें Data Type पहले से घोषित (Declare) नहीं करना पड़ता। Python स्वयं Variable के Value को देखकर उसका Data Type पहचान लेता है। यही विशेषता Python को अन्य कई Programming Languages से अलग बनाती है।
इस अध्याय में हम Variables, Constants, Data Types, Dynamic Typing, Type Conversion और type() Function को विस्तार से समझेंगे।
Variable कंप्यूटर की Memory में बनाया गया एक नाम (Name) होता है, जिसमें किसी Value को Store किया जाता है।
जब Program चलता है, तो Variable उस Value को Memory में सुरक्षित रखता है ताकि आवश्यकता पड़ने पर उसे उपयोग किया जा सके।
name = "Rahul"
age = 21
marks = 88
यहाँ—
- name में "Rahul"
- age में 21
- marks में 88
Store किया गया है।
यदि Variables न हों, तो हर बार Value को सीधे Program में लिखना पड़ेगा।
Variables की सहायता से—
- Data Store किया जाता है।
- गणनाएँ (Calculations) की जाती हैं।
- User Input सुरक्षित रखा जाता है।
- Database से प्राप्त Data को Store किया जाता है।
- Programs अधिक Flexible बनते हैं।
Python में Variable बनाने के लिए अलग से कोई Keyword नहीं होता।
सीधे Variable Name लिखकर Value Assign कर दी जाती है।
city = "Delhi"
price = 599.50
is_student = True
Python में Variable Declare और Initialize एक साथ किए जाते हैं।
variable_name = value
student = "Ankit"
age = 20
salary = 35000
जब किसी Variable को Value दी जाती है, तो उसे Assignment कहते हैं।
Assignment Operator
=
x = 100
यहाँ = Assignment Operator है।
Python में एक साथ कई Variables बनाए जा सकते हैं।
a, b, c = 10, 20, 30
Output
print(a)
print(b)
print(c)
Same Value to Multiple Variables
x = y = z = 100
अब तीनों Variables में 100 Store होगा।
Python में Variable Name बनाते समय निम्न नियमों का पालन करना आवश्यक है।
✔ अक्षर (A-Z, a-z) से शुरू हो सकता है।
✔ Underscore (_) से शुरू हो सकता है।
✔ Numbers बाद में आ सकते हैं।
✔ Case Sensitive होता है।
❌ Number से शुरू नहीं हो सकता।
❌ Space नहीं हो सकता।
❌ Special Characters (@, %, #, &, *) का उपयोग नहीं किया जा सकता।
❌ Keyword का उपयोग नहीं किया जा सकता।
name
student_name
roll_no
marks2026
_price
1name
student-name
class
for
user@name
Name = "Amit"
name = "Rahul"
दोनों अलग-अलग Variables हैं।
Python में PEP 8 Style Guide के अनुसार Variables को snake_case में लिखना चाहिए।
student_name
total_marks
monthly_salary
Constant वह Value होती है जिसे Program के दौरान बदलना नहीं चाहिए।
Python में Constant बनाने के लिए कोई अलग Keyword नहीं है, लेकिन Convention के अनुसार Constant को UPPERCASE में लिखा जाता है।
PI = 3.14159
MAX_LIMIT = 100
GST_RATE = 18
|
Variable |
Constant |
|
Value बदल सकती है |
Value बदलनी नहीं चाहिए |
|
सामान्य नाम |
UPPERCASE नाम |
|
Program के दौरान बदल सकता है |
सामान्यतः स्थिर रहता है |
Python एक Dynamically Typed Language है।
इसका अर्थ है कि Variable का Data Type पहले से घोषित करने की आवश्यकता नहीं होती।
Python स्वयं पहचान लेता है।
x = 10
x = "Python"
x = 15.75
हर बार x का Data Type बदल गया।
Data Type बताता है कि Variable में किस प्रकार का Data Store किया गया है।
उदाहरण—
- Integer
- Decimal Number
- Text
- Boolean
- List
- Dictionary
Python में कई Built-in Data Types उपलब्ध हैं।
- int
- float
- complex
- str
- bool
- list
- tuple
- range
- set
- frozenset
- dict
- bytes
- bytearray
- memoryview
- NoneType
पूर्ण संख्याएँ।
age = 25
marks = 450
दशमलव संख्या।
price = 599.99
height = 5.8
x = 4 + 5j
मुख्यतः वैज्ञानिक गणनाओं में उपयोग।
Text Data Store करता है।
name = "Ekta Computer Notes"
केवल दो Values होती हैं।
True
False
उदाहरण
is_pass = True
Multiple Values Store करता है।
marks = [80, 90, 95]
List जैसा लेकिन Immutable।
colors = ("Red", "Blue", "Green")
Unique Values Store करता है।
numbers = {1,2,3,4}
Key और Value के रूप में Data Store करता है।
student = {
"name":"Rahul",
"age":20
}
जब किसी Variable में कोई Value नहीं होती।
data = None
Variable का Data Type जानने के लिए।
x = 50
print(type(x))
Output
<class 'int'>
name = "Ravi"
age = 22
salary = 25000.75
is_student = True
print(type(name))
print(type(age))
print(type(salary))
print(type(is_student))
Output
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
एक Data Type को दूसरे Data Type में बदलने की प्रक्रिया Type Casting कहलाती है।
x = int("100")
x = float(20)
x = str(100)
x = bool(1)
Python स्वयं Type बदल देता है।
x = 10
y = 5.5
print(x+y)
Output
15.5
Programmer स्वयं Type बदलता है।
x = "100"
print(int(x))
Memory में Variable कैसे Store होता है?
जब Variable बनाया जाता है, तो Python Memory में उसके लिए स्थान (Memory Location) निर्धारित करता है और Value Store कर देता है।
यदि वही Value दोबारा Assign की जाती है, तो Python Memory Optimization भी कर सकता है।
✔ Meaningful Variable Name रखें।
✔ snake_case का उपयोग करें।
✔ Constant हमेशा UPPERCASE में लिखें।
✔ एक Variable में एक ही प्रकार का Logical Data रखें।
✔ छोटे लेकिन स्पष्ट नाम रखें।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- Python Dynamic Typing को Support करता है।
- Variable Declare करने के लिए अलग Keyword नहीं होता।
- Constant के लिए कोई Built-in Keyword नहीं है।
- type() Function Data Type बताता है।
- Assignment Operator = होता है।
- Python Case Sensitive Language है।
Frequently Asked Questions (FAQs)
Variable Memory में Data Store करने के लिए उपयोग किया जाने वाला नाम है।
प्रश्न 2. Python Dynamic Typing क्या है?
Python स्वयं Variable का Data Type पहचान लेता है।
प्रश्न 3. Python में Constant कैसे बनाते हैं?
UPPERCASE Naming Convention का उपयोग करके।
प्रश्न 4. type() Function का उपयोग क्या है?
किसी Variable का Data Type जानने के लिए।
प्रश्न 5. Python में कितने Built-in Data Types हैं?
Python में कई Built-in Data Types हैं, जैसे Numeric, String, Boolean, List, Tuple, Set, Dictionary, Binary Types और NoneType।
- Variable = Memory में Data Store करने का नाम
- Constant = ऐसी Value जिसे बदलना नहीं चाहिए
- Python = Dynamic Typing Language
- type() = Data Type बताता है
- Assignment Operator = =
- Data Types = int, float, complex, str, bool, list, tuple, set, dict, None आदि
Python Operators (ऑपरेटर्स) — इसमें Arithmetic, Assignment, Comparison, Logical, Bitwise, Identity, Membership Operators, Operator Precedence और प्रत्येक Operator के Practical Examples तथा Interview/Exam आधारित प्रश्नों को विस्तार से समझेंगे।