If Statement in Python in Hindi – Syntax, Flowchart, Examples & Programs

If Statement in Python in Hindi के इस अध्याय में हम Syntax, Flowchart, उदाहरण (Examples) और Programs की सहायता से if statement का महत्व, उपयोग और प्रयोग विस्तार से सीखेंगे। साथ ही समझेंगे कि Python में Decision Making के लिए if statement का उपयोग कैसे किया जाता है।

If Statement (Decision Making Statement)

हम अपनी रोज़मर्रा की ज़िंदगी में हर वक्त कोई न कोई फैसला (decision) लेते रहते हैं। जैसे:

  • अगर बाहर बारिश हो रही है, तो हम छाता लेकर निकलते हैं।
  • अगर ट्रैफ़िक सिग्नल ग्रीन है, तो हम आगे बढ़ जाते हैं।

ठीक इसी तरह, कोडिंग या प्रोग्रामिंग में भी ऐसे कई मौके आते हैं जहाँ कंप्यूटर को फैसले लेने होते हैं। कुछ खास कंडीशंस (conditions) के बेसिस पर कंप्यूटर यह तय करता है कि आगे कौन सा स्टेप या इंस्ट्रक्शन (instruction) रन (execute) करना है।

इन्हीं फैसलों को लेने के लिए हम Decision-making statements का इस्तेमाल करते हैं। ये स्टेटमेंट्स अलग-अलग सिचुएशन के हिसाब से प्रोग्राम को सही रास्ता चुनने में मदद करते हैं।

Python में हमें ये मुख्य Decision-making statements मिलते हैं:

  • if statement
  • if…else statement
  • if…elif…else statement
  • Nested if statement

if Statement

if स्टेटमेंट किसी खास कंडीशन (condition) को चेक करता है.

  • अगर वो कंडीशन True (सही) होती है, तो if ब्लॉक के अंदर लिखा गया कोड रन (execute) हो जाता है.
  • लेकिन अगर कंडीशन False (गलत) निकलती है, तो कंप्यूटर उस कोड को छोड़ देता है (skip कर देता है) और आगे बढ़ जाता है.

Syntax (लिखने का तरीका)

if condition:
    statement(s)

ज़रूरी बात: if condition के बाद कोलन (:) लगाना बहुत ज़रूरी है। इसके बाद जो भी स्टेटमेंट्स रन करने हैं, उन्हें थोड़ा स्पेस (indentation) देकर लिखा जाता है।

Flow chart of if statement (इफ़ स्टेटमेंट का फ़्लोचार्ट)

Example (उदाहरण)

num = int(input(“Enter a number: “))
if num > 0:
    print(“Positive Number”)
print(“Program Ended”)

Output (नतीजा)

अगर यूजर ने नंबर 5 इनपुट किया, तो स्क्रीन पर ऐसा दिखेगा:

Enter a number: 5
Positive Number
Program Ended

Indentation in Python

  • Python कोड ब्लॉक्स (blocks of code) और nested block structures को डिफाइन करने के लिए indentation का इस्तेमाल करता है।
  • Indentation का मतलब किसी स्टेटमेंट (statement) की शुरुआत में जोड़े गए स्पेस (spaces) या टैब (tabs) से है।
  • Python पूरी सख्ती से indentation को चेक करता है, और गलत indentation होने पर सिंटैक्स एरर (syntax error) आता है।

if…else Statement

कभी-कभी हम चाहते हैं कि कंडीशन True होने पर एक एक्शन हो और False होने पर दूसरा एक्शन। ऐसे मामलों में, हम if…else statement का इस्तेमाल करते हैं।

Syntax

if condition:
    statement(s)
else:
    statement(s)

Flow chart of if…else

Example

age = int(input(“Enter your age”))
if age >= 18:
    print(“You can vote”)
else:
    print(“You cannot vote”)

Output

अगर यूजर ने 16 इनपुट किया, तो स्क्रीन पर ऐसा दिखेगा:

Enter your age:
16
You cannot vote

if…elif…else Statement

if…elif statement का इस्तेमाल तब किया जाता है जब कई कंडीशंस (multiple conditions) को चेक करने की ज़रूरत होती है और प्रोग्राम को कई विकल्पों में से किसी एक को चुनना होता है।

यह कैसे कार्य करता है?

  • सबसे पहले if condition को चेक किया जाता है।
  • यदि यह True होती है, तो if block execute होता है।
  • यदि यह False होती है, तो Python अगली elif condition को चेक करता है।
  • यह प्रक्रिया तब तक चलती रहती है जब तक कोई condition True नहीं हो जाती।
  • यदि कोई भी condition True नहीं होती, तो else block execute होता है।
  • Python केवल पहले True मिलने वाले block को execute करता है और उसके बाद की सभी conditions को अनदेखा (ignore) कर देता है।

Syntax

if condition1:
    statement(s)
elif condition2:
    statement(s)
elif condition3:
    statement(s)
else:
    statement(s)

Flowchart of if…elif…else Statement

Example

marks = int(input(“Enter your marks”))
if marks >= 90:
    print(“Grade A+”)
elif marks >= 75:
    print(“Grade A”)
else:
    print(“Grade B”)

Output:

Enter your marks: 80
Grade A

Explanation

इस प्रोग्राम में सबसे पहले यूज़र से marks इनपुट लिए जाते हैं। इसके बाद if…elif…else statement अंकों के आधार पर Grade निर्धारित करता है।

  • यदि marks 90 या उससे अधिक हैं, तो Grade A+ प्रदर्शित होता है।
  • यदि पहली condition False होती है, तो Python elif condition (marks >= 75) को चेक करता है।
  • यदि यह True होती है, तो Grade A प्रदर्शित होता है।
  • यदि दोनों conditions False होती हैं, तो else block execute होता है और Grade B प्रदर्शित होता है।

दिए गए Output में पहली condition (marks >= 90) False है, लेकिन दूसरी condition (marks >= 75) True है। इसलिए elif block execute होता है और “Grade A” प्रदर्शित होता है।

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *