python while loop | while loop in hindi


 while loop -

लूप तब तक चलता है जब तक कोई condition (शर्त) सही (True) होती है।

Let’s master it step by step:-

1.नंबर तक लूप चलाना-

count = 1
while count <= 5:
    print(count)
    count = count + 1

 Explanation--

count = 1  
(बनाओ एक variable count जिसकी value 1 है)

while count <= 5:  
(जब तक count की value 5 से छोटी या बराबर है, तब तक ये loop चलेगा)

    print(count)  
    (count को print करो)

    count = count + 1  
    (count में 1 जोड़ दो, ताकि अगली बार नई value के साथ check हो)

2.User से input लेना जब तक वो सही जवाब न दे-

password = ""
while password != "1234":
    password = input("Enter password: ")
print("Access granted.")

 Explanation-

password = ""  
(शुरू में password खाली है)

जब तक password "1234" नहीं होता:  
    user से password मांगो  
    (हर बार नया input लो)

जब password सही हो गया, loop रुक गया  
    और message print हुआ

3.Count Down (गिनती उल्टी)-

num = 5
while num > 0:
    print(num)
    num = num - 1

 Explanation-

num = 5  
(बनाओ num जिसकी value 5 है)

while num > 0:  
(जब तक num 0 से बड़ा है)

    print(num)  
    (num को print करो)

    num = num - 1  
    (num में से 1 घटा दो)

 4. While loop with list index (list को index से traverse करो)

fruits = ['apple', 'banana', 'cherry']
i = 0
while i < len(fruits):
    print(fruits[i])
    i = i + 1

 Breakdown-

 fruits नाम की list में 3 चीजें हैं  
i = 0 से शुरू करो

जब तक i list की length से छोटा है:  
    print करो fruits[i]  
    i को 1 बढ़ाओ
 

 

5. while+for+if 

i = 1
while i <= 5:
    for num in [i]:  # for-loop with one element (same as i)
        if num % 2 == 0:
            print(num, "is even")
        else:
            print(num, "is odd")
    i += 1

 Breakdown:-

 i = 1 से शुरू करो 

while i <= 5:

जब तक की i, 5 से कम या बराबर न हो जाये
    for num

 in [i]:  # for-loop with one element (same as i)

    for=लो , num=each in= में से , [i]= मतलब की i की लिस्ट में से जिसमे होंगे i=[1,2,3,4,5]
        if num % 2 == 0:

       if=यदि , num का शेषफल(%) == 0
            print(num, "is even")

            प्रिंट करो num
        else:

      अन्यथा
            print(num, "is odd")

    i += 1 का अर्थ है i=i+1,

 मतलब - यह अंतिम लाइन है और यहाँ कोड पूरा हो रहा है इसलिए  दुबारा i की value i+1

रखो  (क्युकी while लूप है इसलिए कोड शुरू से दुबारा चलेगा ) 

 

⭐ Key Concept   

       ➤ :  का अर्थ होता है की अगली लाइन में इंडेंट रखे (4 space या Tab)

       ➤ हम हर बार प्रिंट इसलिए करवाते है जिससे की यह चेक हो सके की कोड सही से execute हुआ है

 

🎯 Task for You: (give answer in comment)

      1.यूजर से इनपुट में password लीजिये और while लूप को चलाइए जब तक की password न आ जाये?

      2.while लूप का उपयोग करते हुवे एक सिंपल कैलकुलेटर बनाइए ?

      3.while loop के बाद इंडेंट ( 4 space or Tab) क्यों किया जाता है ?

      4. Guess the number game (जब तक सही number न guess हो) ?


 

No comments:

Post a Comment