To work with all items in a Python list, we use loops. Let’s explore for loop
, while loop
, enumerate()
, and range()
.
Python list के सभी items को access करने के लिए हम loop का use करते हैं। आइए for loop, while loop, enumerate(), और range() को समझते हैं।
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple banana cherry
fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
print(i, fruits[i])
Output:
0 apple 1 banana 2 cherry
fruits = ["apple", "banana", "cherry"]
i = 0
while i < len(fruits):
print(fruits[i])
i += 1
Output:
apple banana cherry
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)
Output:
0 apple 1 banana 2 cherry
fruits = ["apple", "banana", "cherry"]
[print(fruit.upper()) for fruit in fruits]
Output:
APPLE BANANA CHERRY