Saturday , September 18 2021
Home / Python / Python Loops
Python Loops - For Loop, While Loop and Nested Loops

Python Loops

4 Looping Statement

Python Loops

Python has two primitive loop commands:

  • for loops
  • while loops

For Loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

In [1]:
L=[10,20,"Machine Learning",40,50]
for i in L:
    print("Printing",i)
Printing 10
Printing 20
Printing Machine Learning
Printing 40
Printing 50

The range() Function

To loop through a set of code a specified number of times, we can use the range() function

In [2]:
L=list(range(10))
for i in L:
    print(i,end=' ')   
0 1 2 3 4 5 6 7 8 9 
In [3]:
for i in range(10):
    print(i,end=' ')
0 1 2 3 4 5 6 7 8 9 
In [4]:
for i in range(1,10):
    print(i,end=' ')
1 2 3 4 5 6 7 8 9 
In [5]:
for i in range(1,10,1):
    print(i,end=' ')
1 2 3 4 5 6 7 8 9 
In [6]:
for i in range(1,10,2):
    print(i,end=' ')
1 3 5 7 9 
In [7]:
for i in range(5):
    print("MachineLearning.org.in")
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in

For Loop Control Statement

In [8]:
for i in range(1,10):
    if i%3==0:
        print(i,end=' ')
3 6 9 

Looping Through a String

Even strings are iterable objects, they contain a sequence of characters:

In [9]:
for x in "python":
    print(x)
p
y
t
h
o
n

For Loop with break Statement

With the break statement we can stop the loop before it has looped through all the items:

In [10]:
for i in range(1,10):
    if i==5:
        break
    print(i,end=' ')
1 2 3 4 

For Loop with Continue Statement

With the continue statement we can stop the current iteration of the loop, and continue with the next:

In [11]:
for i in range(1,10):
    if i==5:
        continue
    print(i,end=' ')
1 2 3 4 6 7 8 9 

Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

In [12]:
for x in range(6):
    print(x)
else:
    print("Finally finished!")
0
1
2
3
4
5
Finally finished!

Nested For Loop

A nested loop is a loop inside a loop.
The “inner loop” will be executed one time for each iteration of the “outer loop”:

In [13]:
adj = ["Red", "Big", "Tasty"]
fruits = ["Apple", "Watermelon", "Mango"]

for x in adj:
    for y in fruits:
        print(x, y)
Red Apple
Red Watermelon
Red Mango
Big Apple
Big Watermelon
Big Mango
Tasty Apple
Tasty Watermelon
Tasty Mango

The while Loop

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

In [14]:
i=1
while(i<=5):
    print("MachineLearning.org.in")
    i=i+1
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
In [15]:
i = 1
while i <= 10:
    print(i,end=' ')
    i += 1
1 2 3 4 5 6 7 8 9 10 

The break Statement

With the break statement we can stop the loop even if the while condition is true:

In [16]:
i = 1
while i <= 10:
    if i==4:
        break
    print(i)
    i += 1
1
2
3

The continue Statement

With the continue statement we can stop the current iteration, and continue with the next:

In [17]:
i = 0
while i <= 10:
    i += 1
    if i==4:
        continue
    print(i) 
1
2
3
5
6
7
8
9
10
11

The else Statement

With the else statement we can run a block of code once when the condition no longer is true:

In [18]:
i = 1
while i < 6:
    print(i)
    i += 1
else:
    print("i is no longer less than 6")
1
2
3
4
5
i is no longer less than 6

About Machine Learning

Check Also

Python MySQL Insert Into Table

4 Python MySQL Insert Into Table Python MySQL Insert Into Table¶To fill a table in …

Leave a Reply

Your email address will not be published. Required fields are marked *