what is python? (Python क्या है? आसान भाषा में समझें)

What is Python? पाइथन क्या है? 

Python एक उच्च स्तरीय high level प्रोग्रामिंग भाषा है, जिसे इसके सिंपल सिंटैक्स और उपयोग में आसान इंटरफ़ेस के लिए जाना जाता है। इसका उपयोग वेब डेवलपमेंट, डेटा एनालिसिस, मशीन लर्निंग, ऑटोमेशन आदि के लिए होता है। इसकी सबसे बड़ी विशेषता यह है कि यह पढ़ने और लिखने में सरल है।

Introduction to Python Programming

पाइथन एक high-level, interpreted प्रोग्रामिंग लैंग्वेज है जो beginners के लिए सबसे उपयुक्त मानी जाती है। इसका सिंटैक्स बहुत ही सिंपल और पढ़ने में आसान होता है।

Python ka Use kahan hota hai?

  • Web Development

  • Data Science & Machine Learning

  • Automation Scripts

  • Desktop Applications

  • Game Development

Python ki khas baatein (Key Features) Python कैसे सीखें? एक शुरुआती गाइड

  • Easy to Learn & Use

  • Open Source hai

  • Large Community Support

  • Extensive Libraries (jaise NumPy, Pandas, Matplotlib)

  • यहाँ Python का संक्षिप्त परिचय:

    ·         आसान और पठनीय: Python का सिंटेक्स (code structure) अन्य प्रोग्रामिंग भाषाओं की तुलना में सरल होता है। इसे पढ़ना और समझना आसान है।

    ·         बहु-प्रयोजन: Python का उपयोग वेब ऐप्स, डेटा एनालिसिस, गेम डेवलपमेंट और बहुत कुछ बनाने के लिए किया जा सकता है।

    ·         ऑब्जेक्ट ओरिएंटेड: Python ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग का समर्थन करता है, जिससे कोड को अधिक संगठित और पुन: उपयोग योग्य बनाया जा सकता है।

    ·         लाइब्रेरी सपोर्ट: Python में लाखों प्री-निर्मित लाइब्रेरी उपलब्ध हैं, जो इसे शक्तिशाली और समय बचाने वाला बनाती हैं। उदाहरण के लिए, NumPy, Pandas, और TensorFlow जैसी लाइब्रेरी।

    Windows पर Python इंस्टॉल करना बेहद आसान है। यहाँ पर आपको इसके स्टेप-बाय-स्टेप निर्देश दिए गए हैं:

How to install Python? 

1. Python का डाउनलोड करें

·         Python की आधिकारिक वेबसाइट पर जाएं।

·         आपके ऑपरेटिंग सिस्टम के लिए उचित संस्करण स्वतः ही दिखेगा। “Download Python [Version]” बटन पर क्लिक करें।

2. इंस्टॉलर चलाएं

·         डाउनलोड की गई .exe फ़ाइल पर डबल-क्लिक करें।

·         इंस्टॉलेशन विंडो खुलने पर, “Add Python to PATH” विकल्प को चेक करें। (यह महत्वपूर्ण है, क्योंकि इससे Python के कमांड लाइन टूल्स तक पहुंच आसान हो जाएगी।)

·         अब “Install Now” बटन पर क्लिक करें।

3. इंस्टॉलेशन प्रक्रिया पूरी करें

·         Python इंस्टॉल होने में कुछ मिनट लगेंगे। प्रक्रिया पूरी होने पर “Setup was successful” संदेश दिखाई देगा।

4. सत्यापन करें

·         Python सही तरीके से इंस्टॉल हुआ या नहीं, यह चेक करने के लिए:

1.      Windows Search में “cmd” टाइप करके कमांड प्रॉम्प्ट खोलें।

2.      टाइप करें: python --version और Enter दबाएं।

3.      अगर इंस्टॉलेशन सफल रहा, तो Python का संस्करण दिखाई देगा (जैसे, Python 3.x.x).

5. एक साधारण प्रोग्राम चलाएं

·         Python का IDE (जैसे, IDLE) या कमांड प्रॉम्प्ट खोलें।

·         टाइप करें: print("Hello, World!") और Enter दबाएं। स्क्रीन पर Hello, World! दिखाई देगा।

Conclusion:

Agar aap programming start karna chahte hain, to Python ek perfect language hai. Aage ke blogs me hum basic se lekar advanced topics tak explore karenge.


1. VSCode डाउनलोड करें

Windows पर VSCode (Visual Studio Code) को इंस्टॉल करना बहुत आसान है। नीचे दिए गए स्टेप्स का पालन करें:

·         Visual Studio Code की आधिकारिक वेबसाइट पर जाएं।

·         “Download for Windows” बटन पर क्लिक करें।

2. इंस्टॉलर फ़ाइल चलाएं

·         डाउनलोड की गई .exe फ़ाइल को डबल-क्लिक करें।

·         इंस्टॉलेशन विज़ार्ड ओपन होगा। इसमें “I accept the agreement” विकल्प चुनें और Next पर क्लिक करें।

3. इंस्टॉलेशन सेटिंग्स करें

·         आप इंस्टॉलेशन पथ चुन सकते हैं या डिफ़ॉल्ट सेटिंग्स के साथ आगे बढ़ सकते हैं।

·         “Add to PATH” विकल्प को चेक करना न भूलें, ताकि इसे आसानी से एक्सेस किया जा सके।

4. इंस्टॉल करें

·         अब “Install” बटन पर क्लिक करें। प्रक्रिया पूरी होने में कुछ मिनट लगेंगे।

·         इंस्टॉलेशन प्रक्रिया पूरी होने पर “Finish” पर क्लिक करें और VSCode को लॉन्च करें।

5. VSCode का उपयोग शुरू करें

·         पहली बार लॉन्च होने पर, आपको कुछ सेटअप करने का विकल्प मिलेगा। आप थीम, एक्सटेंशन, और भाषा सेट कर सकते हैं।

·         इसके बाद, आप Python, HTML, या किसी अन्य प्रोग्रामिंग लैंग्वेज के साथ कोडिंग शुरू कर सकते हैं।

First Program

1. Python का IDE या कमांड प्रॉम्प्ट खोलें

·         Python इंस्टॉल करने के बाद, आप इसका डिफ़ॉल्ट IDE (IDLE) या कमांड प्रॉम्प्ट का उपयोग कर सकते हैं।

2. कोड लिखें

·         सबसे सरल Python प्रोग्राम है “Hello, World!”। इसे लिखने के लिए नीचे दिया गया कोड टाइप करें:

print("Hello, World!")

3. कोड को रन करें

·         अगर आप IDLE का उपयोग कर रहे हैं, तो ऊपर दिए गए कोड को टाइप करें और Enter दबाएं।

·         कमांड प्रॉम्प्ट पर Python फ़ाइल रन करने के लिए, पहले कोड को किसी फ़ाइल में सेव करें। उदाहरण: hello.py

o    फिर cmd खोलें और उस डायरेक्टरी पर जाएं जहां फ़ाइल सेव की गई है।

o    टाइप करें: python hello.py और Enter दबाएं।

4. आउटपुट देखें

·         स्क्रीन पर Hello, World! प्रिंट होगा।

Python Character Set

Python के Character Set का उपयोग कोडिंग में विभिन्न प्रकार के कैरेक्टर को पहचानने और उपयोग करने के लिए होता है। इसमें शामिल सभी कैरेक्टर को Python इंटरप्रेटर समझता है और उनका उपयोग कोड बनाने के लिए किया जाता है। आइए इसे विस्तार से समझते हैं:

Python Character Set के प्रकार

1.      Letters (अक्षर):

o    Python में छोटे और बड़े अक्षरों का उपयोग किया जा सकता है।

o    उदाहरण: a, b, c...z और A, B, C...Z

2.      Digits (अंक):

o    Python में अंकों का उपयोग गणनाओं और डेटा प्रोसेसिंग के लिए किया जाता है।

o    उदाहरण: 0, 1, 2, 3...9

3.      Special Symbols (विशेष चिन्ह):

o    Python में विशेष चिन्हों का उपयोग विभिन्न ऑपरेटर और एक्सप्रेशन बनाने में होता है।

o    उदाहरण: +, -, *, /, =, %, @, #, आदि।

4.      White Spaces (खाली स्थान):

o    Python में खाली स्थान (स्पेस, टैब) का उपयोग कोड को पढ़ने योग्य बनाने के लिए किया जाता है।

o    हालांकि, Python में इंडेंटेशन महत्वपूर्ण होता है और यह कोड की संरचना को प्रभावित कर सकता है।

5.      Other Characters (अन्य कैरेक्टर):

o    Python कुछ अन्य कैरेक्टर को भी पहचानता है जैसे:

§  स्केप सीक्वेंस: \n, \t, आदि।

§  ब्रैकेट्स: {}, [], ()

उदाहरण:

# विभिन्न कैरेक्टर का उपयोग करके एक सिंपल कोड
x = 10          # Digit
name = "Python" # Letters

print(name, x)  # Special symbols

Variables and how to use them

Python में Variables (वेरिएबल्स) डेटा को स्टोर करने के लिए उपयोग किए जाते हैं। वेरिएबल्स को कंटेनर की तरह समझ सकते हैं, जो विभिन्न प्रकार के डेटा (जैसे नंबर, टेक्स्ट आदि) को रखते हैं। आइए इसे विस्तार से समझते हैं:

Python Variables का परिचय

·         वेरिएबल एक नाम है जिसका उपयोग डेटा को स्टोर करने के लिए किया जाता है।

·         वेरिएबल्स का उपयोग प्रोग्रामिंग में डेटा को मैनेज और प्रोसेस करने के लिए किया जाता है।

वेरिएबल्स को बनाने के नियम (Rules for creating variables)

1.      वेरिएबल का नाम अक्षरों (a-z,
A-Z
), अंकों (0-9) या _ से बना होना चाहिए।

2.      वेरिएबल का नाम संख्या से शुरू नहीं हो सकता।

3.      Python में वेरिएबल के नाम केस सेंसिटिव होते हैं। यानी name और Name अलग-अलग वेरिएबल हैं।

वेरिएबल्स का उपयोग कैसे करें?

1.      वेरिएबल को स्टोर करना:

o    एक वेरिएबल बनाएं और उसमें डेटा स्टोर करें।

age = 25
name = "Rahul"
height = 5.9

2.      वेरिएबल का उपयोग करना:

o    आप वेरिएबल्स का उपयोग प्रिंट करने या गणना करने के लिए कर सकते हैं।

print("Name:", name)
print("Age:", age)
print("Height:", height)

3.      वेरिएबल्स का ऑपरेशन:

o    वेरिएबल्स का उपयोग गणितीय ऑपरेशन में किया जा सकता है।

x = 10
y = 5
result = x + y
print("Result:", result)

विभिन्न प्रकार के डेटा का उपयोग

Python में वेरिएबल्स कई प्रकार के डेटा को स्टोर कर सकते हैं:

·         संख्या (Numbers): जैसे int, float

marks = 85
percentage = 92.5

·         टेक्स्ट (Strings):

language = "Python"

·         बूलियन (Boolean):

is_active = True

उदाहरण:

name = "Aarav"
age = 30
is_programmer = True
 
print("Name:", name)
print("Age:", age)
print("Is Programmer:", is_programmer)

Rules for Identifiers in python

Python में Identifiers (पहचानकर्ता) का उपयोग वेरिएबल, फंक्शन, क्लास आदि को नाम देने के लिए किया जाता है। यह नाम आपके कोड को अधिक समझने योग्य और संगठित बनाने में मदद करता है। Identifiers का उपयोग करते समय कुछ नियम होते हैं, जिन्हें ध्यान में रखना आवश्यक है। आइए इन नियमों को समझते हैं:

Python Identifiers के लिए नियम

1.      अक्षर और अंक:

o    Identifier का नाम अक्षरों (a-z, A-Z) और अंकों (0-9) का उपयोग करके बनाया जा सकता है।

o    लेकिन Identifier का नाम कभी भी संख्या से शुरू नहीं हो सकता

o    सही उदाहरण: name1, value123

o    गलत उदाहरण: 1name, 123value

2.      खास अक्षर (_underscore):

o    Identifier के नाम में _ (अंडरस्कोर) का उपयोग किया जा सकता है।

o    उदाहरण: _value, my_variable

3.      कीवर्ड का उपयोग नहीं कर सकते:

o    Python में कुछ विशेष कीवर्ड होते हैं जैसे for, while, if, else आदि। इनका उपयोग Identifier के रूप में नहीं किया जा सकता।

o    गलत उदाहरण: if = 10 (गलत क्योंकि if एक कीवर्ड है)

4.      केस सेंसिटिव:

o    Python में Identifiers केस-सेंसिटिव होते हैं। इसका मतलब है कि name और Name को अलग-अलग माना जाएगा।

o    उदाहरण:

name = "Python"
Name = "Programming"
print(name)  # Output: Python
print(Name)  # Output: Programming

5.      कोई स्पेस नहीं हो सकता:

o    Identifier में स्पेस का उपयोग नहीं कर सकते। यदि आवश्यक हो, तो _ का उपयोग करें।

o    सही उदाहरण: my_variable

o    गलत उदाहरण: my variable

6.      लंबाई की कोई सीमा नहीं:

o    Identifier का नाम जितना भी लंबा हो सकता है, लेकिन इसे पढ़ने और समझने में आसान रखें।

7.      विशेष प्रतीकों का उपयोग नहीं:

o    Identifiers में @, $, %, & जैसे विशेष प्रतीकों का उपयोग नहीं कर सकते।

o    गलत उदाहरण: my@name, value%

उदाहरण: सही और गलत Identifiers

सही Identifiers

गलत Identifiers

my_variable    

my
variable

name1

1name

_value

my@variable

MyClass

class (कीवर्ड)

Data Types

Python में Data Types (डेटा प्रकार) महत्वपूर्ण भूमिका निभाते हैं, क्योंकि वे तय करते हैं कि किस प्रकार का डेटा वेरिएबल में स्टोर किया जाएगा। Python एक डायनामिक टाइपिंग भाषा है, जिससे आपको वेरिएबल के डेटा टाइप को explicitly (स्पष्ट रूप से) बताने की आवश्यकता नहीं होती। आइए Python में मौजूद प्रमुख डेटा टाइप्स को समझते हैं:

Python के मुख्य डेटा टाइप्स

1.      Numeric Types (संख्यात्मक प्रकार):

o    int: पूरे नंबर (इंटीजर) को स्टोर करता है।

age = 25

o    float: दशमलव संख्या (Decimal Numbers) को स्टोर करता है।

height = 5.9

o    complex: जटिल संख्या (Complex Numbers) को स्टोर करता है।

num = 3 + 5j

2.      Text Type (पाठ प्रकार):

o    str: टेक्स्ट या स्ट्रिंग को स्टोर करता है।

name = "Python"

3.      Sequence Types (क्रम प्रकार):

o    list: Ordered और बदलने योग्य (mutable) डेटा संरचना।

fruits = ["apple", "banana", "cherry"]

o    tuple: Ordered लेकिन अपरिवर्तनीय (immutable) डेटा संरचना।

numbers = (1, 2, 3)

o    range: एक अनुक्रम (sequence) बनाता है।

r = range(10)

4.      Mapping Type (मैपिंग प्रकार):

o    dict: Key-Value pairs का संग्रह।

person = {"name": "Aarav", "age": 25}

5.      Set Types (सेट प्रकार):

o    set: Unordered और अद्वितीय (unique) items को स्टोर करता है।

colors = {"red", "blue", "green"}

o    frozenset: Immutable सेट।

fs = frozenset(["red", "blue"])

6.      Boolean Type (बूलियन प्रकार):

o    bool: True या False को दर्शाता है।

is_active = True

7.      Binary Types (बाइनरी प्रकार):

o    bytes: Immutable बाइनरी डेटा।

b = b"Hello"

o    bytearray: Mutable बाइनरी डेटा।

ba = bytearray(5)

o    memoryview: बाइनरी डेटा का व्यू।

mv = memoryview(b"Hello")

Python में डेटा टाइप्स का उपयोग कैसे करें?

1.      टाइप चेक करना:

o    आप type() फ़ंक्शन का उपयोग करके किसी वेरिएबल के डेटा टाइप को चेक कर सकते हैं।

age = 25
print(type(age))  # Output: <class 'int'>

2.      कन्वर्जन (Conversion):

o    डेटा टाइप्स को बदलने के लिए आप int(), float(), str() आदि का उपयोग कर सकते हैं।

x = "123"
num = int(x)
print(type(num))  # Output: <class 'int'>

उदाहरण:

# विभिन्न डेटा टाइप्स का उपयोग
name = "Python"   # str
age = 25          # int
height = 5.9      # float
is_active = True  # bool
colors = {"red", "blue"} # set
 
print(name, age, height, is_active, colors)

 

 

 

वेरिएबल (Variable)

वेरिएबल्स डेटा को स्टोर करने वाले कंटेनर्स होते हैं। वेरिएबल को नाम देकर उसमें डेटा रखा जाता है। Python में वेरिएबल डिफाइन करने के लिए किसी डेटा टाइप का उल्लेख करना आवश्यक नहीं है।

name = “Kamod_”   # यह एक स्ट्रिंग वेरिएबल है

age = 25          # यह एक इंटीजर वेरिएबल है

pi = 3.14         # यह फ्लोट वेरिएबल है

is_happy = True   # यह एक बूलियन वेरिएबल है

डेटा टाइप्स (Data Types)

Python में डेटा टाइप्स का उपयोग यह बताने के लिए होता है कि वेरिएबल में किस प्रकार का डेटा स्टोर किया गया है। प्रमुख डेटा टाइप्स निम्नलिखित हैं:

1.      इंटीजर (Integer): यह पूर्णांक संख्याओं को स्टोर करता है।

age = 25

2.      फ्लोट (Float): यह दशमलव संख्या को स्टोर करता है।

price = 99.99

3.      स्ट्रिंग (String): टेक्स्ट डेटा को स्टोर करता है। स्ट्रिंग को डबल या सिंगल कोट्स में लिखा जाता है।

name = "Kamod_"

4.      बूलियन (Boolean): यह True या False मान स्टोर करता है।

is_active = True

5.      लिस्ट (List): यह एक क्रमबद्ध संग्रह है, जिसमें एक से अधिक आइटम हो सकते हैं।

fruits = ["apple", "banana", "cherry"]

6.      टपल (Tuple): लिस्ट के समान होता है लेकिन इसके आइटम्स को बदला नहीं जा सकता।

coordinates = (10, 20)

7.      डिक्शनरी (Dictionary): इसमें key-value जोड़ी का उपयोग किया जाता है।

person = {"name": "Kamod_", "age": 25}

8.      सेट (Set): यह अनऑर्डर्ड और यूनिक आइटम्स का संग्रह होता है।

unique_numbers = {1, 2, 3}

वेरिएबल और डेटा टाइप्स पर नोट्स

·         Python में वेरिएबल को उपयोग करने से पहले डिफाइन करना होता है।

·         Python में आप आसानी से डेटा टाइप्स को चेक कर सकते हैं।

print(type(name))  # Output: <class 'str'>
 

Python में, स्ट्रिंग डेटा टाइप (String Data Type) का उपयोग टेक्स्ट को स्टोर और मैनेज करने के लिए किया जाता है। स्ट्रिंग्स के साथ काम करना प्रोग्रामिंग में बेहद सामान्य और महत्वपूर्ण होता है। चलिए इसे विस्तार से समझते हैं:

स्ट्रिंग क्या है?

स्ट्रिंग टेक्स्ट डेटा को स्टोर करने के लिए इस्तेमाल किया जाता है। स्ट्रिंग को एक या अधिक वर्णों (characters) के अनुक्रम (sequence) के रूप में परिभाषित किया जाता है। इसे सिंगल कोट (‘ ‘), डबल कोट (” “) या ट्रिपल कोट (”’ ”’ या “”” “””) के अंदर लिखा जाता है।

उदाहरण:

python

# सिंगल कोट का उपयोग
string1 = 'Hello'
# डबल कोट का उपयोग
string2 = "Kamod_"
# ट्रिपल कोट का उपयोग (मल्टी-लाइन स्ट्रिंग्स के लिए)
string3 = '''यह 
मल्टी-लाइन
स्ट्रिंग है।'''

स्ट्रिंग की विशेषताएँ (Features of Strings):

1.      इम्यूटेबल (Immutable): स्ट्रिंग्स को बनने के बाद बदला नहीं जा सकता। यदि आप किसी स्ट्रिंग को एडिट करना चाहते हैं, तो आपको एक नई स्ट्रिंग बनानी होगी।

2.      इंडेक्सिंग (Indexing): स्ट्रिंग में हर कैरेक्टर का एक इंडेक्स होता है। इंडेक्स 0 से शुरू होता है।

o    उदाहरण:

string = "Python"
print(string[0])  # Output: P
print(string[-1]) # Output: n (नेगेटिव इंडेक्स)

3.      स्लाइसिंग (Slicing): स्ट्रिंग के किसी भाग को एक्सेस करने के लिए स्लाइसिंग का उपयोग होता है।

o    उदाहरण:

string = "Python Programming"
print(string[0:6])  # Output: Python
print(string[7:])   # Output: Programming

4.      लंबाई (Length): len() फंक्शन का उपयोग स्ट्रिंग की लंबाई ज्ञात करने के लिए किया जाता है।

o    उदाहरण:

string = "Kamod_"
print(len(string))  # Output: 6

स्ट्रिंग में उपयोगी ऑपरेशन (Operations on Strings):

1.      जोड़ना (Concatenation): दो या अधिक स्ट्रिंग्स को जोड़ने के लिए + ऑपरेटर का उपयोग होता है।

o    उदाहरण:

first_name = "Kamod"
last_name = "Kumar"
full_name = first_name + " " + last_name
print(full_name)  # Output: Kamod Kumar

2.      रिपीट करना (Repetition): * ऑपरेटर का उपयोग स्ट्रिंग को रिपीट करने के लिए किया जाता है।

o    उदाहरण:

string = "Python "
print(string * 3)  # Output: Python Python Python

3.      सबस्ट्रिंग चेक करना (Substring Check): आप in ऑपरेटर का उपयोग करके यह जांच सकते हैं कि कोई सबस्ट्रिंग किसी स्ट्रिंग में मौजूद है या नहीं।

o    उदाहरण:

string = "Learn Python Programming"
print("Python" in string)  # Output: True

4.      केस बदलना (Changing Case): स्ट्रिंग को अपर या लोअर केस में बदलने के लिए:

o    upper() और lower():

string = "Python"
print(string.upper())  # Output: PYTHON
print(string.lower())  # Output: python

5.      स्प्लिट और जॉइन (Split and Join):

o    split() का उपयोग स्ट्रिंग को तोड़ने के लिए और join() का उपयोग उन्हें जोड़ने के लिए होता है।

string = "Python is fun"
words = string.split()  # Output: ['Python', 'is', 'fun']
new_string = "-".join(words)  # Output: Python-is-fun

6.      स्ट्रिप (Strip): strip() का उपयोग स्ट्रिंग के शुरुआत और अंत में मौजूद एक्स्ट्रा स्पेस को हटाने के लिए होता है।

o    उदाहरण:

string = "   Hello   "
print(string.strip())  # Output: Hello

प्रैक्टिकल उपयोग:

स्ट्रिंग्स का उपयोग प्रोग्रामिंग में टेक्स्ट मैनिपुलेशन, उपयोगकर्ता इनपुट को हैंडल करने, फाइलों से टेक्स्ट रीड/राइट करने, और JSON/XML डेटा प्रोसेसिंग में किया जाता है।

इंटीजर क्या है?

·         Integer एक डेटा टाइप है जो नकारात्मक संख्याएं (-), शून्य (0), और सकारात्मक संख्याएं (+) को स्टोर करता है।

·         इसका उपयोग तब किया जाता है जब हमें पूरी संख्या की आवश्यकता होती है, बिना दशमलव के।

उदाहरण:

·         -5, 0, 10, 45

Python में इंटीजर का उपयोग कैसे करें?

1. वेरिएबल में इंटीजर स्टोर करना:

# इंटीजर को वेरिएबल में स्टोर करना
age = 25
temperature = -10
score = 100
 
print("Age:", age)
print("Temperature:", temperature)
print("Score:", score)

आउटपुट:

Age: 25
Temperature: -10
Score: 100

2. इंटीजर पर गणना करना:

# गणितीय ऑपरेशन पर इंटीजर का उपयोग
num1 = 15
num2 = 10
 
sum_result = num1 + num2
difference_result = num1 - num2
 
print("Sum:", sum_result)
print("Difference:", difference_result)

आउटपुट:

Sum: 25
Difference: 5

इंटीजर के विशेष गुण

1.      असीमित सीमा:

o    Python में इंटीजर की सीमा (range) तय नहीं होती, यानी आप बहुत बड़ी या बहुत छोटी संख्याओं का उपयोग कर सकते हैं।

big_number = 999999999999999999999999999
print(big_number)

2.      टाइप चेक करना:

o    type() फ़ंक्शन से आप चेक कर सकते हैं कि वेरिएबल इंटीजर है या नहीं।

x = 50
print(type(x))  # Output: <class 'int'>

कुछ मजेदार उदाहरण

1. इंटीजर का उपयोग लूप में:

for i in range(5):  # इंटीजर का उपयोग करते हुए लूप चलाना
    print("Number:", i)

आउटपुट:

Number: 0
Number: 1
Number: 2
Number: 3
Number: 4

2. इंटीजर को अन्य डेटा टाइप्स में बदलना:

# इंटीजर को स्ट्रिंग में बदलना
x = 123
x_str = str(x)
 
print("Integer:", x)
print("Converted to String:", x_str)

आउटपुट:

Integer: 123
Converted to String: 123

Float Data Type

Python में float डेटा टाइप एक numerical डेटा टाइप है जिसका उपयोग दशमलव (decimal) या fractional मानों (values) को संग्रहित करने के लिए किया जाता है। इसे float क्लास के तहत परिभाषित किया जाता है। आइए इसे विस्तार से समझते हैं:

मुख्य बिंदु:

1.      Float डेटा टाइप दशमलव अंकों को संग्रहीत करता है।

2.      Float नंबर को scientific notation में भी लिखा जा सकता है (उदाहरण: 1.2e3 का मतलब 1.2 × 10³)

3.      इनका उपयोग गणितीय और वैज्ञानिक गणनाओं में होता है।

उदाहरण:

1. साधारण float मान:

num = 10.5
print(num)

आउटपुट: 10.5

यहाँ, num एक float वेरिएबल है जो 10.5 को स्टोर करता है।

2. वैज्ञानिक नोटेशन (Scientific Notation):

value = 1.2e3
print(value)

आउटपुट: 1200.0

1.2e3 का मतलब है 1.2×1031.2 × 10³, जो 1200.0 के बराबर है।

3. दशमलव गणना (Fractional Calculation):

result = 5.5 / 2
print(result)

आउटपुट: 2.75

यह float गणनाओं का एक उदाहरण है।

4. Round-off float मान:

number = 3.14159
print(round(number, 2))

आउटपुट: 3.14

यहाँ, round() फ़ंक्शन का उपयोग करके मान को 2 दशमलव स्थान तक सीमित किया गया है।

महत्वपूर्ण नोट्स:

·         Float डेटा टाइप का उपयोग सटीक दशमलव गणना की आवश्यकता होने पर करें।

·         Float अंकगणित में कभी-कभी precision issue आता है। जैसे:

num = 0.1 + 0.2
print(num)

आउटपुट: 0.30000000000000004

यह “राउंडिंग एरर” float अंकगणित की सीमाओं को दर्शाता है।

 

List Data Type

Python में list data type एक बहुत ही महत्वपूर्ण और उपयोगी डेटा टाइप है। यह एक ordered और mutable (बदलने योग्य) collection है, जिसमें विभिन्न प्रकार के डेटा items को एक साथ संग्रहित किया जा सकता है। इसे square brackets [ ] का उपयोग करके परिभाषित किया जाता है। आइए इसे विस्तार से समझते हैं:

मुख्य विशेषताएँ (Features of List):

1.      Ordered Collection: List में items को एक specific क्रम (order) में रखा जाता है।

2.      Mutable: आप list के elements को जोड़, हटा, या बदल सकते हैं।

3.      Heterogeneous: List में विभिन्न प्रकार के डेटा (integer, string, float, आदि) को संग्रहीत किया जा सकता है।

4.      Indexed: List के प्रत्येक item का एक index होता है, जो 0 से शुरू होता है।

List का उपयोग:

·         डेटा को sequence में संग्रहित करने के लिए।

·         विभिन्न प्रकार की जानकारी को एक ही collection में रखने के लिए।

·         Iteration और sorting जैसे operations करने के लिए।

·         डेटा की सूची (list) के साथ काम करने वाले algorithms को लागू करने के लिए।

List कैसे बनाते और उपयोग करते हैं?

1. साधारण List बनाना:

fruits = ["apple", "banana", "cherry"]
print(fruits)

आउटपुट: ['apple', 'banana', 'cherry']

यहाँ fruits नाम की एक list बनाई गई है, जिसमें तीन string elements हैं।

2. List में विभिन्न प्रकार के डेटा:

mixed_list = [10, "hello", 3.14, True]
print(mixed_list)

आउटपुट: [10, 'hello', 3.14, True]

यह list विभिन्न प्रकार के डेटा (integer, string, float, और boolean) को स्टोर करती है।

3. List में element जोड़ना (Appending Elements):

numbers = [1, 2, 3]
numbers.append(4)
print(numbers)

आउटपुट: [1, 2, 3, 4]

append() method का उपयोग करके list में नया element जोड़ा गया।

4. List से element हटाना (Removing Elements):

fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)

आउटपुट: ['apple', 'cherry']

remove() method का उपयोग करके list से “banana” को हटाया गया।

5. Index के माध्यम से Access:

fruits = ["apple", "banana", "cherry"]
print(fruits[1])

आउटपुट: banana

यहाँ index 1 के द्वारा “banana” को access किया गया।

6. List को Iteration करना:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

आउटपुट:

apple
banana
cherry

7. Slicing का उपयोग:

fruits = ["apple", "banana", "cherry", "mango"]
print(fruits[1:3])

आउटपुट: ['banana', 'cherry']

यहाँ list के sub-list को slicing द्वारा निकाला गया।

List का उपयोग कहाँ होता है?

1.      डेटा संग्रहित करना: जैसे छात्रों के नाम, उत्पादों की सूची, या संख्याओं का संग्रह।

2.      डेटा प्रोसेसिंग: एक सूची में डेटा जोड़ने, हटाने, या बदलने के लिए।

3.      डायनमिक डेटा स्टोरेज: जब डेटा की संख्या पहले से तय न हो।

4.      डेटा विश्लेषण: Sorting और searching algorithms में।

अगर आप इसे अपने छात्रों को सिखा रहे हैं, तो उन्हें एक mini-project बनाने के लिए प्रेरित करें, जैसे:

·         एक shopping cart program जिसमें items को जोड़ना और हटाना हो।

·         एक student marks management system जिसमें marks को add, remove और calculate किया जा सके।

Tuple data type

Python में tuple data type एक ordered और immutable (अपरिवर्तनीय) डेटा टाइप है, जिसका उपयोग विभिन्न प्रकार के डेटा items को एक साथ संग्रहित करने के लिए किया जाता है। Tuple को parentheses (
)
का उपयोग करके परिभाषित किया जाता है। Tuple lists की तरह होते हैं, लेकिन एक बार जब tuple बना दिया जाता है, तो इसके elements को बदला नहीं जा सकता।

मुख्य विशेषताएँ (Features of Tuple):

1.      Ordered: Tuple में डेटा items को एक specific क्रम में संग्रहीत किया जाता है।

2.      Immutable: Tuple के elements को बदला, जोड़ा या हटाया नहीं जा सकता।

3.      Heterogeneous: Tuple में विभिन्न प्रकार के डेटा (integer, string, float, आदि) संग्रहित किए जा सकते हैं।

4.      Indexed: Tuple के प्रत्येक item का एक index होता है, जो 0 से शुरू होता है।

5.      Efficient: Tuple की processing तेज़ होती है क्योंकि ये immutable होते हैं।

Tuple कैसे बनाते और उपयोग करते हैं?

1. साधारण Tuple बनाना:

my_tuple = (1, 2, 3)
print(my_tuple)

आउटपुट: (1, 2, 3)

यहाँ my_tuple एक tuple है जिसमें तीन integer elements हैं।

2. विभिन्न प्रकार के डेटा के साथ Tuple:

mixed_tuple = (10, "hello", 3.14, True)
print(mixed_tuple)

आउटपुट: (10, 'hello', 3.14, True)

यह tuple विभिन्न प्रकार के डेटा (integer, string, float और boolean) को स्टोर करता है।

3. Tuple को Index से Access करना:

fruits = ("apple", "banana", "cherry")
print(fruits[1])

आउटपुट: banana

यहाँ index 1 द्वारा “banana” को access किया गया है।

4. Tuple का Slicing:

fruits = ("apple", "banana", "cherry", "mango")
print(fruits[1:3])

आउटपुट: ('banana', 'cherry')

यहाँ slicing द्वारा sub-tuple निकाला गया।

5. Tuple में Single Element:

single_element = (5,)
print(single_element)

आउटपुट: (5,)

ध्यान दें कि single element वाले tuple में comma लगाना आवश्यक है, वरना यह tuple नहीं बल्कि integer माना जाएगा।

6. Tuple को Concatenate करना (जोड़ना):

tuple1 = (1, 2, 3)
tuple2 = (4, 5)
result = tuple1 + tuple2
print(result)

आउटपुट: (1, 2, 3, 4, 5)

Tuple का उपयोग कहाँ होता है?

1.      डेटा संग्रहित करना: जैसे कोऑर्डिनेट्स (x, y), या configuration settings

2.      डेटा स्थिरता सुनिश्चित करना: जब डेटा बदलना न हो।

3.      फिक्स्ड डेटा को स्टोर करना: जैसे महीने के नाम या सप्ताह के दिन।

4.      Functions में Return Multiple Values: Tuple का उपयोग functions में एक से अधिक मान लौटाने के लिए किया जाता है।

Dictionary data type

Python में dictionary data type एक unordered, mutable (बदलने योग्य), और indexed data type है, जिसमें data को key-value pairs के रूप में संग्रहीत किया जाता है। यह { } (curly brackets) का उपयोग करके परिभाषित किया जाता है। Dictionary में हर key यूनिक होनी चाहिए और key के माध्यम से उसके संबंधित value को access किया जाता है।

Dictionary की मुख्य विशेषताएँ (Features):

1.      Key-Value Pairs: Dictionary data को key-value pairs के रूप में स्टोर करता है।

2.      Unordered: Dictionary में items का कोई specific order नहीं होता।

3.      Mutable: इसमें नए items जोड़े जा सकते हैं या मौजूदा items को बदला जा सकता है।

4.      Indexed by Keys: Dictionary का access keys के द्वारा किया जाता है, न कि numerical index के द्वारा (जैसे lists में होता है)।

5.      Duplicate Keys Not Allowed: हर key यूनिक होनी चाहिए।

Dictionary कैसे बनाते और उपयोग करते हैं?

1. Dictionary बनाना:

student = {
    "name": "Ravi",
    "age": 21,
    "grade": "A"
}
print(student)

आउटपुट: {'name': 'Ravi', 'age':
21, 'grade': 'A'}

यहाँ student एक dictionary है जिसमें keys "name", "age", और "grade" हैं और उनकी values "Ravi", 21, और "A" हैं।

2. Dictionary से Value Access करना:

print(student["name"])

आउटपुट: Ravi

यहाँ key "name" का उपयोग करके उसकी value "Ravi" को access किया गया है।

3. नया Item जोड़ना:

student["city"] = "Delhi"
print(student)

आउटपुट: {'name': 'Ravi', 'age':
21, 'grade': 'A', 'city': 'Delhi'}

यहाँ "city" नाम की नई key और "Delhi" value dictionary में जोड़ी गई।

4. किसी Item को बदलना:

student["grade"] = "B"
print(student)

आउटपुट: {'name': 'Ravi', 'age':
21, 'grade': 'B', 'city': 'Delhi'}

यहाँ "grade" key की value को "A" से बदलकर "B" कर दिया गया।

5. Dictionary से Item हटाना:

student.pop("age")
print(student)

आउटपुट: {'name': 'Ravi',
'grade': 'B', 'city': 'Delhi'}

यहाँ "age" key और उसकी value को dictionary से हटा दिया गया।

6. Loop के साथ Dictionary:

for key, value in student.items():
    print(key, ":", value)

आउटपुट:

name : Ravi
grade : B
city : Delhi

Dictionary का उपयोग कहाँ होता है?

1.      डेटा को structured रूप में स्टोर करना: जैसे students की जानकारी, settings, या configurations

2.      JSON डेटा प्रोसेसिंग: APIs और वेब development में उपयोग।

3.      कुशलता से डेटा access करना: बड़ी मात्रा में डेटा के साथ काम करने के लिए।

4.      Mapping Relationships: जैसे शब्दों और उनके अर्थ, शहरों और उनकी जनसंख्या।

अगर आप इसे छात्रों को सिखाना चाहें, तो एक real-world project के रूप में dictionary का उपयोग करें, जैसे:

·         एक contact book application

·         एक inventory management system

Set data type

Python में set data type एक ऐसा डेटा टाइप है जो unordered और unindexed collection होता है, और इसमें unique items (values) संग्रहीत किए जाते हैं। Set का उपयोग तब किया जाता है जब आपको एक ही डेटा को बार-बार store नहीं करना हो और unique elements की आवश्यकता हो। इसे curly brackets {} का उपयोग करके बनाया जाता है, लेकिन इसमें duplicate items अपने आप हटा दिए जाते हैं।

मुख्य विशेषताएँ (Features of Set):

1.      Unordered: Set में items का कोई निर्धारित क्रम (order) नहीं होता।

2.      Unique Items: Set में हर item (element) यूनिक होता है।

3.      Mutable (बदलाव योग्य): Set के अंदर नए items जोड़े जा सकते हैं।

4.      Unindexed: Set के items को किसी specific index द्वारा access नहीं किया जा सकता।

Set कैसे बनाते और उपयोग करते हैं?

1. साधारण Set बनाना:

my_set = {1, 2, 3, 4}
print(my_set)

आउटपुट: {1, 2, 3, 4}

यहाँ my_set में चार unique elements हैं।

2. Duplicate Items को हटाना:

my_set = {1, 2, 2, 3, 4, 4}
print(my_set)

आउटपुट: {1, 2, 3, 4}

Set duplicate elements को अपने आप हटा देता है।

3. Set में नया Item जोड़ना:

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)

आउटपुट: {1, 2, 3, 4}

add() method का उपयोग करके नया element जोड़ा गया।

4. Set से Item हटाना:

my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set)

आउटपुट: {1, 2, 4}

remove() method का उपयोग करके 3 को set से हटा दिया गया।

5. Set Operations (Mathematical Operations जैसे Union और Intersection):

set1 = {1, 2, 3}
set2 = {3, 4, 5}
 
# Union
union_set = set1.union(set2)
print(union_set)
 
# Intersection
intersection_set = set1.intersection(set2)
print(intersection_set)

आउटपुट:

{1, 2, 3, 4, 5}  # Union
{3}  # Intersection

·         Union: दोनों sets के सभी unique elements को मिलाता है।

·         Intersection: दोनों sets में सामान्य (common) elements को निकालता है।

6. Set से Slicing और Indexing संभव नहीं है:

my_set = {1, 2, 3, 4}
print(my_set[1])  # यह error देगा क्योंकि set unindexed है।

Set का उपयोग कहाँ होता है?

1.      Duplicate Values को हटाने के लिए: जैसे किसी सूची में से unique items निकालना।

2.      Mathematical Operations: जैसे union, intersection, difference आदि।

3.      डेटा की तुलना करने के लिए: जैसे छात्रों की सूची में समान या भिन्न नामों को खोजना।

4.      High-Performance Lookup: जब performance महत्वपूर्ण हो।

उदाहरण Duplicate Values हटाने का कार्य:

items = [1, 2, 2, 3, 4, 4, 5]
unique_items = set(items)
print(unique_items)

आउटपुट: {1, 2, 3, 4, 5}

1. डिक्शनरी के नियम (Rules of Dictionary):

  1. Key यूनिक (Unique) होना चाहिए:
    • एक ही डिक्शनरी में दो keys एक जैसी नहीं हो सकतीं।
  2. Keys इम्युटेबल (Immutable) होती हैं:
    • Keys के तौर पर आप int, float, string, tuple वगैरह इस्तेमाल कर सकते हैं।
    • लेकिन list, set, dict जैसी म्यूटेबल चीज़ें keys नहीं बन सकतीं।
  3. Values कुछ भी हो सकती हैं:
    • Values किसी भी टाइप (int, list, string, dict, set आदि) की हो सकती हैं।
  4. डिक्शनरी अनऑर्डर्ड (Unordered) होती थी (Python 3.6 से पहले):
    • लेकिन अब (Python 3.7+ में) insertion order maintain होता है।
  5. {} ब्रैकेट का इस्तेमाल होता है:
    • डिक्शनरी को {} से बनाया जाता है।
  6. Key से Value को एक्सेस करते हैं:
    • हम key के जरिए उसकी value तक पहुँचते हैं।

2. डिक्शनरी बनाने का तरीका:

# खाली डिक्शनरी

d = {}

 

# डाटा के साथ

d = {‘name’: ‘Kamod’, ‘age’: 25, ‘city’: ‘Kokdi’}


3. डिक्शनरी के Important Methods:

Method

काम (Functionality)

dict.clear()

डिक्शनरी को खाली कर देता है।

dict.copy()

डिक्शनरी की shallow copy बनाता है।

dict.fromkeys(seq, val)

Keys का एक नया dict बनाता है।

dict.get(key, default)

Key की value देता है, अगर नहीं है तो default देता है।

dict.items()

(key, value) pairs देता है।

dict.keys()

सभी keys को देता है।

dict.pop(key, default)

Key को हटा देता है और उसकी value देता है।

dict.popitem()

आखिरी (key, value) pair हटा देता है।

dict.setdefault(key, default)

Key की value देता है, नहीं होने पर default डालता है।

dict.update(other_dict)

दूसरी डिक्शनरी से डाटा जोड़ता है।

dict.values()

सभी values देता है।


4. छोटेछोटे Examples:

 

d = {‘name’: ‘Kamod’, ‘age’: 25}

 

# get()

print(d.get(‘name’))       # Output: Kamod

print(d.get(‘city’, ‘NA’)) # Output: NA

 

# update()

d.update({‘city’: ‘Kokdi’})

print(d)  # {‘name’: ‘Kamod’, ‘age’: 25, ‘city’: ‘Kokdi’}

 # keys()

print(d.keys())  # dict_keys([‘name’, ‘age’, ‘city’])

 # values()

print(d.values())  # dict_values([‘Kamod’, 25, ‘Kokdi’])

 # items()

print(d.items())  # dict_items([(‘name’, ‘Kamod’), (‘age’, 25), (‘city’, ‘Kokdi’)])

 # pop()

d.pop(‘age’)

print(d)  # {‘name’: ‘Kamod’, ‘city’: ‘Kokdi’}


 

 डिक्शनरी में कुछ और महत्वपूर्ण चीजें हैं जो आपको ज़रूर सीखनी चाहिए


1. Nested Dictionary (डिक्शनरी के अंदर डिक्शनरी)

  • जब एक डिक्शनरी के अंदर दूसरी डिक्शनरी हो।

students = {

    ‘101’: {‘name’: ‘Kamod’, ‘course’: ‘Python’},

    ‘102’: {‘name’: ‘Rahul’, ‘course’: ‘Java’}

}

 

print(students[‘101’][‘name’])  # Output: Kamod

Nested Dictionary बहुत बड़े प्रोजेक्ट्स में काम आती है जैसे Student Management System वगैरह में।


2. Dictionary Comprehension

  • जैसे List Comprehension होता है, वैसे ही डिक्शनरी के लिए भी होता है।

# Example: Numbers की squares की dictionary बनाना

squares = {x: x*x for x in range(1, 6)}

print(squares)

# Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

फ़ायदा: कोड छोटा और सुंदर हो जाता है।


3. Common Use-Cases

  • Word Count Program:
    टेक्स्ट में हर शब्द कितनी बार आया, ये Dictionary से आसानी से गिन सकते हैं।
  • Data Storage:
    किसी भी चीज़ का structured data स्टोर करना हो तो dictionary बहुत काम आती है।

 

4. Important Concept: Mutable Nature

  • Dictionary mutable होती है यानी उसमें बदलाव किया जा सकता है।

d = {‘name’: ‘Kamod’}

d[‘name’] = ‘Amit’  # Value बदल दी गई

print(d)  # Output: {‘name’: ‘Amit’}


5. Error Handling with Dictionary

  • अगर आप Directly key से access करोगे और वो नहीं मिली तो Error आएगा।
  • इसलिए हमेशा get() method से access करना safe रहता है।

d = {‘name’: ‘Kamod’}

print(d.get(‘age’, ‘Not Found’))  # Output: Not Found


6. Looping Through Dictionary

  • Keys, Values, और दोनों के लिए अलगअलग तरीके से loop लगा सकते हैं:

d = {‘name’: ‘Kamod’, ‘age’: 25}

 

# Only keys

for key in d:

    print(key)

 

# Keys and values

for key, value in d.items():

    print(f'{key}: {value}‘)


7. Important Built-in Functions for Dictionary

Function

काम

len(d)

कितनी key-value pairs हैं

sorted(d)

Keys को sorted list में देता है

str(d)

Dictionary को String में बदलता है


 

 

8. Copy vs Deepcopy

  • अगर डिक्शनरी के अंदर nested डिक्शनरी हो, तो copy() shallow copy बनाता है,
    लेकिन अगर आपको पूरा नया independent copy चाहिए तो deepcopy() इस्तेमाल करते हैं (module copy से लेना पड़ता है)

import copy

d1 = {‘a’: {‘b’: 1}}

d2 = copy.deepcopy(d1)


9. Setdefault() का सही इस्तेमाल

  • अगर कोई key नहीं है और उसे default value से जोड़ना है तो setdefault() काम आता है।

d = {‘name’: ‘Kamod’}

d.setdefault(‘age’, 25)

print(d)  # {‘name’: ‘Kamod’, ‘age’: 25}


10. Practical Tip: Dictionary का इस्तेमाल कब करें?

  • जब आपको key-value pair में organized और fast data retrieval चाहिए।
  • जैसे Students का डेटा, Employees का रिकॉर्ड, Product details, etc.

Python में 5 छात्रों का dictionary बनाने के लिए, आप एक dictionary का उपयोग कर सकते हैं जिसमें प्रत्येक छात्र का विवरण (जैसे नाम, रोल नंबर, ग्रेड आदि) शामिल हो। यहाँ एक उदाहरण दिया गया है:

उदाहरण: 5 छात्रों का dictionary

# 5 छात्रों का dictionary

students = {

    1: {“name”: “Rahul”, “roll_no”: 101, “grade”: “A”},

    2: {“name”: “Priya”, “roll_no”: 102, “grade”: “B”},

    3: {“name”: “Amit”, “roll_no”: 103, “grade”: “A”},

    4: {“name”: “Sneha”, “roll_no”: 104, “grade”: “C”},

    5: {“name”: “Rohit”, “roll_no”: 105, “grade”: “B”}

}

 

# Dictionary प्रिंट करें

print(students)

Output:

python

{

    1: {‘name’: ‘Rahul’, ‘roll_no’: 101, ‘grade’: ‘A’},

    2: {‘name’: ‘Priya’, ‘roll_no’: 102, ‘grade’: ‘B’},

    3: {‘name’: ‘Amit’, ‘roll_no’: 103, ‘grade’: ‘A’},

    4: {‘name’: ‘Sneha’, ‘roll_no’: 104, ‘grade’: ‘C’},

    5: {‘name’: ‘Rohit’, ‘roll_no’: 105, ‘grade’: ‘B’}

}

 📘 Python में Loop (लूप) – सम्पूर्ण जानकारी

🔰 लूप क्या होता है?

Loop का उपयोग किसी कोड को बारबार दोहराने के लिए किया जाता है, जब तक कोई निश्चित शर्त (condition) पूरी हो जाए।


🔄 Python में लूप के प्रकार

Python में मुख्यतः दो प्रकार के लूप होते हैं:

  1. for loop
  2. while loop

1 for loop

उपयोग:

जब हमें किसी कलेक्शन (list, string, tuple, range आदि) के हर आइटम पर काम करना हो।

📌 सिंटैक्स:

for variable in sequence:

    # statements

🧪 उदाहरण:

for i in range(5):

    print(i)

📤 Output:

0

1

2

3

4


🔹 range() फंक्शन:

Syntax

Description

range(n)

0 से n-1 तक

range(start, end)

start से end-1 तक

range(start, end, step)

step के अंतर से

📍उदाहरण:

for i in range(1, 11, 2):

    print(i)

📤 Output:

1

3

5

7

9


2    while loop

उपयोग:

जब हमें लूप तब तक चलाना है जब तक कोई शर्त True है।

📌 सिंटैक्स:

while condition:

    # statements

🧪 उदाहरण:

i = 1

while i <= 5:

    print(i)

    i += 1

📤 Output:

1

2

3

4

5


🔃 लूप कंट्रोल स्टेटमेंट्स

1. break – लूप को तुरंत रोक देता है।

for i in range(5):

    if i == 3:

        break

    print(i)

📤 Output:

0

1

2


2. continue – वर्तमान iteration को छोड़कर अगली पर जाता है।

for i in range(5):

    if i == 3:

        continue

    print(i)

📤 Output:

0

1

2

4


3. else with loop

else ब्लॉक तब चलता है जब लूप पूरा हो जाता है (break से नहीं रुका हो)

for i in range(3):

    print(i)

else:

    print(लूप समाप्त)

📤 Output:

0

1

2

लूप समाप्त


🌀 Infinite Loop (अनंत लूप)

जब लूप की condition हमेशा True हो।

while True:

    print(यह एक Infinite Loop है)

    break  # बिना break के यह रुकेगा नहीं


🧠 उपयोगी अभ्यास:

  1. 1 से 10 तक के नंबर प्रिंट करें।
  2. किसी string के हर अक्षर को प्रिंट करें।
  3. एक list के सभी items को print करें।
  4. केवल even नंबर print करें (1 से 20 तक)
  5. while loop से टेबल बनाएं।

 

 

Python में Loop के बारे में सम्पूर्ण नोट्स

Python में loop का उपयोग बारबार दोहराए जाने वाले कार्यों को करने के लिए किया जाता है। यह कोड को अधिक कुशल और संक्षिप्त बनाता है। Python में मुख्य रूप से for loop और while loop का उपयोग किया जाता है।

1. Loop क्या होता है?

Loop एक नियंत्रण संरचना (Control Structure) है जो किसी कार्य को तब तक दोहराता है जब तक कि एक निश्चित शर्त पूरी हो जाए।

लूप के प्रकार:

  1. For Loop
  2. While Loop
  3. Nested Loop
  4. Loop Control Statements (Break, Continue, Pass)

2. For Loop

for loop का उपयोग एक अनुक्रम (sequence) जैसे list, tuple, dictionary, या range में प्रत्येक आइटम पर पुनरावृत्ति (iteration) करने के लिए किया जाता है।

सिंटैक्स:

for variable in sequence:

    # कोड ब्लॉक

उदाहरण:

for i in range(5):

    print(“Hello, Python!”)

आउटपुट:

Hello, Python!

Hello, Python!

Hello, Python!

Hello, Python!

Hello, Python!

3. While Loop

while loop तब तक चलता रहता है जब तक उसकी शर्त (condition) सही होती है।

सिंटैक्स:

while condition:

    # कोड ब्लॉक

उदाहरण:

python

i = 1

while i <= 5:

    print(i)

    i += 1

आउटपुट:

1

2

3

4

5

4. Nested Loop

जब एक loop के अंदर दूसरा loop होता है, तो उसे Nested Loop कहा जाता है।

उदाहरण:

for i in range(3):

    for j in range(2):

        print(f”i={i}, j={j}”)

आउटपुट:

i=0, j=0

i=0, j=1

i=1, j=0

i=1, j=1

i=2, j=0

i=2, j=1

5. Loop Control Statements

Python में loop को नियंत्रित करने के लिए निम्नलिखित control statements का उपयोग किया जाता है:

  • Break: Loop को तुरंत समाप्त करता है।
  • Continue: Loop के अगले iteration पर जाता है।
  • Pass: कुछ नहीं करता, बस एक placeholder की तरह कार्य करता है।

उदाहरण (Break और Continue):

for i in range(10):

    if i == 5:

        break  # Loop समाप्त हो जाएगा जब i = 5 होगा

    print(i)

आउटपुट:

0

1

2

3

4

for i in range(10):

    if i % 2 == 0:

        continue  # केवल विषम संख्याएँ ही प्रिंट होंगी

    print(i)

आउटपुट:

1

3

5

7

9

निष्कर्ष

Python में loops कोड को अधिक प्रभावी और उपयोगी बनाते हैं। चाहे वह for loop हो या while loop, यह दोनों पुनरावृति प्रक्रिया को सरल बनाते हैं। Loop control statements से loop को नियंत्रित किया जा सकता है जिससे कोड अधिक प्रभावी और स्पष्ट बनता है।

 

 

Functions in Python (फंक्शन क्या होता है?)

 

Functions और Recursion Python के बहुत ही important topics हैं। मैं आपको हिंदी में, आसान भाषा और उदाहरण के साथ समझाता हूँ।


Functions in Python (फंक्शन क्या होता है?)

🔸 Definition:

Function एक block of code होता है, जो किसी काम को करने के लिए बारबार इस्तेमाल किया जा सकता है।

🔸 Syntax:

def function_name(parameters): # Code block return value

🔸 Example:

def greet(name): print(“Hello”, name) greet(“Kamod”) # Output: Hello Kamod greet(“Amit”) # Output: Hello Amit


✅ Functions के फायदे:

1.       Code को बारबार लिखने की जरूरत नहीं।

2.       Code readable और manageable होता है।

3.       Code को अलगअलग हिस्सों में बाँट सकते हैं।


🔷 Types of Functions

1.       Built-in Functions – जैसे print()len()type() आदि।

2.       User-defined Functions – जो हम खुद बनाते हैं, जैसे ऊपर greet()


🔷 Function with Return Value

def add(a, b): return a + b result = add(5, 3) print(result) # Output: 8


🔷 Default Arguments Example

def greet(name=”User”): print(“Hello”, name) greet() # Output: Hello User greet(“Kamod”) # Output: Hello Kamod


 

 

Recursion in Python (Recursive Function)

🔸 Definition:

जब कोई function खुद को ही call करता है, तो उसे Recursion कहते हैं।

🔸 Example: Factorial using Recursion

def factorial(n): if n == 0 or n == 1: return 1 else: return n * factorial(n – 1) print(factorial(5)) # Output: 120

🔸 कैसे काम करता है?

factorial(5)

=> 5 * factorial(4)

=> 5 * 4 * factorial(3)

=> 5 * 4 * 3 * factorial(2)

=> 5 * 4 * 3 * 2 * factorial(1)

=> 5 * 4 * 3 * 2 * 1 = 120


ध्यान देने वाली बातें:

1.       Recursion में हमेशा एक base case होना चाहिए, नहीं तो infinite loop हो जाएगा।

2.       Function call stack memory में store होता हैज्यादा recursion से error सकती है (RecursionError)


🎯 Practice Questions:

1.       एक function लिखो जो 2 नंबर का जोड़ करे।

2.       Recursive function बनाओ जो किसी नंबर का factorial निकाले।

3.       Fibonacci series के लिए recursive function बनाओ।


 

 

 

Example  – Function with Return

def add(a, b): return a + b result = add(4, 5) print(result) # Output: 9


Types of Functions

Type

Example

Built-in Functions

print()len()

User-defined

def my_func():


6. Default Arguments

def greet(name=”User”): print(“Hello”, name) greet() # Output: Hello User greet(“Amit”) # Output: Hello Amit


7. Keyword Arguments

def student(name, age): print(name, age) student(age=18, name=“Kamod”) # Output: Kamod 18


8. Variable Length Arguments

def total(*numbers): return sum(numbers) print(total(1, 2, 3, 4)) # Output: 10


9. Return Statement

·         Function से value वापस भेजने के लिए return का उपयोग होता है।


10. Recursion (Recursive Function)

Definition:

जब कोई function खुद को ही call करता है, उसे Recursion कहते हैं।


🔢 Example: Factorial using Recursion

def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n – 1) print(factorial(5)) # Output: 120


🧠 कैसे काम करता है?

factorial(5)

=> 5 * factorial(4)

=> 5 * 4 * factorial(3)

=> 5 * 4 * 3 * factorial(2)

=> 5 * 4 * 3 * 2 * factorial(1)

=> 5 * 4 * 3 * 2 * 1 = 120


🛑 Base Case क्यों ज़रूरी है?

Recursion infinite ना हो जाए, इसके लिए एक base condition बहुत ज़रूरी होती है।


🔁 Fibonacci Series using Recursion

def fib(n): if n == 0: return 0 elif n == 1: return 1 return fib(n-1) + fib(n-2) for i in range(6): print(fib(i), end=” “) # Output: 0 1 1 2 3 5


 

📝 Practice Questions

1.       दो नंबर को जोड़ने वाला function बनाइए।

2.       factorial का recursive function बनाइए।

3.       Fibonacci series का recursive function बनाइए।

4.       default और keyword arguments के साथ function बनाइए।


📌 Extra Tips

·         Function के नाम meaningful रखें।

·         DRY Principle अपनाएँ: Don’t Repeat Yourself

·         Recursive function से पहले सोचे कि base case क्या होगा।


✅ Conclusion:

·         Function से code clean और reusable होता है।

·         Recursion powerful है लेकिन ध्यान से use करें।

·         Practice करते रहेंयही mastery की कुंजी है!


✅ Python Functions की पूरी लिस्ट (with Categories)


🔹 1. Built-in Functions (इनबिल्ट फंक्शन्स)

Function

Description

print()

Output दिखाने के लिए

input()

यूजर से इनपुट लेने के लिए

len()

Length (length of string, list etc.)

type()

Data type जानने के लिए

int()

किसी वैल्यू को Integer में बदलना

str()

किसी वैल्यू को String में बदलना

float()

Float में कन्वर्ट करना

bool()

Boolean वैल्यू (True/False) देना

range()

Sequence generate करने के लिए

sum()

List या Tuple का Total निकालना

max()

सबसे बड़ा नंबर

min()

सबसे छोटा नंबर

sorted()

Sorting करने के लिए

reversed()

उल्टा क्रम (reverse) करने के लिए

abs()

Absolute value

round()

Decimal round-off

enumerate()

Index के साथ loop करने के लिए

zip()

दो lists को pair करने के लिए


🔹 2. String Functions (स्ट्रिंग फंक्शन्स)

Function

Description

upper()

सब कुछ uppercase में

lower()

सब कुछ lowercase में

capitalize()

पहला अक्षर capital

title()

हर शब्द का पहला अक्षर capital

strip()

दोनों तरफ के spaces हटाना

lstrip()rstrip()

बाएँ या दाएँ spaces हटाना

replace(old, new)

Replace words

find()

Index खोजने के लिए

count()

कितनी बार कोई substring आया

split()

स्ट्रिंग को तोड़ना (list में)

join()

List को जोड़कर string बनाना

startswith()

चेक करें क्या string किसी चीज़ से शुरू हो रहा है

endswith()

चेक करें क्या string किसी चीज़ पर खत्म हो रहा है


🔹 3. List Functions (लिस्ट से जुड़े फंक्शन्स)

Function

Description

append()

एक item जोड़ना

insert(i, val)

किसी position पर item डालना

extend()

दूसरी list को जोड़ना

pop()

आखिरी item हटाना

remove()

कोई specific item हटाना

clear()

पूरी list को empty करना

index()

किसी item की index निकालना

count()

किसी item की frequency

sort()

ascending order में sort करना

reverse()

list को उल्टा करना


🔹 4. Math Functions (math module के functions)

import math

Function

Description

math.sqrt(x)

Square root

math.pow(x, y)

Power (x^y)

math.floor(x)

नीचे की पूरी संख्या

math.ceil(x)

ऊपर की पूरी संख्या

math.pi

3.1415…

math.factorial(x)

फैक्टोरियल निकालना

math.log(x)

लॉगरिथ्म


🔹 5. User-Defined Functions (खुद के बनाए गए फंक्शन)

def my_function(): # code

Types:

·         Simple function

·         Function with arguments

·         Function with return

·         Default arguments

·         Keyword arguments

·         Variable length arguments (*args**kwargs)

·         Recursive functions


🔹 6. Lambda (Anonymous) Function

square = lambda x: x * x print(square(5)) # Output: 25


🔹 7. Higher Order Functions

Function

Description

map()

सभी items पर function apply करना

filter()

Condition के अनुसार filter करना

reduce()

Items को जोड़ते हुए single value बनाना

 

from functools import reduce


 

 

🔹 8. File Functions (फाइल से जुड़ी)

Function

Description

open()

File खोलना

read()

File पढ़ना

write()

File में लिखना

close()

File बंद करना

readline()

एक लाइन पढ़ना


🔹 9. Date & Time Functions (datetime module)

import datetime

Function

Description

datetime.now()

Current date & time

datetime.today()

सिर्फ current date

datetime.strptime()

string को datetime में बदलना

strftime()

datetime को string में बदलना


✅ Conclusion:

Python में लगभग 70+ useful functions हैं जो daily coding में काम आते हैं।

 

✅ Python Functions: Short Explanation with Examples


🔷 1. Built-in Functions

Function

Explanation

Example

print()

आउटपुट दिखाने के लिए

print(“Hello”) → Hello

input()

यूजर से इनपुट लेने के लिए

x = input(“Name: “)

len()

लंबाई निकालने के लिए

len(“Kamod”) → 5

type()

Data type जानने के लिए

type(5) → <class ‘int’>

int()

integer में बदलने के लिए

int(“5”) → 5

float()

float में बदलने के लिए

float(“5”) → 5.0

str()

string में बदलने के लिए

str(5) → “5”

sum()

सभी नंबर जोड़ने के लिए

sum([1,2,3]) → 6

max()

सबसे बड़ा नंबर

max([4,2,8]) → 8

min()

सबसे छोटा नंबर

min([4,2,8]) → 2

sorted()

list को sort करता है

sorted([3,1,2]) → [1,2,3]

abs()

absolute value देता है

abs(-5) → 5

round()

Decimal value round करता है

round(4.6) → 5


🔷 2. String Functions

Function

Explanation

Example

upper()

सब uppercase कर देता है

“kamod”.upper() → “KAMOD”

lower()

सब lowercase कर देता है

“HELLO”.lower() → “hello”

capitalize()

पहला अक्षर बड़ा करता है

“kamod”.capitalize() → “Kamod”

strip()

दोनों ओर के spaces हटाता है

” hi “.strip() → “hi”

replace()

text बदलने के लिए

“hi kamod”.replace(“hi”, “hello”) → “hello kamod”

find()

सबटेक्स्ट की पोजिशन देता है

“hello”.find(“e”) → 1

split()

space या separator से अलग करता है

“a,b,c”.split(“,”) → [‘a’,’b’,’c’]


🔷 3. List Functions

Function

Explanation

Example

append()

एक item लिस्ट में जोड़ता है

lst.append(4)

insert()

किसी position पर item डालता है

lst.insert(1, “x”)

extend()

दूसरी लिस्ट जोड़ता है

lst.extend([5,6])

pop()

आखिरी item हटाता है

lst.pop()

remove()

किसी item को हटाता है

lst.remove(3)

sort()

ascending order में sort करता है

lst.sort()

reverse()

उल्टा कर देता है

lst.reverse()


🔷 4. Math Functions

import math

Function

Explanation

Example

math.sqrt()

स्क्वायर रूट निकालता है

math.sqrt(25) → 5.0

math.pow()

पावर निकालता है (x^y)

math.pow(2,3) → 8.0

math.floor()

नीचे की पूरी संख्या देता है

math.floor(3.8) → 3

math.ceil()

ऊपर की पूरी संख्या देता है

math.ceil(3.2) → 4

math.factorial()

फैक्टोरियल देता है

math.factorial(5) → 120


🔷 5. User-Defined Functions

def greet(name): print(“Hello”, name) greet(“Kamod”) # Output: Hello Kamod


🔷 6. Lambda Function (Short Function)

square = lambda x: x * x print(square(4)) # Output: 16


🔷 7. Higher Order Functions

Function

Explanation

Example

map()

सभी items पर function apply करता है

map(lambda x: x*2, [1,2]) → [2,4]

filter()

condition से filter करता है

filter(lambda x: x>2, [1,2,3]) → [3]

reduce()

सबको जोड़कर एक value देता है

reduce(lambda x,y:x+y, [1,2,3]) → 6


 

 

8. File Handling Functions

f = open(“file.txt”, “r”) data = f.read() f.close()

Function

Explanation

open()

फाइल खोलता है

read()

फाइल पढ़ता है

write()

फाइल में लिखता है

close()

फाइल बंद करता है


🔷 9. Recursive Function (Self calling)

def factorial(n): if n == 0: return 1 return n * factorial(n – 1) print(factorial(5)) # Output: 120


File Handling in Python

File Handling kya hota hai?

File handling ka matlab hota hai kisi file ko Python program ke through read, write ya update karna. Ye bahut useful hota hai jab aapko data store ya process karna ho.

File Open karna (Read Mode):

file = open("data.txt", "r")
content = file.read()
print(content)
file.close()

File Write karna (Write Mode):

file = open("data.txt", "w")
file.write("Hello Kamod")
file.close()

Note: Write mode purani file ko overwrite karta hai.

File Append karna (Add new data):

file = open("data.txt", "a")
file.write("\nWelcome to Python")
file.close()

With statement ka use (Best Practice):

with open("data.txt", "r") as file:
    print(file.read())

Iska fayda: File automatically close ho jati hai.

File Modes:

  • “r” – Read

  • “w” – Write

  • “a” – Append

  • “r+” – Read and Write

Conclusion:

File handling se hum external files ke sath interact kar sakte hain. Ye real-world Python projects ke liye important concept hai.

 

Chapter: Input and Output in Python (इनपुट और आउटपुट)


🔹 1. print() Function – Output दिखाने के लिए

Python में आउटपुट दिखाने के लिए print() function का उपयोग होता है।

Basic Example:

print(“Hello Kamod”)     # Output: Hello Kamod

print(5 + 3)             # Output: 8

Multiple items print करना:

name = “Kamod”

age = 25

print(“Name:”, name, “Age:”, age)

# Output: Name: Kamod Age: 25


🔹 2. input() Function – यूजर से इनपुट लेने के लिए

Python में यूज़र से डेटा लेने के लिए input() function का उपयोग होता है।

Example 1:

name = input(“Enter your name: “)

print(“Welcome”, name)

Example 2 – Number Input:

num = input(“Enter a number: “)

print(“You entered:”, num)

Important: input() से लिया गया डेटा always string होता है।

तो यदि आपको integer चाहिए, तो convert करना होगा:

a = int(input(“Enter first number: “))

b = int(input(“Enter second number: “))

print(“Total:”, a + b)


🔹 3. Formatting Output – format() और f-string

Using format():

name = “Kamod”

marks = 90

print(“Student {} got {} marks.”.format(name, marks))

# Output: Student Kamod got 90 marks.

Using f-string (Python 3.6+):

name = “Kamod”

marks = 90

print(f”Student {name} got {marks} marks.”)


🔹 4. sep और end Parameters in print()

sep = Separator between multiple items:

print(“Kamod”, “Kumar”, sep=“-“)   # Output: Kamod-Kumar

end = Line खत्म होने पर क्या हो:

print(“Hello”, end=” “)

print(“World”)

# Output: Hello World


🔹 5. Taking Multiple Inputs

One-line input (split):

a, b = input(“Enter two numbers: “).split()

print(“a =”, a, “b =”, b)

Convert to int:

a, b = map(int, input(“Enter 2 numbers: “).split())

print(“Sum:”, a + b)


🔹 6. Taking List Input from User

nums = list(map(int, input(“Enter numbers: “).split()))

print(nums)


✅ Summary:

Task

Method

Output दिखाना

print()

Input लेना

input()

Number input लेना

int(input())

Multiple input (string)

input().split()

Multiple input (int)

map(int, input().split())

Output formatting

format() / f-string

Custom separator or end

print(…, sep=” “, end=””)


✅ Python Input/Output Practice Questions


Q1. यूज़र से उसका नाम इनपुट लो और उसे Welcome message दो।

Input: “Kamod”
Output:
“Welcome Kamod!”

Hint: input() + print()


Q2. दो संख्याएँ इनपुट लेकर उनका योग (sum) निकालो।

Input: 5, 10
Output:
Total: 15

Hint: int(input()) और +


Q3. एक लाइन में तीन संख्याएँ लो और उनका औसत (average) निकालो।

Input: 10 20 30
Output:
Average: 20.0

Hint: map(int, input().split())


Q4. यूज़र से उसका नाम और उम्र लो और इस फॉर्मेट में प्रिंट करो

Output: “Hello Kamod, You are 25 years old.”

Hint: f-string


Q5. यूज़र से एक वाक्य लो और उसके शब्दों की संख्या निकालो।

Input: “I am learning Python”
Output:
Word Count: 4

Hint: split() + len()


Q6. यूज़र से कोई नंबर लो और बताओ वो odd है या even

Input: 7
Output:
Odd number

Hint: % operator


Q7. एक लिस्ट की तरह इनपुट लो और प्रिंट करो।

Input: 1 2 3 4 5
Output:
[1, 2, 3, 4, 5]

Hint: list(map(int, input().split()))


Q8. यूज़र से नाम और अंक लो और फॉर्मेटेड रिजल्ट दो

Output: “Student Kamod scored 95 marks.”

Hint: format() या f-string


Q9. यूज़र से दो शब्द लो और उनके बीच में लगाकर प्रिंट करो।

Input: “Digital”, “Center”
Output:
“Digital-Center”

Hint: print(…, sep=”-“)


Q10. यूज़र से कोई टेक्स्ट लो और बताओ उसमें कितने अक्षर (characters) हैं।

Input: “Learn Python”
Output:
Total Characters: 12

Hint: len()

✅ Python Input/Output Practice Questions – With Solutions


Q1. Welcome message

name = input(“Enter your name: “)

print(“Welcome”, name + “!”)


Q2. दो संख्याओं का योग

a = int(input(“Enter first number: “))

b = int(input(“Enter second number: “))

print(“Total:”, a + b)


Q3. तीन नंबरों का औसत (average)

a, b, c = map(int, input(“Enter three numbers: “).split())

avg = (a + b + c) / 3

print(“Average:”, avg)


Q4. नाम और उम्र के साथ मैसेज

name = input(“Enter your name: “)

age = input(“Enter your age: “)

print(f”Hello {name}, You are {age} years old.”)


Q5. शब्दों की गिनती

sentence = input(“Enter a sentence: “)

words = sentence.split()

print(“Word Count:”, len(words))


Q6. Odd या Even चेक करना

num = int(input(“Enter a number: “))

if num % 2 == 0:

    print(“Even number”)

else:

    print(“Odd number”)


Q7. लिस्ट इनपुट लेना

nums = list(map(int, input(“Enter numbers: “).split()))

print(nums)


Q8. नाम और अंक के साथ स्टेटमेंट

name = input(“Enter student name: “)

marks = input(“Enter marks: “)

print(f”Student {name} scored {marks} marks.”)


Q9. दो शब्दों को जोड़ना (with )

word1 = input(“Enter first word: “)

word2 = input(“Enter second word: “)

print(word1, word2, sep=“-“)


Q10. टेक्स्ट में अक्षरों की संख्या

text = input(“Enter text: “)

print(“Total Characters:”, len(text))


  

✅ OOPs in Python – Objects and Classes (with Examples in Hindi)


1. Object-Oriented Programming (OOP) क्या है?

OOP एक programming style है जो objects और classes के concepts पर आधारित होती है।

OOPs के 4 मुख्य सिद्धांत (Principles):

  1. Class and Object
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

2. Class क्या है?

Class एक blueprint या design है जिससे हम objects बनाते हैं।

Syntax:

class ClassName:

    # attributes + methods

Example:

class Student:

    def __init__(self, name, age):

        self.name = name

        self.age = age

     def show(self):

        print(f”Name: {self.name}, Age: {self.age}“)


3. Object क्या होता है?

Object = Class का real example होता है।
जैसे अगर class “Student” है, तो Ram, Shyam, Komal = objects हैं।

Object बनाना:

s1 = Student(“Kamod”, 22)     # object of Student class

s1.show()                     # Output: Name: Kamod, Age: 22


4. __init__() Method क्या है?

यह एक constructor होता है जो object बनते ही auto run होता है।
इसमें हम object की starting values set करते हैं।

Example:

def __init__(self, name, age):

    self.name = name

    self.age = age


5. self क्या होता है?

self का मतलब होता हैcurrent object का reference
Python
में method के अंदर object की properties को access करने के लिए self लिखा जाता है।


पूरा Example समझें:

class Student:

    def __init__(self, name, course):

        self.name = name

        self.course = course

     def show(self):

        print(f”Student Name: {self.name}“)

        print(f”Course Enrolled: {self.course}“)

 # Object banayein

s1 = Student(“Kamod”, “Python”)

s2 = Student(“Amit”, “Tally”)

 # Method call karein

s1.show()

s2.show()

Output:

Student Name: Kamod

Course Enrolled: Python

Student Name: Amit

Course Enrolled: Tally


✅ Summary (सारांश):

Term

Meaning (हिन्दी में)

Class

Design/Template (खाका)

Object

Class का उदाहरण (Example)

__init__

Constructor (Object बनते ही चलता है)

self

Current Object का Reference

Method

Class के अंदर function


सबसे पहले सोचिए

आपके कंप्यूटर सेंटर में हर स्टूडेंट की जानकारी होती हैजैसे:

  • name
  • course
  • age

अब मान लीजिए आपको Python में हर स्टूडेंट का डेटा स्टोर करना है।
तो आप हर बार बारबार code नहीं लिखेंगे।
एक बार एक “Class” बनाइए, फिर जितने चाहें उतने “Student” बना लीजिए।


1. Class क्या है?

Class = Design या Template
जैसे: “Student” एक class है, जिसमें हम तय करेंगे कि
हर स्टूडेंट में क्याक्या चीज़ होंगीनाम, उम्र, कोर्स।

class Student:

    pass


2. Object क्या है?

Object = असली स्टूडेंट
Class
तो बस Design है, Object असली चीज़ है।

s1 = Student()   # ये एक स्टूडेंट है (Object)

s2 = Student()   # दूसरा स्टूडेंट


3. __init__() क्या करता है?

जब आप एक नया Object बनाते हैं, तो Python सबसे पहले __init__() function को चलाता है।

ये function automatically call होता हैऔर उसी में हम data डालते हैं।

class Student:

    def __init__(self, name, course):

        self.name = name

        self.course = course

यहाँ self.name और self.course में डेटा स्टोर हो रहा है।


4. self क्या होता है?

self का मतलब होता हैजिस Object के लिए काम हो रहा है
यानि अभी हम किस Student के बारे में बात कर रहे हैं।

def show(self):

    print(“Name:”, self.name)

मतलब जो स्टूडेंट अभी active है, उसी का नाम print करो।


पूरा Example – देसी तरीका:

class Student:

    def __init__(self, naam, course):

        self.naam = naam

        self.course = course

     def show(self):

        print(“Naam:”, self.naam)

        print(“Course:”, self.course)

 # अब Students बनाएं

s1 = Student(“Kamod”, “Python”)

s2 = Student(“Ravi”, “Tally”)

 # दोनों का डेटा दिखाएं

s1.show()

s2.show()

Output:

Naam: Kamod

Course: Python

Naam: Ravi

Course: Tally


इसको ऐसे समझिए:

Concept

Example (देसी भाषा में)

Class

जैसे एक Form या Template बनाना

Object

उस Form को भरकर असली स्टूडेंट बना देना

_init__

Form भरने के टाइम पे जो फील्ड भरते हैं

self

अभी किस स्टूडेंट की बात हो रही है, उसका नाम


✅ Encapsulation और Inheritance in Python (with Simple Examples)


1. Encapsulation (एन्कैप्सुलेशन)

मतलब:

Encapsulation का मतलब होता है
“Data
को छुपाना और सुरक्षित रखना ताकि कोई बाहर वाला सीधा data छेड़ पाए।


Python में कैसे करते हैं?

Python में हम किसी variable के आगे _ (protected) या __ (private) लगा देते हैं।

Example:

class Student:

    def __init__(self, name, fees):

        self.name = name

        self.__fees = fees   # private variable

    def show(self):

        print(“Name:”, self.name)

        print(“Fees:”, self.__fees)   # access inside class

 s1 = Student(“Kamod”, 5000)

s1.show()

 # बाहर से access नहीं कर सकते:

# print(s1.__fees)   ❌ Error देगा


Encapsulation का फायदा:

  • Data सुरक्षित रहता है
  • Direct बाहर से कोई change नहीं कर सकता
  • सिर्फ class के अंदर ही access possible है

2. Inheritance (इनहेरिटेंस)

Inheritance का मतलब होता है
बच्चा अपने मातापिता से गुण (features) लेता है
Programming
में भी एक Class दूसरी Class की properties ले सकती है।


Example:

class Student:

    def __init__(self, name):

        self.name = name

     def show(self):

        print(“Name:”, self.name)

 # ये दूसरी class पहली वाली से गुण ले रही है

class OnlineStudent(Student):

    def display_mode(self):

        print(“Mode: Online Class”)

 

s1 = OnlineStudent(“Kamod”)

s1.show()          # Student class से मिला

s1.display_mode()  # अपनी खुद की method


Inheritance के फायदे:
  • Code बारबार नहीं लिखना पड़ता
  • एक बार बनाकर reuse कर सकते हैं
  • नया class पुराने वाले को extend कर सकता है

✅ Summary Table

Concept

मतलब

Python में क्या होता है?

Encapsulation

Data को छुपाकर safe रखना

__variable और methods के ज़रिये

Inheritance

एक class दूसरी class के गुण लेता है

class B(A): मतलब B class, A class से गुण ले रहा है