Online Computer Courses Classes and Training Program

Python में Decision-Making Statements


Python में Decision-Making Statements

Python में Decision-Making Statements का उपयोग प्रोग्राम को conditions के आधार पर चलाने के लिए किया जाता है। इसका मतलब है कि कोड का execution condition के true या false होने पर depend करता है।


Python में Decision-Making Statements के प्रकार

  1. if Statement
  2. if-else Statement
  3. if-elif-else Statement
  4. Nested if Statement

1. if Statement

if Statement तब use होता है जब आपको कोई condition check करनी हो और वो condition true हो तो ही code execute करना हो।

Syntax:

if condition:
    # Code to execute if the condition is True

Example:

age = 18
if age >= 18:
    print("You are eligible to vote.")

Explanation:

  1. age की value 18 है।
  2. if condition check करता है कि age >= 18 है या नहीं।
  3. अगर condition true होती है, तो print() statement execute होता है।

Output:

You are eligible to vote.

2. if-else Statement

if-else Statement तब use होता है जब आपको condition के true या false होने पर अलग-अलग code execute करना हो।

Syntax:

if condition:
    # Code to execute if condition is True
else:
    # Code to execute if condition is False

Example:

number = 5
if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Explanation:

  1. Condition check करती है कि number % 2 == 0 (even number) है या नहीं।
  2. अगर condition true होती है, तो "even" print होता है।
  3. अगर condition false होती है, तो "odd" print होता है।

Output:

The number is odd.

3. if-elif-else Statement

if-elif-else Statement तब use होता है जब multiple conditions check करनी हों।

Syntax:

if condition1:
    # Code to execute if condition1 is True
elif condition2:
    # Code to execute if condition2 is True
else:
    # Code to execute if none of the conditions are True

Example:

marks = 85
if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
else:
    print("Grade: C")

Explanation:

  1. सबसे पहले condition check होती है: marks >= 90
  2. अगर यह false होती है, तो अगली condition marks >= 75 check होती है।
  3. अगर दोनों false होती हैं, तो else block execute होता है।

Output:

Grade: B

4. Nested if Statement

जब एक if Statement के अंदर दूसरा if Statement होता है, तो इसे Nested if Statement कहते हैं।

Syntax:

if condition1:
    if condition2:
        # Code to execute if both conditions are True

Example:

number = 10
if number > 0:
    if number % 2 == 0:
        print("The number is positive and even.")
    else:
        print("The number is positive and odd.")

Explanation:

  1. Outer if condition check करती है कि number > 0 है या नहीं।
  2. Inner if condition check करती है कि number % 2 == 0 है या नहीं।
  3. Inner else तभी चलेगा जब number odd हो।

Output:

The number is positive and even.

Logical Operators in Conditions

Logical operators का use conditions को combine करने के लिए किया जाता है:

  1. and: दोनों conditions true होनी चाहिए।
  2. or: कम से कम एक condition true होनी चाहिए।
  3. not: condition को उल्टा कर देता है (True को False और False को True)।

Example:

age = 25
if age > 18 and age < 30:
    print("You are a young adult.")

Output:

You are a young adult.

Conclusion

Python के Decision-Making Statements से आप अपने प्रोग्राम को flexible और interactive बना सकते हैं। ये statements conditions के आधार पर अलग-अलग tasks perform करने में मदद करते हैं।

अगर आप Python programming में expert बनना चाहते हैं, तो इन statements को practice करना शुरू करें।


Examples of Decision-Making Programs in Python

Example 1: Check if a Number is Positive or Negative

number = int(input("Enter a number: "))

if number > 0:
    print("The number is positive.")
elif number == 0:
    print("The number is zero.")
else:
    print("The number is negative.")

Explanation:

  • User से एक number input लिया जाता है।
  • if condition check करती है कि number positive है या नहीं।
  • अगर number zero हो, तो elif execute होता है।
  • Negative होने पर else block execute होता है।

Output Example:

Enter a number: -5  
The number is negative.

Example 2: Check Eligibility to Vote

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

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

Explanation:

  • User से age input ली जाती है।
  • if condition check करती है कि age 18 या उससे अधिक है।
  • True होने पर "eligible to vote" print होता है, वरना "not eligible"।

Output Example:

Enter your age: 16  
You are not eligible to vote.

Example 3: Grading System

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

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 50:
    print("Grade: C")
else:
    print("Grade: F")

Explanation:

  • Marks input के आधार पर grade assign किया जाता है।
  • सबसे पहले if condition 90 और उससे ऊपर check करती है।
  • अलग-अलग ranges के लिए elif blocks चलते हैं।
  • अगर सभी conditions fail होती हैं, तो else block "Grade: F" print करता है।

Output Example:

Enter your marks: 78  
Grade: B

Example 4: Check Even or Odd Number

number = int(input("Enter a number: "))

if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Explanation:

  • Number को 2 से divide करके remainder (%) check किया जाता है।
  • अगर remainder 0 है, तो number even है; वरना odd है।

Output Example:

Enter a number: 11  
The number is odd.

Example 5: Check if a Number is Divisible by Both 3 and 5

number = int(input("Enter a number: "))

if number % 3 == 0 and number % 5 == 0:
    print("The number is divisible by both 3 and 5.")
elif number % 3 == 0:
    print("The number is divisible by 3 only.")
elif number % 5 == 0:
    print("The number is divisible by 5 only.")
else:
    print("The number is not divisible by 3 or 5.")

Explanation:

  • if condition check करती है कि number 3 और 5 दोनों से divisible है।
  • elif conditions सिर्फ 3 या 5 के लिए check करती हैं।
  • अगर कोई condition true नहीं होती, तो else block execute होता है।

Output Example:

Enter a number: 15  
The number is divisible by both 3 and 5.

Example 6: Nested if Example – Check Positive, Even or Odd

number = int(input("Enter a number: "))

if number > 0:
    if number % 2 == 0:
        print("The number is positive and even.")
    else:
        print("The number is positive and odd.")
else:
    print("The number is not positive.")

Explanation:

  • Outer if check करता है कि number positive है या नहीं।
  • Inner if check करता है कि number even है या odd।

Output Example:

Enter a number: 4  
The number is positive and even.

Practice These Programs

Python में decision-making programs को समझने और लिखने के लिए इन examples को practice करें। इससे आपको real-world problems solve करने की skill develop होगी।


Multiple Choice Questions (MCQs) on Decision-Making in Python

Question 1:

Which of the following is the correct syntax for an if statement in Python?
A) if (condition) then:
B) if condition:
C) if condition then:
D) if (condition):

Answer:
B) if condition:


Question 2:

What will the following code output?

x = 10  
if x > 5:  
    print("A")  
else:  
    print("B")  

A) A
B) B
C) A B
D) Error

Answer:
A) A


Question 3:

What is the output of the following code?

x = 5  
if x % 2 == 0:  
    print("Even")  
else:  
    print("Odd")  

A) Even
B) Odd
C) Error
D) None of the above

Answer:
B) Odd


Question 4:

Which of the following operators is used to combine multiple conditions?
A) +
B) or
C) and
D) Both B and C

Answer:
D) Both B and C


Question 5:

What will the following code output?

x = 10  
if x > 5 and x < 15:  
    print("Valid")  
else:  
    print("Invalid")  

A) Valid
B) Invalid
C) Error
D) None

Answer:
A) Valid


Question 6:

What will the following code output if marks = 80?

if marks >= 90:  
    print("Grade A")  
elif marks >= 75:  
    print("Grade B")  
elif marks >= 50:  
    print("Grade C")  
else:  
    print("Grade F")  

A) Grade A
B) Grade B
C) Grade C
D) Grade F

Answer:
B) Grade B


Question 7:

In a nested if statement, which block will execute if the outer condition is False?
A) Inner if block
B) Outer else block
C) Inner else block
D) None

Answer:
B) Outer else block


Question 8:

What will the following code output?

x = 7  
if x % 2 == 0:  
    if x > 5:  
        print("Even and greater than 5")  
    else:  
        print("Even and less than or equal to 5")  
else:  
    print("Odd")  

A) Even and greater than 5
B) Even and less than or equal to 5
C) Odd
D) None

Answer:
C) Odd


Question 9:

Which statement is true about Python's if-elif-else?
A) You can have multiple else statements.
B) The elif block is optional.
C) The else block is mandatory.
D) You cannot use elif without else.

Answer:
B) The elif block is optional.


Question 10:

What is the purpose of the else block in an if-else structure?
A) To execute when the if condition is True.
B) To execute when the if condition is False.
C) To terminate the program.
D) To execute regardless of the if condition.

Answer:
B) To execute when the if condition is False.


Question 11:

What will be the output of the following code?

x = 15  
if x % 3 == 0:  
    print("Divisible by 3")  
elif x % 5 == 0:  
    print("Divisible by 5")  
else:  
    print("Not divisible by 3 or 5")  

A) Divisible by 3
B) Divisible by 5
C) Divisible by both 3 and 5
D) Not divisible by 3 or 5

Answer:
A) Divisible by 3


These MCQs cover the basics of decision-making in Python and are great for practice or exams.


Decision-Making in Python: Question and Answer Examples

Question 1:

Write a program to check if a number is positive, negative, or zero.

Answer:

number = int(input("Enter a number: "))

if number > 0:
    print("The number is positive.")
elif number == 0:
    print("The number is zero.")
else:
    print("The number is negative.")

Question 2:

Write a program to determine if a given number is even or odd.

Answer:

number = int(input("Enter a number: "))

if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

Question 3:

Write a program to check if a person is eligible to vote based on their age.

Answer:

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

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

Question 4:

Write a program to assign a grade to a student based on their marks:

  • Marks >= 90: Grade A
  • Marks >= 75: Grade B
  • Marks >= 50: Grade C
  • Below 50: Grade F

Answer:

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

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 50:
    print("Grade: C")
else:
    print("Grade: F")

Question 5:

Write a program to check if a number is divisible by both 3 and 5.

Answer:

number = int(input("Enter a number: "))

if number % 3 == 0 and number % 5 == 0:
    print("The number is divisible by both 3 and 5.")
else:
    print("The number is not divisible by both 3 and 5.")

Question 6:

Write a program to check if a number is divisible by 3, 5, or both using if-elif-else.

Answer:

number = int(input("Enter a number: "))

if number % 3 == 0 and number % 5 == 0:
    print("The number is divisible by both 3 and 5.")
elif number % 3 == 0:
    print("The number is divisible by 3.")
elif number % 5 == 0:
    print("The number is divisible by 5.")
else:
    print("The number is not divisible by 3 or 5.")

Question 7:

Write a program to check if a number is positive and even using nested if.

Answer:

number = int(input("Enter a number: "))

if number > 0:
    if number % 2 == 0:
        print("The number is positive and even.")
    else:
        print("The number is positive but odd.")
else:
    print("The number is not positive.")

Question 8:

Write a program to find the largest of three numbers.

Answer:

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a > b and a > c:
    print("The largest number is:", a)
elif b > c:
    print("The largest number is:", b)
else:
    print("The largest number is:", c)

Question 9:

Write a program to check if a character is a vowel or a consonant.

Answer:

char = input("Enter a character: ").lower()

if char in 'aeiou':
    print("The character is a vowel.")
else:
    print("The character is a consonant.")

Question 10:

Write a program to determine if a year is a leap year or not.

Answer:

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

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print("The year is a leap year.")
else:
    print("The year is not a leap year.")

These examples will help you understand and practice Python's decision-making concepts effectively.




Post a Comment

0 Comments