w3ajay

tally tutorial point, ms word 2013, ms excel 2013, ms powerpoint 2010,ccc question with answer in hindi 2021, Tally Prime in hindi , tally prime,Python,in python,programming in python,python

Friday, December 10, 2021

While Loop in Python

While Loop in Python 


Python 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 falls under the category of indefinite iteration. Indefinite iteration means that the number of times the loop is executed isn’t specified explicitly in advance. 


Syntax

The syntax of a while loop in Python programming language is −

variable assign

while expression:

increment/decrement

   statement(s)

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.


When the condition becomes false, program control passes to the line immediately following the loop.


In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.


Flowchart of While Loop : 


Example 1: Python While Loop


# Python program to illustrate
# while loop
a = 0
while (a < 3):
   a = a + 1
    print("Hello Ajay")

Output

Hello Ajay
Hello Ajay
Hello Ajay

Note:-In this  example, the condition for while will be True as long as the counter variable (count) is less than 3. 

Example 2: Python While Loop

a= 0
while a < 5:
   print (a, " is  less than 5")
   a = a + 1
else:
   print (a, " is not less than 5")

Output

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5


Example 3: Python while loop with list


# checks if list still
# contains any element
b = [1, 2, 3, 4]
 
while b:
    print(b.pop())

Output

4
3
2
1

In this example, we have run a while loop over a list that will run until there is an element present in the list.

Single statement while block

Just like the if block, if the while block consists of a single statement we can declare the entire loop in a single line. If there are multiple statements in the block that makes up the loop body, they can be separated by semicolons (;). 

Example 4 Single statement while block


# Python program to illustrate
# Single statement while block
count = 0
while (count < 5): count += 1; print("Hello Ajay")

Output: 

Hello Ajay
Hello Ajay
Hello Ajay
Hello Ajay
Hello Ajay

Example 3: Python while loop with continue statement


# Prints all letters except 'y' and 'L'
i = 0
a = 'ajaypatel'
 
while i < len(a):
    if a[i] == 'y' or a[i] == 'l':
        i += 1
        continue
         
    print('Current Letter :', a[i])
    i += 1

Output: 


Current Letter : a
Current Letter : j
Current Letter : a
Current Letter : p
Current Letter : a
Current Letter : t
Current Letter : e

Example 5: Python while loop with break statement


# break the loop as soon it sees 'j'
# or 'y'
i = 0
a = 'ajay'
 
while i < len(a):
    if a[i] == 'j' or a[i] == 'y':
        i += 1
        break
         
    print('Current Letter :', a[i])
    i += 1

Output: 


Current Letter : a

Pass Statement

The Python pass statement to write empty loops. Pass is also used for empty control statements, functions, and classes.

Example 6: Python while loop with pass statement


# An empty loop
a = 'ajay'
i = 0
 
while i < len(a):
    i += 1
    pass
   
print('Value of i :', i)

Output: 


Value of i : 5

While loop with else

As discussed above, while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed. The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed.

Note: The else block just after for/while is executed only when the loop is NOT terminated by a break statement. 



# Python program to demonstrate
# while-else loop
 
i = 0
while i < 4:
    i += 1
    print(i)
else:  # Executed because no break in for
    print("No Break\n")
 
i = 0
while i < 4:
    i += 1
    print(i)
    break
else:  # Not executed as there is a break
    print("No Break")

Output: 


1
2
3
4
No Break

1

Please read this also
range function in python

No comments:

Post a Comment

for more information please share like comment and subscribe