DEV Community

Ranjith Jr
Ranjith Jr

Posted on

8.Python Loops

Python Loops:

Python has two primitive loop commands:

while loops
for loops

while Loop:

With the while loop we can execute a set of statements as long as a condition is true.

i = 1
while i < 6:
  print(i)
  i += 1

Output :

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

For loop:

A "For" Loop is used to repeat a specific block of code a known number of times

`fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

Output :
apple
banana
cherry`
Enter fullscreen mode Exit fullscreen mode

Type of Loops:

For Loop:

A for loop in Python is used to iterate over a sequence (list, tuple, set, dictionary, and string). Flowchart.

While Loop:

The while loop is used to execute a set of statements as long as a condition is true.

*Nested Loop : *

If a loop exists inside the body of another loop, it is called a nested loop.

Top comments (0)