Chapter 9Python Tutorial~1 min read
Loops in Python
Loops — Repetition करणे
Loop म्हणजे एकच code block वारंवार run करणे. Python मध्ये for loop आणि while loop असे दोन प्रकार आहेत.
for Loop
for loop — sequence iterate करणे
python
# List iterate करणे
fruits = ["Apple", "Mango", "Banana"]
for fruit in fruits:
print(fruit)
# Output: Apple, Mango, Banana
# String iterate करणे
name = "Rahul"
for char in name:
print(char, end="-")
# Output: R-a-h-u-l-
# range() वापरून numbers
for i in range(5): # 0 ते 4
print(i, end=" ")
# Output: 0 1 2 3 4
for i in range(1, 6): # 1 ते 5
print(i, end=" ")
for i in range(0, 10, 2): # 0, 2, 4, 6, 8 (step=2)
print(i, end=" ")while Loop
while loop — condition True असेपर्यंत
python
# Basic while loop
count = 5
while count > 0:
print(count)
count -= 1
# Output: 5, 4, 3, 2, 1
# while with else
x = 3
while x > 0:
print(x)
x -= 1
else:
print("Loop संपला!") # condition False झाल्यावर run होतोbreak, continue, pass
Loop control statements
python
# break — loop थांबवतो
for i in range(1, 10):
if i == 5:
break
print(i, end=" ")
# Output: 1 2 3 4
# continue — current iteration skip करतो
for i in range(1, 8):
if i % 2 == 0:
continue
print(i, end=" ")
# Output: 1 3 5 7
# pass — placeholder, काहीही करत नाही
for i in range(5):
pass # TODO: later लिहू✅ Key Points — लक्षात ठेवा
- ▸for loop — sequence/range iterate करतो
- ▸range(start, stop, step) — number sequence
- ▸while loop — condition True असेपर्यंत चालतो
- ▸break — loop पूर्ण थांबवतो
- ▸continue — current iteration skip, loop चालू राहतो
0/12 chapters पूर्ण