Conditional Statement क्या होती है ?
Conditional Statement का मतलब जहां पर किसी Condition के अनुसार निर्णय लिया गया हो| या ये भी कह सकते है की Conditional Statement में एक सवाल होता है जिसका ज़वाब या तो सही हो सकता है या गलत| थोड़ा और आसान भाषा में समझते है, मान लीजिये आपको India से USA जाना तो यदि India से USA Flight जाएगी तो आप जा सकते है नहीं तो नहीं जा सकते| तो जाने वाली Condition True होगी और न जाने वाली False| सही हुआ तो 1 Value return होगा, और गलत हुआ तो 0 Return होगा| Python में तीन प्रकार के Conditional Statement होते हैं|
- if Statements
- if..else Statements
- if ..elif..else Statements
- inline if
1. if Statements
if का हिन्दी मतलब होता है "यदि"| जैसे मान लीजिये दो Variable है int टाइप के num1 और दूसरा num2| num1 की value 20 है और num2 की value 40 है, तब कुछ इस तरह लिखा जायेगा-
if (num1<num2):
print(“num2 is greater then num1”)
ऊपर लिखे Statements में एक if Condition है जिसमे ये check किया गया है की क्या num1 की value num2 की value से बड़ी है| इसके बाद (:) sign लगा है इसका मतलब है की अगर condition सही हुई तो if के अंदर complier जायेगा और जो भी statements लिखे होंगे उन्हें perform करेगा और अगर गलत हुआ तो सीधे if block से ही बाहर हो जायेगा|
ध्यान दे जो भी Statements if ब्लॉक के अंदर होंगे वो if ब्लॉक की लाइन से थोड़ा अंदर लिखे जायेंगे
उदाहरण If Statements
print("This program is example of if statements")
num1=input("Enter the first number:- ")
num2=input("Enter the second number:- ")
if int(num1)<int(num2):
print("num2 is greater then num1")
Flow
Chart of if Statement
स्टार्ट और स्टॉप का Sign कैप्सूल के आकार में होता है, Input का sign Rectangle shape में किया जाता है, Condition का Diamond sign में किया जाता है |
if…else statement
if..else statement मतलब यदि
दी
हुई
Condition अगर गलत हो
जाती
है
तो
else ब्लॉक
में
Compiler चला जाता है
और
else ब्लॉक
में
जो
भी
instruction दिया होता है
उसको
execute करता
है|
लेकिन
else में
कोई
भी
Condition नहीं होती
if..elif..else statement
print("This program is example of if statements")
num1=input("Enter the first number:- ")
num2=input("Enter the second number:- ")
if int(num1)<int(num2):
print("num2 is greater then num1")
elif int(num1)==int(num2):
print("num1 is equal then num2")
else:
print("num1 is greater then num2")
4. Inline
if statement
inline
if बहुत ही simple होता है, यह लगभग if.. else
statement जैसा ही काम करता है| यह एक ही लाइन में कंडीशन और statement होता है, कुछ इस तरह
do
Task A if condition is true else
do Task B
उदहारण, मान लीजिये
num1
= 12 if myInt==10 else 13
कुछ इस तरह भी लिखा जा सकता है
print
(“This is task A” if myInt == 10 else “This is task B”)
इस statement “This
is task A” प्रिंट होगा (Task A) यदि myInt = 10 अथवा “This is task
B” (Task B). प्रिंट होगा
What is Loop in Python?
Loop शब्द का मतलब बड़ा सीधा सा है, loop यानी जहाँ से कोई भी चीज़ शुरू होती है वहीं पर ख़तम हो जैसे Circle का Shape| पाइथन में loop का मतलब किसी भी statements को बार बार Execute करता है जब तब की दी हुई Condition गलत न हो|
Table of Contents
What is Loop in Python? |
Types of Loop in Python |
For loop in Python |
While loop in Python |
Break in Python |
Continue in Python |
Try, Except in Python |
For Loop in Python
पायथन में, एक iterate किसी भी Value को Access करने को कहते है जिसे लूप से किया जा सकता है, जैसे कि एक स्ट्रिंग, सूची या टपल| सिंटैक्स कुछ इस प्रकार है:
for a in iterable:
print (a)
Example:
pets = ['cat', 'dog', 'rabbit', 'hamster']
for myPet in pets:
print (myPet)
ऊपर दिए Program में, हम पहले Pets की List Declare करते हैं, और उसे कुछ Value या Members Assign करते है, Value - 'Cat', 'Dog', 'Rabbit' And 'Hamster'| दूसरी लाइन में for loop में myPet नाम का Variable हैं जो Pets list से Value Iterate करता है|
यदि आप इस program को run करते है तो
cat
dog
rabbit
hamster
हम index number भी members दिखा सकते है enumerate() function की मदद से
for index, myPet in enumerate(pets):
print (index, myPet)
अगर इस Program को Run करेंगे तो पाएंगे
0 cat
1 dog
2 rabbit
3 hamster
आइये कुछ String example से समझते है|
msg = ‘Hello’
for i in msg:
print (i)
अगर इस program को run करेंगे तो पाएंगे
H
e
l
l
o
range() function
range() function का उपयोग ये बताने के लिए होता है की loop कहाँ से Start होगा कहाँ पे खतम होगा और step कैसा होगा|
range (start, end, step)
और अगर Start value नहीं दी हो तो automatic जीरो से start होगा| कुछ और उदाहरण
range(5) will make the list [0, 1, 2, 3, 4]
range(3, 10) will make [3, 4, 5, 6, 7, 8, 9]
range(4, 10, 2) will make [4, 6, 8]
आइये समझते है की rang() function कैसे काम करता हैं|
for i in range(5):
print (i)
अगर इस program को run करेंगे तो पाएंगे
0
1
2
3
4
While loop in Python
जैसा कि नाम से पता चलता है, की while loop बार-बार execute होता है जब तक लूप के अंदर निर्देश एक निश्चित शर्त वैध रहती है। while loop का Structure कुछ इस प्रकार हैं|
while condition is true:
do A
Break in Python
जब भी हमें loop के साथ काम करते समय, कभी-कभी loop से बाहर निकलना चाहते हैं, भले ही loop की शर्त पूरी ना हुई हो। ऐसा करने के लिए, हम Break Keyword उपयोग करते हैं|
j = 0
for i in range(5):
j = j + 2
print (‘i = ’, i, ‘, j = ’, j)
if j == 6:
break
यदि आप इस प्रोग्राम को रन करते है, तो कुछ इस तरह का आउटपुट प्राप्त होगा
i = 0 , j = 2
i = 1 , j = 4
i = 2 , j = 6
बिना Break Keyword के प्रोग्राम i = 0 से i =4 तक लूप को जाना चाहिए क्योंकि हमने फंक्शन रेंज(5) का उपयोग किया है। हालांकि के साथ Break Keyword, प्रोग्राम समय से पहले i = 2 पर समाप्त होता है। क्योंकि जब i = 2, होता है तब j= 6 के मान तक पहुँच जाता है और Break Keyword लूप को समाप्त कर देता है।
Continue in Python
Continue Keyword एक बहुत useful है लूप में| जब हम Continue का use करते है, तो Continue के बाद जो भी स्टेटमेंट होता है कम्पाइलर उसे इग्नोर कर देता है, और लूप को अगले स्टेप में ले जाता है| उदहारण से समझते है
j = 0
for i in range(5):
j = j + 2
print (‘\ni = ’, i, ‘, j = ’, j)
if j == 6:
continue
print (‘I will be skipped over if j=6’)
यदि आप इस प्रोग्राम को रन करते है, तो कुछ इस तरह का आउटपुट प्राप्त होगा
i = 0 , j = 2
I will be skipped over if j=6
i = 1 , j = 4
I will be skipped over if j=6
i = 2 , j = 6
i = 3 , j = 8
I will be skipped over if j=6
i = 4 , j = 10
I will be skipped over if j=6
Try, except in Python
Try, Except का यूज़ तब करते है जब प्रोग्राम में कोई logically error आती है| इसका मतलब यह की कम्पाइलर जब प्रोग्राम को compile करने में कोई error नहीं दिखाता पर जब प्रोग्राम को रन करता है तो वह फस जाता है| उदाहरण से समझते है| उससे पहले try, except का Syntax देखते है|
try:
do something
except:
do something else when an error occurs
उदाहरण
try:
answer =12/0
print (answer)
except:
print (“An error occurred”)
जब आप प्रोग्राम चलाते हैं, तो आपको "एक Error" मिलेगा। ऐसा इसलिए है क्योंकि जब प्रोग्राम दिए हुए स्टेटमेंट को execute करने का प्रयास करता है तब error आती है| स्टेटमेंट 12/0 ब्लॉक में, एक त्रुटि है क्योंकि आप किसी संख्या को शून्य से विभाजित नहीं कर सकते। try ब्लॉक अगले स्टेटमेंट को इग्नोर करता है और ब्लॉक को छोड़कर except block में चला जाता है|
Post a Comment