while loop in Python.


while loop in python.

In this post we are going to learn about while loop in Python. Let's start..!


loop :

First thing we should know that What is loop ? Loop is very simple concept that doing same thing again and again. We use loops for repeating something. Suppose you want to print your name on console ( screen ) What you will do ? You will simply write print("name") statement. That's correct. But What will you do ? If you to print your name on console 100 times or 1000 times. Here we will not be writing print statement 100 or 1000 times. So we will use a Loop there. 

But Which loop ? and How we will use it ? We are using while loop here. Let's learn about while loop.


while loop :

To use while loop we need a keyword that is while. After that we have to specify a condition that how many times this loop will execute. Otherwise loop will execute infinity times. After this you have to put colon ( : ) there.

Now you can write a statements that you want to execute how many times you wants with proper indentation. 

[ if you don't know about indentation then click here  to learn about indentation in python

Syntax : 

while condition :

    while block

For ex. Print hello 5 times on console ( screen ). For this first we have to set a counter ( for counting how many times hello was printed ) simply initialize a variable count = 1 then write a keyword while and write a condition put colon ( : ) after condition. So condition is count <= 5 ( here instead of 5 you can write any number that how many times you want that block execute ) Now write a print statement ( here you can do any operations or print whatever you want ) ; after this you have to increment a counter. Simply write count = count + 1. This is a block so it should be in proper indentation. 

[ if you don't know how these operator works then click here to learn more about Operators in Python ]

print("\n")
# using while loop 
count = 1  # setting a counter 

while count <= 5:
   print("hello")  # printing hello 
   count = count + 1  # incrementing counter

print("\nTask completed ")


This will print hello five times and Task completed.



Now we What is Nested Loop ? We have studied nested if else so now you know what nested means. Simply loop inside a loop is a nested loop. 

For this we have to create a simple while loop and write a while loop inside a while loop.

Syntax : 

while condition : 

    outer_while block

    while condition : 

        inner_while block

for ex. Print hello..! and after hello print hi 3 times. and this combination will execute 3 times. For this you have to create a simple while loop and inside while block initialize a second counter for inside while loop ( to count how many times hi was printed ) count2 = 1 write a keyword while and specify the condition, here we want hi 3 times only so condition will be  count2 <=3 put a colon ( : ). on next line write a print statement. and increment a second counter. Simply write a count2 = count2 + 1. This a block so this block should be in proper indentation.

print("\n")
# using while loop 
count1 = 1  # setting a counter 

while count1 <= 3# condition
   print("hello")  # printing hello 
   
   count2 = 1 # setting a second counter
   while count2 <= 3# condition 
      print("hi"# printing hi
      count2 = count2 + 1 # incrementing second counter

   count1 = count1 + 1  # incrementing counter1

print("\nTask completed ")

 

This will print hello and hi 3 times and this combination will repeat total 3 times.




This is all about while loop in Python. Create simple while loops and nested while loops and try your own things. 


If you have any doubts in this post then please ask me in comment section below. I will try to solve it.


If you like these post and you have learned something from this then share this post with your friends and family.

Keep learning always.....!!! 😇😇😇




Previous Post  Next Post