Python Looping Statements

Table Of Contents:

  1. What Are Looping Statements?
  2. Types Of Looping Statements.
  3. while Loop.
  4. for Loop.
  5. Nested Loop.

(1) What Are Looping Statements?

  • In the programming world, your code executes sequentially. That means, the first statement in a function is executed first, followed by the second, and so on.
  • There will come a situation where you need to execute a block of code several times.
  • At that time you can use, a python loop statement that will execute a statement or group of statements multiple times.

(2) Types Of Looping Statements?

  1. while loop
  2. for loop
  3. Nested Loop

(3) While Loop

  • A while loop is used to execute a block of statements repeatedly until a given condition is satisfied.
  • And when the condition becomes false, the line immediately after the loop in the program is executed.

While Loop Syntax

while condition:
   Body Of While Loop.

Example-1

count = 0
while (count < 9):
    print('The count is:', count)
    count = count + 1

print("Good bye!")
Output:
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

Note:

  • This While loop will execute till the count value is reaching to 8.
  • It will execute 9 times, from 0 to 8.

Example-2

count = 0
while count < 6:
    print(count, " is  less than 6")
    count = count + 1
else:
    print(count, " is not less than 6")
Output:
0  is  less than 6
1  is  less than 6
2  is  less than 6
3  is  less than 6
4  is  less than 6
5  is  less than 6
6  is not less than 6

Note:

  • Python supports having an else statement associated with a loop statement.
  • If the else statement is used with a while loop, the else statement is executed when the condition becomes false.

(4) For Loop

  • For loops are used for traversing sequential variables like list, tuple, set, and dictionary.
  • It will iterate over individual elements of a list, tuple, set, dictionary, or any sequence.
  • The process of traversing a sequence is known as iteration.
  • You can iterate over in both directions from front to back and from back to front.

For Loop Syntax

for value in sequence:  
     Body Of For Loop  

Example-1

numbers = [1,2,3,4,5]  
   
for value in numbers:  
    print(value)
Output:
1
2
3
4
5

Note:

  • This program will iterate over a list element and print the values.

Example-2

message = "Hello World"
for i in message:
    print(i)
Output:
H
e
l
l
o
 
W
o
r
l
d

Note:

  • This program will iterate over a string object and print the values.

(5) Nested Loops

  • Python programming allows using one loop inside another loop.
  • When you come across such a scenario, you can use Nested loops.

Nested Loop Syntax

for iterating_var in sequence:
   for iterating_var in sequence:
      statements(s)
   statements(s)
while expression:
   while expression:
      statement(s)
   statement(s)

Example-1

rows = int(input('Enter Number Of Rows'))
k = 0
for i in range(1,rows+1):
    for space in range(1,(rows-i)+1):
        print(end="  ")
    while k != (2*i-1):
        print("*",end=" ")
        k += 1
    k = 0
    print()
Enter Number Of Rows5
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 

Note:

  • This program will print a pyramid pattern using *.

Example-2

i = 2
while(i < 100):
    j = 2
    while(j <= (i/j)):
        if not(i%j): break
        j = j + 1
    if (j > i/j) : print(i, " is prime")
    i = i + 1

print("Good bye!")
Output:
2  is prime
3  is prime
5  is prime
7  is prime
11  is prime
13  is prime
17  is prime
19  is prime
23  is prime
29  is prime
31  is prime
37  is prime
41  is prime
43  is prime
47  is prime
53  is prime
59  is prime
61  is prime
67  is prime
71  is prime
73  is prime
79  is prime
83  is prime
89  is prime
97  is prime
Good bye!

Note:

  • This program will print all the prime numbers between 0 to 100.

Leave a Reply

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