Skip to main content

loop structure in python for loop and while loop पायथन में लूप संरचना फॉर लूप और व्हाइल लूप

A loop structure or iterative structure or repetitive structure in programming refers to a control flow mechanism that allows a set of instructions or code to be executed repeatedly based on a condition or over a collection of items. Loops help automate repetitive tasks and reduce redundancy in code.

प्रोग्रामिंग में एक लूप संरचना या पुनरावृत्त संरचना या दोहराव संरचना एक नियंत्रण प्रवाह तंत्र को संदर्भित करती है जो किसी शर्त के आधार पर या वस्तुओं के संग्रह पर निर्देशों या कोड के एक सेट को बार-बार निष्पादित करने की अनुमति देती है। लूप्स दोहराए जाने वाले कार्यों को स्वचालित करने और कोड में अतिरेक को कम करने में मदद करते हैं।

Key Components of a Loop Structure:-
लूप संरचना के प्रमुख घटक:-

A.) Initialization:- The starting point or value for the loop.
ए.) इनिशियलाइज़ेशन:- लूप के लिए शुरुआती बिंदु या मान।

B.) Condition:- The logical test that determines whether the loop should continue or stop.
बी) स्थिति:- तार्किक परीक्षण जो यह निर्धारित करता है कि लूप जारी रहना चाहिए या बंद हो जाना चाहिए।

C.) Iteration:- The process of moving from one execution of the loop to the next. Increments or decrements are common forms of iteration.
सी.) पुनरावृत्ति:- लूप के एक निष्पादन से दूसरे निष्पादन तक जाने की प्रक्रिया। वृद्धि या कमी पुनरावृत्ति के सामान्य रूप हैं।

D.) Termination:- When the condition fails, the loop ends, and the program continues with the next block of code.
डी.) समाप्ति:- जब स्थिति विफल हो जाती है, तो लूप समाप्त हो जाता है, और प्रोग्राम कोड के अगले ब्लॉक के साथ जारी रहता है।

In Python, there are two main loop structures `for` loop and `while` loop.
पायथन में, दो मुख्य लूप संरचनाएं हैं `फॉर` लूप और `व्हाइल` लूप।

1.) For Loop:- A `for` loop is used to iterate over a sequence (like a list, tuple, string, or range) and execute a block of code for each item in that sequence.
1.) फॉर लूप : - एक `फॉर` लूप का उपयोग एक अनुक्रम (जैसे एक सूची, टपल, स्ट्रिंग, या रेंज) पर पुनरावृत्त करने और उस अनुक्रम में प्रत्येक आइटम के लिए कोड के एक ब्लॉक को निष्पादित करने के लिए किया जाता है।

Syntax:-

for item in sequence:
 # Block of code to execute

Example 1:-

for i in range(0,5): #range(start,stop)
    print(i)

Output:-

0
1
2
3
4

Example 2:-

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

   print(fruit)

Output:-

apple
banana
cherry

Example 3:-

numbers = [1, 2, 3, 4, 5]

for num in numbers:

   print(num)

Output:-

1
2
3
4
5

Example 4:-

word = "Python"

for letter in word:

   print(letter)

Output:-

P
y
t
h
o
n

2.) While Loop:- A `while` loop repeatedly executes a block of code as long as the given condition is `True`.
2.) व्हाइल लूप:- एक `व्हाइल` लूप बार-बार कोड के एक ब्लॉक को निष्पादित करता है जब तक कि दी गई स्थिति `सही` है।
Syntax:-

while condition:
  # Block of code to execute

Example 1:-

i = 0
while i < 5:
  print(i)
  i += 1

Output:-
0
1
2
3
4

Example 2:-

i = 0
while True: #infinite loop
  i += 1
  print(i)
  if i == 5:
     break # Breaks the loop when i equals 5

Output:-
1
2
3
4
5

Example 3:-

i = 0
while i < 10:
  i += 1
  if i % 2 == 0:
     continue # Skips the even numbers
  print(i)

Output:-

1
3
5
7
9

Comments

Popular posts from this blog

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...

Inbuilt Functions in Python पाइथन में अंतर्निहित फ़ंक्शन्स

Python is a powerful programming language that provides many inbuilt functions to make coding simple and efficient. These functions help programmers perform common tasks like finding length, converting characters, or checking memory addresses without writing extra code. पायथन एक शक्तिशाली प्रोग्रामिंग भाषा है जो कई अंतर्निहित (inbuilt) फ़ंक्शन्स प्रदान करती है, जिससे कोडिंग आसान और प्रभावी हो जाती है। ये फ़ंक्शन प्रोग्रामर को सामान्य कार्य करने में मदद करते हैं जैसे लंबाई निकालना, अक्षरों का रूपांतरण करना या मेमोरी एड्रेस जांचना, और इसके लिए अतिरिक्त कोड लिखने की आवश्यकता नहीं होती। (a)  id()  – Unique Identifier of an Object  किसी वस्तु का अद्वितीय पहचानकर्ता The  id()  function returns the unique memory address of an object. Every variable or object in Python is stored in memory, and  id()  helps us know where exactly it is stored. id()  फ़ंक्शन किसी ऑब्जेक्ट का अद्वितीय मेमोरी एड्रेस लौटाता है। पायथन में हर वेरिएबल या ऑब्जेक्ट मेमोरी में संग्रह...

Conditional statements in Python if, if-else, if-elif-else पायथन में सशर्त कथन

  In Python, conditional statements help you make decisions in the code. There are three main types of conditionals:- पायथन में, सशर्त कथन आपको कोड में निर्णय लेने में मदद करते हैं। सशर्त के तीन मुख्य प्रकार हैं:-  1.) Conditional `if` statement:- An `if` statement executes the code block only if the condition evaluates to `True`. 1.) सशर्त `if` कथन:- एक `if` स्टेटमेंट कोड ब्लॉक को केवल तभी निष्पादित करता है जब स्थिति सही पर मूल्यांकन करती है। Syntax:- if condition:    Statement(s) Example:- x = 10 if x > 5:     print("x is greater than 5") 2.) Alternative `if-else` statement:  An `if-else` statement checks a condition and executes a block of code('if' block), if the condition is true otherwise provides an alternative block of code ('else' block), if the condition is false. 2.) वैकल्पिक `if- else` कथन:- एक `if-else` स्टेटमेंट एक शर्त की जाँच करता है और कोड के एक ब्लॉक ('if' ब्लॉक) को निष्पादित करता है, यदि स्थिति सही है अन्यथा कोड का एक वैकल्पिक ब्ल...