मुख्य कंटेंट पर जाएं
Python Variables, Constants और Data Types
PYTHON PROGRAMMING 13 मिनट पढ़ें 1 बार पढ़ा गया

Python Variables, Constants और Data Types

Lavkush Chakrawarti

परिचय

Variable क्या है?

उदाहरण

Variable क्यों आवश्यक है?

Variable कैसे बनाते हैं?

उदाहरण

Variable Declaration

Syntax

उदाहरण

Variable Assignment

उदाहरण

Multiple Variable Assignment

उदाहरण

Same Value to Multiple Variables

Variable Naming Rules

सही नियम

गलत नियम

सही Variable Names

गलत Variable Names

Python Case Sensitive है

Naming Convention

उदाहरण

Constant क्या होता है?

उदाहरण

Variable और Constant में अंतर

Dynamic Typing क्या है?

उदाहरण

Data Type क्या है?

Python के Built-in Data Types

Numeric Types

Text Type

Boolean Type

Sequence Types

Set Types

Mapping Type

Binary Types

None Type

Integer (int)

Float

Complex Number

String

Boolean

List

Tuple

Set

Dictionary

None

type() Function

उदाहरण

विभिन्न Data Types का उदाहरण

Type Casting क्या है?

Integer में बदलना

Float में बदलना

String में बदलना

Boolean में बदलना

Implicit Type Conversion

Explicit Type Conversion

Memory में Variable कैसे Store होता है?

Best Practices

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

Frequently Asked Questions (FAQs)

प्रश्न 1. Variable क्या है?

प्रश्न 2. Python Dynamic Typing क्या है?

प्रश्न 3. Python में Constant कैसे बनाते हैं?

प्रश्न 4. type() Function का उपयोग क्या है?

प्रश्न 5. Python में कितने Built-in Data Types हैं?

Quick Revision

अगला अध्याय

 

परिचय

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 क्या है?

Variable कंप्यूटर की Memory में बनाया गया एक नाम (Name) होता है, जिसमें किसी Value को Store किया जाता है।

जब Program चलता है, तो Variable उस Value को Memory में सुरक्षित रखता है ताकि आवश्यकता पड़ने पर उसे उपयोग किया जा सके।

उदाहरण

name = "Rahul"

age = 21

marks = 88

यहाँ—

  • name में "Rahul"
  • age में 21
  • marks में 88

Store किया गया है।

 

Variable क्यों आवश्यक है?

यदि Variables न हों, तो हर बार Value को सीधे Program में लिखना पड़ेगा।

Variables की सहायता से—

  • Data Store किया जाता है।
  • गणनाएँ (Calculations) की जाती हैं।
  • User Input सुरक्षित रखा जाता है।
  • Database से प्राप्त Data को Store किया जाता है।
  • Programs अधिक Flexible बनते हैं।

 

Variable कैसे बनाते हैं?

Python में Variable बनाने के लिए अलग से कोई Keyword नहीं होता।

सीधे Variable Name लिखकर Value Assign कर दी जाती है।

उदाहरण

city = "Delhi"

price = 599.50

is_student = True

 

Variable Declaration

Python में Variable Declare और Initialize एक साथ किए जाते हैं।

Syntax

variable_name = value

उदाहरण

student = "Ankit"

age = 20

salary = 35000

 

Variable Assignment

जब किसी Variable को Value दी जाती है, तो उसे Assignment कहते हैं।

Assignment Operator

=

उदाहरण

x = 100

यहाँ = Assignment Operator है।

 

Multiple Variable Assignment

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 होगा।

 

Variable Naming Rules

Python में Variable Name बनाते समय निम्न नियमों का पालन करना आवश्यक है।

सही नियम

✔ अक्षर (A-Z, a-z) से शुरू हो सकता है।

✔ Underscore (_) से शुरू हो सकता है।

✔ Numbers बाद में आ सकते हैं।

✔ Case Sensitive होता है।

 

गलत नियम

❌ Number से शुरू नहीं हो सकता।

❌ Space नहीं हो सकता।

❌ Special Characters (@, %, #, &, *) का उपयोग नहीं किया जा सकता।

❌ Keyword का उपयोग नहीं किया जा सकता।

 

सही Variable Names

name

student_name

roll_no

marks2026

_price

 

गलत Variable Names

1name

student-name

class

for

user@name

 

Python Case Sensitive है

Name = "Amit"

name = "Rahul"

दोनों अलग-अलग Variables हैं।

 

Naming Convention

Python में PEP 8 Style Guide के अनुसार Variables को snake_case में लिखना चाहिए।

उदाहरण

student_name

total_marks

monthly_salary

 

Constant क्या होता है?

Constant वह Value होती है जिसे Program के दौरान बदलना नहीं चाहिए।

Python में Constant बनाने के लिए कोई अलग Keyword नहीं है, लेकिन Convention के अनुसार Constant को UPPERCASE में लिखा जाता है।

उदाहरण

PI = 3.14159

MAX_LIMIT = 100

GST_RATE = 18

 

Variable और Constant में अंतर

Variable

Constant

Value बदल सकती है

Value बदलनी नहीं चाहिए

सामान्य नाम

UPPERCASE नाम

Program के दौरान बदल सकता है

सामान्यतः स्थिर रहता है

 

Dynamic Typing क्या है?

Python एक Dynamically Typed Language है।

इसका अर्थ है कि Variable का Data Type पहले से घोषित करने की आवश्यकता नहीं होती।

Python स्वयं पहचान लेता है।

उदाहरण

x = 10

x = "Python"

x = 15.75

हर बार x का Data Type बदल गया।

 

Data Type क्या है?

Data Type बताता है कि Variable में किस प्रकार का Data Store किया गया है।

उदाहरण—

  • Integer
  • Decimal Number
  • Text
  • Boolean
  • List
  • Dictionary

 

Python के Built-in Data Types

Python में कई Built-in Data Types उपलब्ध हैं।

Numeric Types

  • int
  • float
  • complex

 

Text Type

  • str

 

Boolean Type

  • bool

 

Sequence Types

  • list
  • tuple
  • range

 

Set Types

  • set
  • frozenset

 

Mapping Type

  • dict

 

Binary Types

  • bytes
  • bytearray
  • memoryview

 

None Type

  • NoneType

 

Integer (int)

पूर्ण संख्याएँ।

age = 25

marks = 450

 

Float

दशमलव संख्या।

price = 599.99

height = 5.8

 

Complex Number

x = 4 + 5j

मुख्यतः वैज्ञानिक गणनाओं में उपयोग।

 

String

Text Data Store करता है।

name = "Ekta Computer Notes"

 

Boolean

केवल दो Values होती हैं।

True

False

उदाहरण

is_pass = True

 

List

Multiple Values Store करता है।

marks = [80, 90, 95]

 

Tuple

List जैसा लेकिन Immutable।

colors = ("Red", "Blue", "Green")

 

Set

Unique Values Store करता है।

numbers = {1,2,3,4}

 

Dictionary

Key और Value के रूप में Data Store करता है।

student = {

    "name":"Rahul",

    "age":20

}

 

None

जब किसी Variable में कोई Value नहीं होती।

data = None

 

type() Function

Variable का Data Type जानने के लिए।

उदाहरण

x = 50

print(type(x))

Output

<class 'int'>

 

विभिन्न Data Types का उदाहरण

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'>

 

Type Casting क्या है?

एक Data Type को दूसरे Data Type में बदलने की प्रक्रिया Type Casting कहलाती है।

Integer में बदलना

x = int("100")

 

Float में बदलना

x = float(20)

 

String में बदलना

x = str(100)

 

Boolean में बदलना

x = bool(1)

 

Implicit Type Conversion

Python स्वयं Type बदल देता है।

x = 10

y = 5.5

 

print(x+y)

Output

15.5

 

Explicit Type Conversion

Programmer स्वयं Type बदलता है।

x = "100"

 

print(int(x))

 

Memory में Variable कैसे Store होता है?

जब Variable बनाया जाता है, तो Python Memory में उसके लिए स्थान (Memory Location) निर्धारित करता है और Value Store कर देता है।

यदि वही Value दोबारा Assign की जाती है, तो Python Memory Optimization भी कर सकता है।

 

Best Practices

✔ 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)

प्रश्न 1. Variable क्या है?

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।

 

Quick Revision

  • 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 आधारित प्रश्नों को विस्तार से समझेंगे।

 

निष्कर्ष (Conclusion)

Python Variables, Constants और Data Types की पूरी जानकारी हिंदी में। Variable Declaration, Naming Rules, Dynamic Typing, type() Function, Type Casting और सभी Built-in Data Types को उदाहरण सहित सीखें।