0% found this document useful (0 votes)
10 views2 pages

What Is A Loop

A loop in programming is a construct that allows for repeated execution of a block of code until a specified condition is met. In Python, there are two main types of loops: 'for loops' for a known number of iterations and 'while loops' for an unknown number of iterations based on a condition. Examples include using a 'for loop' to print 'Hello' five times and a 'while loop' to print 'Hello' until a counter exceeds five.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

What Is A Loop

A loop in programming is a construct that allows for repeated execution of a block of code until a specified condition is met. In Python, there are two main types of loops: 'for loops' for a known number of iterations and 'while loops' for an unknown number of iterations based on a condition. Examples include using a 'for loop' to print 'Hello' five times and a 'while loop' to print 'Hello' until a counter exceeds five.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

What is a Loop?

A loop is like telling the computer:

"Hey, repeat this thing again and again until I tell you to stop."

Types of Loops in Python

There are two main types:

1. for loop — when you know how many times you want to repeat something.

2. while loop — when you don't know how many times, and you want to keep going
while a condition is true.

Example 1: for loop (counting specific times)

Suppose you want to say "Hello" 5 times.

for i in range(5):

print("Hello")

What happens here?

• range(5) means numbers from 0 to 4 (5 times total)

• i takes each number in that range

• print("Hello") runs 5 times

while loop (repeat until condition is false)


i=1 # Start with 1

while i <= 5: # Keep looping while i is less than or equal to 5

print("Hello") # Print Hello

i=i+1 # Increase i by 1 each timeAdd 1 to i

Repeat until i is no longer less than or equal to 5


Loop with list

Print all the fruits in a basket.

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

print(fruit)

You might also like