मुख्य कंटेंट पर जाएं
Python if-else Statement in Hindi – Decision Making की पूरी जानकारी
PYTHON PROGRAMMING 15 मिनट पढ़ें 1 बार पढ़ा गया

Python if-else Statement in Hindi – Decision Making की पूरी जानकारी

Lavkush Chakrawarti

परिचय

Decision Making क्या है?

Condition क्या होती है?

Flow Chart (Decision Making)

1. if Statement

Syntax

उदाहरण 1

उदाहरण 2

if Statement का Flow

2. if...else Statement

Syntax

उदाहरण 1

उदाहरण 2 – Even/Odd Program

3. if...elif...else Statement

Syntax

उदाहरण – Grade निकालना

if...elif...else का Flow

4. Nested if Statement

Syntax

उदाहरण

उदाहरण – Login System

5. Match-Case Statement (Python 3.10+)

Syntax

उदाहरण

if और Match-Case में अंतर

Conditional (Ternary) Operator

Syntax

उदाहरण

Practical Programs

Program 1 – Positive, Negative या Zero

Program 2 – Largest Number

Program 3 – Leap Year

Program 4 – Voting Eligibility

Program 5 – Calculator

Common Errors

1. Missing Colon

2. Wrong Indentation

3. Assignment की जगह Comparison

Best Practices

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

Frequently Asked Questions (FAQs)

प्रश्न 1. Python में Decision Making क्या है?

प्रश्न 2. if और if...else में क्या अंतर है?

प्रश्न 3. elif का उपयोग कब किया जाता है?

प्रश्न 4. Nested if क्या है?

प्रश्न 5. Match-Case किस Version में आया?

Quick Revision

अगला अध्याय (Chapter 11)

 

परिचय

Programming में कई बार ऐसी परिस्थितियाँ आती हैं जहाँ Program को किसी शर्त (Condition) के आधार पर निर्णय (Decision) लेना पड़ता है। उदाहरण के लिए—

  • यदि विद्यार्थी के अंक 33 या उससे अधिक हैं, तो उसे Pass घोषित करना।
  • यदि बैंक खाते में पर्याप्त राशि है, तभी पैसा निकालना।
  • यदि User का Username और Password सही है, तभी Login की अनुमति देना।
  • यदि किसी व्यक्ति की आयु 18 वर्ष या उससे अधिक है, तो उसे मतदान (Voting) की अनुमति देना।

ऐसी परिस्थितियों में Program को अलग-अलग परिस्थितियों के अनुसार अलग-अलग कार्य करना होता है। Python में इस प्रक्रिया को Decision Making या Conditional Statements कहा जाता है।

Python में Decision Making के लिए मुख्य रूप से निम्न Statements का उपयोग किया जाता है—

  • if
  • if...else
  • if...elif...else
  • Nested if
  • Match-Case (Python 3.10+)

 

Decision Making क्या है?

Decision Making वह प्रक्रिया है जिसमें Program किसी Condition को जाँचकर यह तय करता है कि कौन-सा Code Execute करना है।

यदि Condition True होती है, तो एक Block Execute होता है और यदि False होती है, तो दूसरा Block Execute हो सकता है।

 

Condition क्या होती है?

Condition एक ऐसा Expression होता है जिसका परिणाम केवल दो में से एक होता है—

  • True
  • False

उदाहरण—

age = 20

 

print(age >= 18)

Output

True

 

Flow Chart (Decision Making)

           Condition

               │

        ┌──────┴──────┐

      True          False

        │              │

 Execute Block     Skip/Else Block

 

  1. if Statement

यदि कोई Condition True होती है, तो if Block के अंदर का Code Execute होता है।

यदि Condition False है, तो Program सीधे अगले Statement पर चला जाता है।

 

Syntax

if condition:

    statement

 

उदाहरण 1

age = 20

 

if age >= 18:

    print("You are eligible for voting.")

Output

You are eligible for voting.

 

उदाहरण 2

marks = 75

 

if marks >= 33:

    print("Pass")

Output

Pass

 

if Statement का Flow

यदि Condition True होगी—

Condition → True → Execute Block

यदि Condition False होगी—

Condition → False → Block Skip

 

  1. if...else Statement

जब Program को दो अलग-अलग परिस्थितियों में अलग-अलग कार्य करना हो, तब if...else का उपयोग किया जाता है।

 

Syntax

if condition:

    statement1

else:

    statement2

 

उदाहरण 1

age = 16

 

if age >= 18:

    print("Eligible for Voting")

else:

    print("Not Eligible")

Output

Not Eligible

 

उदाहरण 2 – Even/Odd Program

num = int(input("Enter Number: "))

 

if num % 2 == 0:

    print("Even Number")

else:

    print("Odd Number")

 

  1. if...elif...else Statement

जब दो से अधिक Conditions की जाँच करनी हो, तब if...elif...else का उपयोग किया जाता है।

 

Syntax

if condition1:

    statement

elif condition2:

    statement

elif condition3:

    statement

else:

    statement

 

उदाहरण – Grade निकालना

marks = int(input("Enter Marks: "))

 

if marks >= 90:

    print("Grade A+")

elif marks >= 75:

    print("Grade A")

elif marks >= 60:

    print("Grade B")

elif marks >= 33:

    print("Grade C")

else:

    print("Fail")

 

if...elif...else का Flow

Condition 1

   │

True → Execute

   │

False

   │

Condition 2

   │

True → Execute

   │

False

   │

Condition 3

   │

Else

 

  1. Nested if Statement

जब एक if के अंदर दूसरा if लिखा जाता है, तो उसे Nested if कहते हैं।

 

Syntax

if condition1:

    if condition2:

        statement

 

उदाहरण

age = 20

citizen = True

 

if age >= 18:

    if citizen:

        print("Eligible for Voting")

Output

Eligible for Voting

 

उदाहरण – Login System

username = input("Username: ")

password = input("Password: ")

 

if username == "admin":

    if password == "12345":

        print("Login Successful")

    else:

        print("Wrong Password")

else:

    print("Invalid Username")

 

  1. Match-Case Statement (Python 3.10+)

Python 3.10 में Match-Case Statement जोड़ा गया, जो कई विकल्पों (Multiple Choices) में से किसी एक को चुनने के लिए उपयोग किया जाता है।

यह कुछ हद तक अन्य भाषाओं के Switch Case जैसा कार्य करता है।

 

Syntax

match variable:

    case value1:

        statement

    case value2:

        statement

    case _:

        default_statement

 

उदाहरण

day = int(input("Enter Day Number: "))

 

match day:

    case 1:

        print("Monday")

    case 2:

        print("Tuesday")

    case 3:

        print("Wednesday")

    case _:

        print("Invalid Day")

 

if और Match-Case में अंतर

if-elif

Match-Case

Conditions के लिए

निश्चित Values के लिए

Complex Logic

Multiple Fixed Options

सभी Versions

Python 3.10+

 

Conditional (Ternary) Operator

यदि केवल एक Condition हो और छोटा Code लिखना हो, तो Ternary Operator का उपयोग किया जा सकता है।

Syntax

value_if_true if condition else value_if_false

 

उदाहरण

age = 20

 

result = "Adult" if age >= 18 else "Minor"

 

print(result)

Output

Adult

 

Practical Programs

Program 1 – Positive, Negative या Zero

num = int(input("Enter Number: "))

 

if num > 0:

    print("Positive")

elif num < 0:

    print("Negative")

else:

    print("Zero")

 

Program 2 – Largest Number

a = int(input("First Number: "))

b = int(input("Second Number: "))

 

if a > b:

    print("Largest =", a)

else:

    print("Largest =", b)

 

Program 3 – Leap Year

year = int(input("Enter Year: "))

 

if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):

    print("Leap Year")

else:

    print("Not a Leap Year")

 

Program 4 – Voting Eligibility

age = int(input("Enter Age: "))

 

if age >= 18:

    print("Eligible")

else:

    print("Not Eligible")

 

Program 5 – Calculator

a = int(input("First Number: "))

b = int(input("Second Number: "))

 

op = input("Enter Operator (+,-,*,/): ")

 

if op == "+":

    print(a + b)

elif op == "-":

    print(a - b)

elif op == "*":

    print(a * b)

elif op == "/":

    if b != 0:

        print(a / b)

    else:

        print("Division by zero is not allowed.")

else:

    print("Invalid Operator")

 

Common Errors

  1. Missing Colon

if age >= 18

❌ Error

✔ सही

if age >= 18:

 

  1. Wrong Indentation

if age >= 18:

print("Adult")

❌ IndentationError

 

  1. Assignment की जगह Comparison

❌ गलत

if age = 18:

✔ सही

if age == 18:

 

Best Practices

  • हमेशा Conditions को स्पष्ट रखें।
  • Complex Conditions में Parentheses () का उपयोग करें।
  • अनावश्यक Nested if से बचें।
  • Fixed Values के लिए Match-Case उपयोग करें।
  • Meaningful Variable Names रखें।

 

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

  • if केवल True होने पर Execute होता है।
  • if...else दो परिस्थितियों के लिए उपयोग होता है।
  • if...elif...else अनेक Conditions के लिए उपयोग होता है।
  • Nested if में एक if के अंदर दूसरा if होता है।
  • match-case Python 3.10 और बाद के Versions में उपलब्ध है।
  • Comparison Operators (==, >, <, !=) का उपयोग Conditions में किया जाता है।

 

Frequently Asked Questions (FAQs)

प्रश्न 1. Python में Decision Making क्या है?

Conditions के आधार पर Program का अलग-अलग Code Execute करना।

प्रश्न 2. if और if...else में क्या अंतर है?

if केवल True होने पर चलता है, जबकि if...else में True और False दोनों स्थितियों के लिए अलग Block होता है।

प्रश्न 3. elif का उपयोग कब किया जाता है?

जब दो से अधिक Conditions की जाँच करनी हो।

प्रश्न 4. Nested if क्या है?

एक if Statement के अंदर दूसरा if Statement लिखना।

प्रश्न 5. Match-Case किस Version में आया?

Python 3.10 में।

 

Quick Revision

  • if = एक Condition
  • if...else = दो विकल्प
  • if...elif...else = अनेक Conditions
  • Nested if = एक if के अंदर दूसरा if
  • match-case = Multiple Fixed Values
  • Ternary Operator = एक लाइन में Condition

 

अगला अध्याय (Chapter 11)

Python Loops (for Loop, while Loop, Nested Loop, break, continue, pass और range() Function) — इसमें Loop की आवश्यकता, for और while Loop, Nested Loops, Infinite Loop, break, continue, pass, range() Function तथा 30+ Practical Programs को विस्तार से सीखेंगे।

 

निष्कर्ष (Conclusion)

Python Decision Making की पूरी जानकारी हिंदी में। if, if-else, elif, Nested if, Match-Case Statement, Conditional Operators, Flow Control और Practical Programs उदाहरण सहित सीखें।