Sitemap

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.😃

Press enter or click to view image in full size

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,).

Press enter or click to view image in full size

How It Works

  • The variable takes each element of the iterable one 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 += 1

Infinite 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!!

Written by Samip Jung K.C

Python Learner aiming to be a fabulous programmer.