Conditional Statements in Python क्या हैं?
Table of Contents
Introduction to Conditional Statements in Hindi
Python में जब program को यह decide करना होता है कि किसी condition के सही (True) या गलत (False) होने पर कौन सा code चलाया जाए, तो इसके लिए Conditional Statements का इस्तेमाल किया जाता है।
आसान भाषा में कहें तो, Conditional Statements program को यह क्षमता देते हैं कि वह हर बार एक ही sequence में statements execute करने के बजाय, condition के अनुसार अलग-अलग रास्ते (path) चुन सके।
Python में Conditional Statements मुख्य रूप से
ifkeyword पर आधारित होते हैं, और साथ मेंelifतथाelsekeywords का इस्तेमाल करके अलग-अलग possibilities को handle किया जाता है।यह statements हमेशा किसी relational या logical expression के result (
True/False) पर depend करते हैं, इसलिए इन्हें समझने से पहले Relational aur Logical Operators को समझना फायदेमंद रहता है।Python में indentation (space देकर code को align करना) बेहद ज़रूरी होता है, क्योंकि यही यह तय करता है कि कौन सा code किस condition के अंदर आता है — Python curly braces
{}के बजाय indentation पर ही निर्भर करता है।Real life में इसे ऐसे समझ सकते हैं — "अगर बारिश हो रही है तो छाता ले जाओ, नहीं तो मत ले जाओ" — यही सोचने का तरीका program में Conditional Statements के ज़रिए implement किया जाता है।
Exam की दृष्टि से इसकी सरल परिभाषा याद रखनी हो तो: "Conditional statements allow a program to execute certain blocks of code only when a specified condition evaluates to True, enabling decision-making within the program."
Importance of Decision Making in Hindi
अगर किसी program में Decision Making न हो, तो वह हमेशा top से bottom तक एक ही fixed sequence में चलेगा, चाहे input कुछ भी हो — जिससे program real-world problems को solve करने में असमर्थ हो जाएगा।
Decision Making की वजह से ही program अलग-अलग users, अलग-अलग inputs और अलग-अलग situations के हिसाब से अपना behavior बदल पाता है, जो किसी भी useful software के लिए बेहद ज़रूरी है।
जैसे किसी login system में यह decide करना कि username-password सही है या नहीं, या किसी e-commerce app में यह check करना कि item stock में है या नहीं — यह सब Decision Making की वजह से ही possible होता है।
Decision Making, Loops (जैसे
for,while) के साथ मिलकर programming की असली ताकत बनाती है, क्योंकि लगभग हर complex logic इन्हीं दो concepts के combination से बनता है।बिना सही Decision Making के, program में गलत output आने की संभावना बढ़ जाती है, इसलिए conditions को सही तरीके से design करना एक अच्छे programmer की सबसे ज़रूरी skill मानी जाती है।
Exam में अक्सर यह भी पूछा जाता है कि Decision Making किन statements से implement होती है — इसका सीधा जवाब है Conditional Statements (
if,elif,else) के ज़रिए।
Types of Conditional Statements in Hindi
-
1. Simple if Statement:
इसमें सिर्फ एक condition check की जाती है, और अगर वहTrueहो, तभी उसके अंदर का code execute होता है। अगर conditionFalseहो, तो वह block skip कर दिया जाता है।age = 20
if age >= 18:
print("Aap vote de sakte hain") -
2. if-else Statement:
इसमें दो possibilities को handle किया जाता है — अगर conditionTrueहो तोifवाला block चलता है, और अगरFalseहो तोelseवाला block चलता है।age = 15
if age >= 18:
print("Aap vote de sakte hain")
else:
print("Aap abhi vote nahi de sakte") -
3. if-elif-else Statement:
जब दो से ज़्यादा possibilities को check करना हो, तोelif(else-if) का इस्तेमाल किया जाता है। Python एक-एक करके सभी conditions check करता है और जो पहली conditionTrueमिलती है, उसी का block execute होता है।marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C") -
4. Nested if Statement:
जब एकifstatement के अंदर एक औरifstatement लिखा जाता है, तो उसे Nested if कहते हैं। इसका उपयोग तब होता है जब पहली condition True होने के बाद भी एक और condition check करनी हो।age = 20
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
else:
print("ID chahiye") -
5. Shorthand if-else (Conditional Expression):
Python में एक बहुत ही छोटी condition को एक ही line में लिखने के लिए shorthand तरीका भी उपलब्ध है, जिसे Ternary Operator जैसा भी माना जा सकता है।age = 20
status = "Adult" if age >= 18 else "Minor"
# status ki value "Adult" ho jaayegi
Characteristics of Conditional Statements in Hindi
1. Boolean-based Execution:
हर Conditional Statement का flow पूरी तरह इस बात पर depend करता है कि दी गई conditionTrueहै याFalse— इसके अलावा कोई तीसरा outcome नहीं होता।2. Indentation-Dependent:
Python में किसी condition के अंदर कौन सा code आएगा, यह पूरी तरह indentation (spaces) से तय होता है, इसलिए indentation में हल्की सी गलती भी error या गलत output दे सकती है।3. Mutually Exclusive Blocks:
if-elif-elsestructure में एक बार में सिर्फ एक ही block execute होता है — जैसे ही कोई condition True मिलती है, बाकी सारी conditions skip हो जाती हैं।4. Nesting Support:
Conditional Statements को एक-दूसरे के अंदर nest किया जा सकता है, जिससे complex aur multi-level decisions को handle करना संभव होता है।5. Combination with Logical Operators:
Conditional Statements अक्सरand,or,notजैसे Logical Operators के साथ मिलकर इस्तेमाल किए जाते हैं, ताकि एक साथ कई conditions को check किया जा सके।6. Optional else Block:
elseblock लिखना ज़रूरी नहीं होता — अगर सिर्फ एक specific condition पर ही कुछ करना हो, तो सिर्फifstatement से भी काम चल सकता है।
Frequently Asked Questions (FAQ) – Conditional Statements in Python
वे statements जो किसी condition के True या False होने के आधार पर program के अलग-अलग हिस्सों को execute करने का निर्णय लेते हैं, उन्हें Conditional Statements कहते हैं — जैसे if, elif, else।
मुख्य रूप से Simple if, if-else, if-elif-else, Nested if aur Shorthand if-else — ये Python में Conditional Statements के मुख्य types हैं।
if-else सिर्फ दो possibilities (True/False) को handle करता है, जबकि if-elif-else दो से ज़्यादा multiple conditions को एक-एक करके check करने की सुविधा देता है।
Python curly braces {} इस्तेमाल नहीं करता, इसलिए यह पता लगाने के लिए कि कौन सा code किसी condition के अंदर आता है, सिर्फ indentation (spaces) का ही सहारा लिया जाता है।
जब एक condition True होने के बाद भी उसके अंदर एक और अलग condition check करनी हो, तब Nested if का इस्तेमाल किया जाता है।