While loops - II





Hello Folks...!!!

Today we shall see how to use while loops for iterating through the data structures....

Example 1: 
Consider the sentence: s="learning python is very easy." . Now the task is to capitalize each word of the sentence. See the code below:

Python While loops

  • We first split the sentence into list of words.  
  • Then we store the length of the list in the variable 'n'. We initialize the loop control variable 'i' to '0'
  • We define a loop stating that, until 'i' remains less than the length of the list.
  • Inside the loop, we use capitalize() to capitalize each word and store it back in the same index in the list.
  • We join the list and store it back in the string variable 's'.
Example 2:
Minu is trying to decode a secret message.The message is  "yawa rotcod eth speek yad a elppa na". She found that every word is reversed and the whole sentence is also reversed. Now let us help her decode it programatically.


Example 3:  

Now I will code a simple placement problem - 'Array rotations'

If you come across such a problem, it is nothing but rotate the list for specified number of times. 
For example:
Given list : [1,2,3,4,5]
No.of.rotations: 2
Resultant list: [3,4,5,1,2]

Let us look at the solution now :


First we store the element at index '0' in a temporary variable, then we remove it out and the add it at the end of the list.

break and continue in loops:

One can either break the loop or continue without executing the statements below using the keywords 'break' and 'continue'.

Example 4Consider the pattern below :
                            1
                          333
                        55555
                      7777777
                    999999999

A pyramid of odd numbers below 10. Let us look at the coding :

Pyramid pattern printing

What we do here !?
  • The first loop checks whether the number is less than 10.
  • The if condition checks whether it is an odd number or an even one.
  • If it is an even number, then without executing the next if-condition and statements below, it continues the first while loop. If you do not use a continue here, it will check the next if-condition also.
  • If it is an odd number then the further proceeding for printing it out is carried out.
  • We have a variable 'space' initialized to 10. It is used to print the number in appropriate place leaving space before it. 
  • Thus the second while loop is concerned with the alignment.
  • The third while loop prints the number.
  • When 'i' increases, space has to decrease. So we decrement space in the outer most loop.
This is how we print a pyramid pattern.

Example 5:
Let it be a game. You must get input from the user and display it. Once the user hits a number greater than 10, It must exit the loop and stop getting input from user.

Python break

With this simple last example, we will finish the while loops concept.

Python provides you a provision to use 'else' with 'while' loops. You can give the instructions to be executed if the while loop fails in the else part. For example:

Example 6:

while and else

Here when the count is less than 5, its square is printed. When it is exceeded, the loop is not simply terminated, but a message is also displayed.


Next Topic👉

Comments