Loop in python: Day 10
This article will help you to know about loop function and to use it in python which will help you to make creative projects.😃
What is loop?🤔
In Python, loops are used to execute a block of code repeatedly. Python provides two main types of loops: for loops and while loops. Here's a guide to loops in Python:
1. for Loops
The for loop in Python follows a sequence (like a list, tuple, string, range,).
How It Works
- The
variabletakes each element of theiterableone at a time. - The block of code inside the loops for each element.
Get Samip Jung K.C’s stories in your inbox
Join Medium for free to get updates from this writer.
Examples
a. Iterating over a list:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)b. Iterating over a string:
for char in "hello":
print(char)2. While Loops
The while loop repeats a block of code as long as a specified condition is True.
Examples
Basic example:
count = 0
while count < 5:
print(count)
count += 1Infinite loop (use with caution):
while True:
print("This will run forever")I hope now you have the concept of loop😁. Now make your ow projects using it!!







