Conditional Statements in Python in Hindi
Python में Conditional statements, प्रोग्राम को कुछ निश्चित कंडीशन के आधार पर निर्णय लेने में मदद करता है। ये प्रोग्राम को एक कंडीशन की जांच करने में मदद करता है कि वह True है या False है और उसके आधार पर code को रन करता है।यह प्रोग्राम के flow को नियंत्रित करने में मदद करता है। यदि condition सही है तो code का एक विशिष्ट block रन होगा। यदि condition गलत है तो प्रोग्राम या तो कुछ नहीं करेगा या code के एक block को रन करेगा।
ये statements, logic या comparison का उपयोग करके values की जांच करते हैं। उदाहरण के लिए वे यह जांच करते हैं कि क्या एक नंबर अन्य नंबर से बड़ा है, छोटा है या बराबर है।
यह प्रोग्राम को यह निर्णय लेने में मदद करता है कि क्या करना है। Python में, कोड के उस ब्लॉक को जो किसी शर्त (condition) पर निर्भर करता है, अलग दिखाने के लिए indentation का उपयोग किया जाता है।
Python में मुख्य conditional statements
if Statement
Python में `if` statement का उपयोग एक प्रोग्राम में निर्णय लेने के लिए किया जाता है। यह कंडीशन की जांच करता है और यदि कंडीशन True है तो यह कोड के एक ब्लॉक को रन करता है।यदि कंडीशन, false है तो यह कोड के ब्लॉक को छोड़ देता है और प्रोग्राम के अगले भाग पर चला जाता है।
condition, आमतौर पर एक तुलना होती है जैसे `x > 5` या `x == 10`.।
Python, Indentation (line के पहले space ) का उपयोग या दिखाने के लिए करता है कि कौन सा ब्लॉक `if` block से सम्बन्धित है। यह flow को स्पष्ट रूप से नियंत्रित करने में मदद करता है।
Python, Indentation (line के पहले space ) का उपयोग या दिखाने के लिए करता है कि कौन सा ब्लॉक `if` block से सम्बन्धित है। यह flow को स्पष्ट रूप से नियंत्रित करने में मदद करता है।
Syntax:
if condition:
# This code will run if the condition is true
Example
temperature = 35 if temperature > 30: print("It's a hot day")
Output: It's a hot day
if else Statement
Python `if else` statement, का उपयोग उस समय होता है जब आप चाहते हैं कि प्रोग्राम दो विकल्पों में से एक का चयन करे।पहले, `if` condition, की जांच की जाती है। यदि condition, true है तो `if` block के अंदर का कोड रन होता है। यदि condition, false है तो `else` block के अंदर का कोड रन होता है।
इस तरीके से कंडीशन के आधार पर प्रोग्राम विभिन्न चीजों को कर सकता है। यह निर्णयों को सही ढंग से नियंत्रित करने में मदद करता है।
Syntax:
if condition1:
# code block if condition1 is true
if condition2:
# code block if both condition1 and condition2 are true
else:
# code block if condition1 is true but condition2 is false
else:
# code block if condition1 is false
Example:
year = int(input("Enter a year: ")) if year % 4 == 0: print("It is a leap year") else: print("It is not a leap year")
Output: Enter a year: 25 It is not a leap year
elif Statement
Python में `elif` statement "else if को संक्षिप्त रूप है। इसका उपयोग एक `if` के बाद कई conditions की जांच करने में किया जाता है।यदि `if` condition, false है तब Python, `elif` condition की जांच करता है। यदि यह true है तब `elif` के भीतर का code रन होता है।
आप अलग-अलग conditions की जांच करने के लिए कई `elif` statements का उपयोग कर सकतें है। यदि `if` या `elif` में से कोई भी कंडीशन true नहीं है
तो आप बचे हुए case को संभालने के लिए `else` का उपयोग कर सकते हैं। यह दो से अधिक विकल्पों के साथ निर्णय लेने में मदद करता है।
Syntax:
if condition1:
# code block if condition1 is true
elif condition2:
# code block if condition2 is true
elif condition3:
# code block if condition3 is true
else:
# code block if none of the above conditions are true
Example:
signal = "yellow" if signal == "red": print("Stop") elif signal == "yellow": print("Wait") elif signal == "green": print("Go") else: print("Invalid signal")
Output: Wait
Nested if statement
एक Nested if statement का मतलब एक `if` statement के अंदर अन्य `if` statement का उपयोग करना। यह तभी अधिक conditions की जांच करने में मदद करता है जब पहली condition true हो।Python पहले outer if की condition को check करता है। अगर वह true होती है, तो वह अंदर जाता है और inner if को check करता है। यह तब उपयोगी होता है जब decision एक से ज्यादा conditions पर निर्भर करता है।
Syntax:
if condition1:
if condition2:
# code runs if both condition1 and condition2 are true
else:
# code runs if condition1 is true but condition2 is false
else:
# code runs if condition1 is false
Example:
username = "akhil" password = "4789" if username == "akhil": if password == "4789": print("Login successful") else: print("Wrong password") else: print("Invalid username")
Output: Login successful
Python में conditional statements का क्या उपयोग है
Python में conditional statements के निम्न उपयोग है।1. इसका उपयोग यह जांच करने के लिए होता है कि यूजर क्या टाइप करता है और इनपुट के आधार पर विभिन्न कार्य को करने के लिए।
2. यह सुनिश्चित करता है कि उत्तर सही है जैसे यह जांचना कि कोई संख्या किसी निश्चित सीमा के भीतर है या नहीं।
3. एक गेम में क्या होता है यह नियंत्रित करने के लिए जैसे गेम में जीतना, हारना, या अगले स्तर पर जाना।
4. यह निर्णय करने के लिए की कंडीशन के आधार पर code का कौन सा हिस्सा रन होना चाहिए।
5. निश्चित मानो या स्थितियों के आधार पर स्क्रीन पर विभिन्न संदेशों को दिखाने के लिए।
6 login के दौरान enter किया गया username और password सही है या नहीं इसे varify करने के लिए।
7. समय के आधार पर विभिन्न कार्यों को परफॉर्म करने के लिए जैसे “Good Morning” या “Good Night” दिखाने के लिए
Logical Operators के साथ Conditions
Logical operators हमे conditional statements जैसे `if`, `elif`, या `while` के अंदर कई conditions को मिलाने में मदद करता है।Python के पास logical operators है।
'and`
जब सभी conditions true रहता है तब `True` लौटता है।Example:
age = 20
citizen = "India"
if age >= 18 and age <= 120 and citizen == "India":
print("You are eligible to vote")
else:
print("You are not eligible to vote")
Output: You are eligible to vote
`or`
जब कोई भी एक condition, सही रहता है तब True लौटाता है।Example:
flower = "Rose" if flower == "Rose" or flower == "Lotus": print("Red colour")
Output: Red colour
`not`
यह condition को उल्टा करता है। अर्थात not किसी भी Boolean वैल्यू को उल्टा (reverse) कर देता है।not True → False
not False → True
Example:
is_present = False if not is_present: print("You are absent in class") else: print("You are present in class")
Output: You are absent in class
Explanation
is_present = False → इसका मतलब है कि student, absent हैं।
not is_present → यह सही है क्योंकि student present नही है इसलिए यह "You are absent in class" print करता है।
Logical operators, conditional statements को ज्यादा शक्तिशाली बनाता है। क्योंकि यह एक समय में कई conditions को जांच करने की सुविधा देता है।
Practical Examples (व्यवहारिक उदाहरण)
Example1: Age check program
age = int(input("Enter your age: ")) if age < 13: print("You are a child") elif age >= 13 and age < 20: print("You are a teenager") elif age >= 20 and age < 60: print("You are an adult") else: print("You are a senior citizen")
Output: Enter your age: 25 You are an adult
Example2: Number comparison
num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if num1 > num2: print("First number is greater") elif num1 < num2: print("Second number is greater") else: print("Both numbers are equal")
Output: Enter first number: 14 Enter second number: 17 Second number is greater
Example 3: Marks grading system
marks = int(input("Enter your marks: ")) if marks >= 90: print("Grade: A") elif marks >= 80: print("Grade: B") elif marks >= 70: print("Grade: C") elif marks >= 60: print("Grade: D") elif marks >= 40: print("Grade: E") else: print("Grade: F (Fail)")
Output: Enter your marks: 68 Grade: C
0 टिप्पणियाँ