if Statement in Python in hindi - if Statement in Python क्या है?

Arpit Nageshwar
⏰ 3 min read

if Statement in Python क्या है? Syntax, Flowchart

Table of Contents

Introduction to if Statement in Hindi

  • Python में if Statement सबसे basic aur सबसे ज़्यादा इस्तेमाल होने वाला Conditional Statement है, जिसकी मदद से program यह decide करता है कि कोई specific block of code चलाना है या नहीं।

  • आसान भाषा में कहें तो, if Statement एक condition check करता है, और अगर वह condition True निकलती है, तभी उसके नीचे इंडेंट किया हुआ code execute होता है।

  • अगर condition False हो, तो if block के अंदर का code बिल्कुल भी execute नहीं होता, और program सीधे उसके बाद वाली line पर चला जाता है।

  • if Statement हमेशा किसी ऐसी expression पर काम करता है जिसका result Boolean (True/False) में निकलता हो — यह expression अक्सर Relational या Logical Operators से बनी होती है।

  • यह Conditional Statements की सबसे सरल form है, और इसी के ऊपर if-else, if-elif-else जैसे बाकी सभी advanced structures बने होते हैं।

  • Exam की दृष्टि से इसकी सरल परिभाषा याद रखनी हो तो: "The if statement executes a block of code only when the given condition evaluates to True; if the condition is False, the block is simply skipped."

Syntax of if Statement in Hindi

  • Basic Syntax:

    if condition:
        statement(s)

    यहाँ condition कोई भी ऐसी expression है जो True या False में evaluate होती है, और statement(s) वह code है जो सिर्फ तभी चलेगा जब condition True हो।

  • Colon (:) का महत्व:
    if keyword के बाद condition लिखने के तुरंत बाद एक colon (:) लगाना अनिवार्य होता है — यह Python को बताता है कि यहाँ से नया code block शुरू हो रहा है।

  • Indentation का महत्व:
    if statement के अंदर आने वाला हर statement एक जैसी indentation (आमतौर पर 4 spaces) के साथ लिखा जाना चाहिए, वरना Python IndentationError दे देता है।

  • Simple Example:

    marks = 85
    if marks >= 40:
        print("Aap pass ho gaye")

    यहाँ चूंकि marks >= 40 condition True है, इसलिए print statement execute होकर "Aap pass ho gaye" print करेगा।

Flowchart of if Statement in Hindi

if Statement Flowchart Start Condition True hai? Yes if block execute karo No if block skip karo End
  • Flowchart में सबसे पहले Start से शुरुआत होती है, फिर program दी गई condition को check करता है।

  • अगर condition True निकलती है, तो program if block के अंदर का code execute करता है, और उसके बाद आगे बढ़ जाता है।

  • अगर condition False निकलती है, तो if block को पूरी तरह skip कर दिया जाता है, और program सीधे आगे के code पर पहुंच जाता है।

  • दोनों ही स्थितियों में, चाहे condition True हो या False, program अंततः End पर पहुंचकर आगे का normal execution जारी रखता है।

Example Programs in Hindi

  • Example 1: Even Number Check करना

    num = 8
    if num % 2 == 0:
        print("Yeh number even hai")

    यहाँ num % 2 == 0 True है (8 को 2 से divide करने पर remainder 0 आता है), इसलिए "Yeh number even hai" print होगा।

  • Example 2: Eligibility Check करना

    age = 20
    if age >= 18:
        print("Aap vote de sakte hain")

    चूंकि age >= 18 True है, इसलिए voting eligibility वाला message print होगा।

  • Example 3: Multiple Conditions को Logical Operator से जोड़ना

    marks = 45
    attendance = 80
    if marks >= 40 and attendance >= 75:
        print("Aap exam mein baith sakte hain")

    यहाँ दोनों conditions (marks aur attendance) True हैं, इसलिए message print होगा।

  • Example 4: String Comparison के साथ if Statement

    password = "python123"
    if password == "python123":
        print("Login Successful")

    यहाँ == operator से दो strings compare की गई हैं, और मैच होने पर "Login Successful" print होगा।

Common Errors in if Statement in Python in Hindi

  • 1. Colon (:) भूल जाना:
    अगर if condition के बाद colon (:) नहीं लगाया जाए, तो Python SyntaxError दे देता है, क्योंकि colon ही यह बताता है कि नया block शुरू हो रहा है।

    # Galat: if age >= 18
    # Sahi: if age >= 18:

  • 2. Indentation में गलती करना:
    अगर if block के अंदर सभी statements एक जैसी indentation में न हों, तो Python IndentationError throw कर देता है।

  • 3. == की जगह = इस्तेमाल करना:
    Comparison के लिए == (Relational Operator) की जगह गलती से = (Assignment Operator) लिख देना एक बहुत common mistake है, जिससे SyntaxError आता है।

    # Galat: if age = 18:
    # Sahi: if age == 18:

  • 4. Non-Boolean Expression को Condition के रूप में गलत समझना:
    शुरुआती programmers अक्सर यह भूल जाते हैं कि condition हमेशा किसी Boolean-evaluating expression से बनी होनी चाहिए, सिर्फ कोई random statement नहीं होनी चाहिए।

  • 5. Curly Braces {} इस्तेमाल करने की कोशिश करना:
    C या Java से आने वाले programmers अक्सर code block define करने के लिए {} इस्तेमाल करने की कोशिश करते हैं, जबकि Python में सिर्फ indentation से ही block define होता है।

  • 6. Empty if Block छोड़ देना:
    अगर किसी वजह से if block के अंदर temporarily कोई code नहीं लिखना हो, तो उसे खाली नहीं छोड़ा जा सकता — इसके लिए pass keyword लिखना अनिवार्य होता है, वरना IndentationError आ जाएगा।

    if age >= 18:
        pass # abhi kuch nahi karna

Frequently Asked Questions (FAQ) – if Statement in Python

वह Conditional Statement जो किसी condition के True होने पर ही एक specific code block execute करता है, उसे if Statement कहते हैं।

Colon (:) Python को यह बताता है कि यहाँ से एक नया indented code block शुरू हो रहा है, इसके बिना Python SyntaxError देता है।

सबसे common mistake है comparison के लिए == की जगह गलती से = इस्तेमाल कर लेना, जिससे program में error आ जाती है।

नहीं, Python में if block को पूरी तरह खाली नहीं छोड़ा जा सकता — अगर temporarily कुछ नहीं करना हो, तो pass keyword लिखना अनिवार्य होता है।

Python curly braces {} इस्तेमाल नहीं करता, इसलिए यह पता लगाने के लिए कि कौन सा code किस condition के अंदर आता है, सिर्फ indentation (spaces) का ही सहारा लिया जाता है।

Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified