While loops - I





Hello folks....!!!
Now we are going to step into looping concepts in python.
When you want to do a job repeatedly, you need to put the instructions under a loop. The loops available with python are :

  • while loop
  • for loop

Python does not have a do-while loop. Today we will look into 'while-loops'.

While-loop in Python:

Syntax:

        initialize loop control variable
        while condition:
                  statement 1
                  statement 2
                          .
                          .
                          .        
                  statement n

First we need to initialize the 'loop control variable'. It is nothing but the value of variable on which the whole loop is going to be dependent. Here also we do not have curly braces. The indentation only matters.

Example 1: Print the numbers from 1 to 10

while-loop python

Here I have checked the condition as 'while i<11:', because You need to print the number '10' also. Thus we take one excess.
Note that if you fail to give the incremental part, then the loop will become an infinite one. 

#Printing hacks in Python

How to print on the same line !?

Just write as given below:

Python print on same line

In the print statement add end=" ". This will leave a space after each printed element instead of adding a new line.

Example 2:  Get a range from the user and print the numbers in that range:

Print numbers in a range python

Example 3: Can we do decremental steps in the loop ? Look at the code below. Let us print the numbers in given range in reverse.

loop decremental steps python

Nested While-loops:

We can nest the while loops as we did in if-else conditions. When you nest the loops, the innermost loop is executed until its condition fails. Then the nearest outer loop will be executed.

Example 4:
Consider the most frequently asked placement problem : Pattern Printing. You might be asked to print a pattern like this:
*
**
***
****
*****

Code:

Pattern printing python

Example 5:

Now lets us code for finding sum of digits in a number. These are also a kind of placement problems. These can be asked in interviews.

Consider a 5 digit number - 23764. The sum is 2+3+7+6+4=22

sum of digits python

What we do here is :
First we do modulo division with 10, so that we can get the last number as reminder every time. It is added with 't'. We save the quotient of the number back to 'i'. Now take a look at each iteration:
Iteration 0:
i=45767
t=0
Iteration 1:
t+=i%10------- 45767%10=7
Thus t=7
i=i//10--------45767//10=4576
**Here we use '//' for division because we need an integer as output and not a floating number. This yields the integer value.
Iteration 2:
t+=i%10------4576%10= 7+6 =13
t=13
i=4576//10 = 457
Iteration 3:
t+=7
t=20
i=45
Iteration 4:
t+=5
t=25
i=4
Iteration 5:
t+=4
t=29
i=0
thus the answer is 29.



Exercise:
1. Get input 'n' from the user ,  find the product of numbers from 1 to n.
2. Try printing the pattern below:
1
22
333
4444
55555
666666
7777777
88888888
999999999

Next Topic👉

Comments