Conditional Statement in Python

Conditional Statement in Python

In Python, Conditional Statement performs different actions depending on whether a specific Boolean constraint evaluates to true or false.

पॉयथन में, कंडीशनल स्टेटमेंट यह सुनिश्चित करता है कि एक विशिष्ट बूलियन कॉन्सट्रेन्ट टू या फाल्स के आधार पर अलग-अलग कार्य करती है।

Python programming language provides following types of decision making statements.

पॉयथन प्रोग्रामिंग भाषा निम्नलिखित प्रकार के डिसीजन मेकिंग स्टेटमेन्ट प्रदान करती है।

  1. if statement (one way decisions)

  2. if .. else statement (two way decisions)

  3. if ..elif ..else statements (multi-way decisions)

  4. Nested if .. else (inner decisions)

In conditions, the following comparison or relational operators commonly using:

कंडीशन में, निम्नलिखित कम्पेरिजन या रिलेशनल ऑपरेटर आमतौर पर उपयोग करते हैं:

  1. >greater than – से अधिक

  2. >= greater than equal to – से अधिक या बराबर है

  3. <less than – से कम

  4. <= less than equal to – से कम या बराबर

  5. == equal – बराबर

  6. != not equal – बराबर नहीं

if and simple programs
ईफ और सिंपल प्रोग्राम

In Python, If Statement is used for decision making. Decision making is required when we want to execute a code only if a certain condition is satisfied. Here, the program evaluates the test-expression and will execute statement(s) only if the text expression is True. If the text expression is False, the statement(s) is not executed.

पॉयथन में, ईफ स्टेटमेन्ट का उपयोग निर्णय लेने के लिए किया जाता है। निर्णय लेने की आवश्यकता तब होती है जब हम किसी निश्चित कंडीशन से संतुष्ट होने पर ही किसी कोड को एक्सिक्यूट करना चाहते हैं। यहां, प्रोग्राम टेस्ट-एक्सप्रेशन का मूल्यांकन करता है और स्टेटमेंट को एक्सिक्यूट करेगा तभी यदि टेक्स्ट एक्सप्रेशन सही है। यदि टेक्स्ट एक्सप्रेशन गलत है, तो स्टेटमेन्ट एक्सिक्यूट नहीं किया गया है।

Python interprets non-zero values as ‘True’. ‘None’ and ‘0’ are interpreted as ‘False’.

पॉयथन नॉन-जीरो वैल्यू को ‘टू’ के रूप में इन्टरप्रेट्स करता है। ‘कोई नहीं’ और ‘0’ को इन्टरप्रेटेड ‘फाल्स’ के रूप में करता है।

Syntax:
if <test-expression>:
Statement(s)
Example 1:

x = 100
y  = 200 
if x < y:
      print("x is less than y.")

In this example we use two variables, ‘x’ and ‘y’, which are used as part of the if statement to test whether ‘x’ is less than ‘y’ & display a message.

इस उदाहरण में हम दो वैरिएबल का उपयोग करते हैं, ‘x’ और ‘y’, जो ईफ स्टेटमेन्ट के भाग के रूप में उपयोग किया जाता । है चेक करने के लिए यदि ‘x’ ‘y’ से छोटा है और मैसेज दिखाता है।

Output:
x is less than y

Indentation: Since other programming languages uses curly-braces { } to define a block of code but in place of this python uses indentation. Python depend on indentation, using white space, to define scope in the code. The enforcement of indentation in Python makes the code look neat and clean. This results into Python programs that look similar and consistent. The code block which is either body of a function or loop etc is starts with indentation and ends with the first unidentified line.

इंडेटेशनः चूंकि अन्य प्रोग्रामिंग लैंगवेजेस कोड के एक ब्लॉक को परिभाषित करने के लिए कर्ली-ब्रेसेस { } का उपयोग करती हैं लेकिन इसके स्थान पर पाइथन इंडेंटेशन का उपयोग करता है। पायथन इंडेंटेशन पर निर्भर करता है इसके कोड में इंडेंटेशन को परिभाषित करने के लिए, सफेद स्थान का उपयोग होता है। पायथन में इंडेंटेशन का प्रवर्तन कोड को साफ और स्वच्छ बनाता है। यह ऐसे पायथन प्रोग्रामों को देता है जिसके कोड काफी समान और सुसंगत दिखते हैं। कोड ब्लॉक जो या तो फंक्शन या लूप आदि का बॉडी होते है वे इंडेंटेशन के साथ शुरू होता है और पहली अनइंडेंटेड लाइन के साथ समाप्त होते है।

Example 2: In Python, without indentation will raise an error message.

पॉयथन में, बिना किसी इंडेंटेशन के एक एरर मैसेज आएगा।

In Python, the body of the ‘if statement is indicated by the indentation. Body starts with an indentation and the first unidentified line marks the end.

‘ पॉयथन में, ‘if statement’ की बॉडी को इंडेंटेशन द्वारा इंडीकेट किया गया है। बॉडी एक इंडेंटेशन के साथ प्रारम्भ होता है और पहली अन-इंडेंट लाइन अंत का प्रतीक है।

In a Python program, contiguous statements that are indented to the same level are considered to be part of the same block.

पॉयथन प्रोग्राम में, एक ही स्तर के स्टेटमेन्ट जो समान लेवल पर इन्डेन्टेड होते है उसी ब्लॉक का हिस्सा माने जाते हैं।

Thus, a compound if statement in Python looks like this:

इस प्रकार, पॉयथन में एक कंपाउंड ईफ स्टेटमेंट इस तरह दिखता है:
Example 3:

a = int(input ("Enter a value : ")) 
if a>0:
print ("Line 1") 
print ("Line 2") 
print("......")
print ("End") 
print("after if block")

Output:
Enter a value : 100
Line 1
Line 2
End after if block
>>> 
Restart: Program5.Py
Enter a value = -1
after if block
Example 4:

x = input ("Enter any number: ")
if int (x) >0:
print("+Ve Value") 
print("First if block statement")
print("outside of the First if block") 
x = input ("Enter any number: ") 
if int (x) <0:
print("-Ve Value") 
print ("Second if block statement") 
print("outside of the Second if block")

Output:
Enter any number: 12
+Ve Value
First if block statement
outside of the First if block
Enter any number: -54
-Ve Value
Second if block statement
outside of the Second if block
Example 5:

name = input ("Enter your Name :") 
if name =='python':
print ("Hello", name) 
print ("Welcome") 
print ("How are you ...", name)

Output:
Enter your Name :python
Hello python
Welcome
How are you .. python
Restart: Program6.Py
Enter your Name : Program
How are you . . . . Program
if-else and simple programs
ईफईल्स और सिम्पल प्रोग्राम

An ‘else’ statement can be combined with an ‘if statement. An ‘else’ statement contains a block of code that executes if the conditional expression in the ‘if statement resolves to ‘O’ or a ‘False’ value. The ‘else’ statement is an optional statement and there could be at the most only one else statement following if.

एक ‘else’ स्टेटमेन्ट को ‘if स्टेटमेन्ट के साथ जोड़ा जा सकता है। एक ‘else’ स्टेटमेंट में कोड का एक ब्लॉक होता है, जो एक्सिक्यूट होता है यदि कंडीशनल एक्सप्रेशन ईफ स्टेटमेंट मे ‘0’ या ‘फाल्स’ वैल्यू को हल करता है ‘else’ स्टेटमेन्ट एक वैकल्पिक स्टेटमेन्ट है और केवल एक ईफ स्टेटमेन्ट के लिए ईफ के बाद केवल एक ‘else’ स्टेटमेन्ट होता है।

Syntax:
if <test-expression>:
<statement(s)>
else:
<statement(s)>

If <test-expression> is true, the first suite or block is executed, and the second is skipped.

यदि <test-expression> सत्य है, तो पहला सूट या ब्लॉक एक्सिक्यूट किया जाता है, और दूसरा छोड़ दिया जाता है। If <test-expression> is false, the first suite is skipped and the second is executed.

यदि <test-expression> गलत है, तो पहला सूट छोड़ दिया जाता है और दूसरा एक्सिक्यूट किया जाता है।
Example :

val = int (input ("Enter a Number : ") ) 
if val> 0 :
print ("Positive number") 
else:
print ("Negative number")

In the above example, when ‘val’ is above, the test expression is ‘true’ and body of ‘if’ is executed and body of ‘else’ is skipped.

उपरोक्त उदाहरण में, जब ‘val 0 से ऊपर है, तो टेस्ट एक्सप्रेशन ‘टू’ है और ‘if’ की बॉडी को एक्सिक्यूट किया जाता है। और ‘else’ बॉडी को छोड़ दिया जाता है।

Output:
Enter a Number :15
Positive number

If ‘val’ is below ‘O’, the test expression is ‘false’ and body of ‘else’ is executed and body of ‘if is skipped.

यदि ‘val’ ‘0’ से नीचे है, टेस्ट एक्सप्रेशन ‘फाल्स’ है और ‘else’ की बॉडी को एक्सिक्यूट किया जाता है और ‘if की बॉडी को तो स्किप्ड कर दिया जायेगा।

Output:
Enter a Number: -30
Positive number

if-elif-else; simple programs

The ‘elif statement allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True.

‘elif स्टेटमेंट आपको टू के लिए कई एक्सप्रेशन को चेक करने और जैसे ही एक कंडीशन टू होती है कोड के एक ब्लॉक को एक्सिक्यूट करने की अनुमति देता है।

Syntax:
if<test-expression>:
     Body of if
elif<test-expression>:
     Body of elif
else:
    Body of else

If the condition for if is ‘False’, it checks the condition of the next ‘elif block’ and so on. If all the conditions are False, body of ‘else’ is executed. Only one block among the several ‘if…elif…else blocks’ is executed according to the condition. The’if block can have only one ‘else block’, but it can have multiple ‘elif blocks’.

यदि ‘if के लिए कंडीशन ‘फाल्स’ है, तो यह अगले ‘elif block’ के कंडीशन को चेक करता है और इसी तरह बार-बार करता है। यदि सभी कंडीशन फाल्स है, तो ‘else’ के बॉडी को एक्सिक्यूट किया जाता है। यदि कई में से एक ब्लॉक है, तो ‘if…elif…else blocks’ को कंडीशन के अनुसार एक्सिक्यूट किया जाता है। यदि ईफ ब्लॉक के पास केवल एक ‘else’ ब्लॉक है, लेकिन आपके पास कई ‘elif blocks’ हो सकते हैं।

Example: Find Biggest value of given 3 numbers from the command line.
कमांड लाइन से दिए गए 3 नंबर का सबसे बड़ी वैल्यू ज्ञात कीजिए।

a = int (input ("Enter a value :")) 
b = int (input ("Enter b value :"))
c = int(input ("Enter c value :") )
if a > b & a > c:
print("a is greater") 
elif b > c:
print("b is greater") 
else:
print ("c is greater")

Output:
Enter a value : 1
Enter b value : 2
Enter c value : 3
c is greater
Restart: Program1.Py
Enter a value : 3
Enter b value : 2
Enter c  value :1
a is greater

Chapter wise Model Paper Link

About Me