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

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...