Python Programming While Loops Tutorial with Stone River eLearning

Having discussed Python variables and Python mathematical functions in the last two blog posts, we will now move towards the part of Python that forms the core for performing all the operations. It is highly probably that you might already know about flow control statements, but given their importance, we are going to discuss each flow control statement in detail. Flow control statements are basically loops aimed at performing a particular set of operation for a period.  Writing condition for let’s say 100 integers knowing that the operation to be performed on each one of them is the same is both ineffective and time consuming. Therefore we need a flow control statement that can customise our code and group all the elements together and then write the operation to be performed for the group instead of doing it for individual elements.  To deal with the repetitive nature of any code or program, we can use either the ‘for loop’ or the while loop. However; if the operation to be performed in each case is the same, while loop is a better alternative than the ‘for loop’.  We will discuss more about the ‘for loop’ in the upcoming blog posts.

There are quite a few interesting points to note about the while loop. First and foremost, a while loop provides a condition and as long as that condition holds true, performs a specified operation. The while loop is essentially used to optimize a given code and to ensure the perfect use of flow control statements in it, therefore it has a few similarities to both, the IF statement and the FOR statement.

Eg:

condition =1

while condition < 10:                   #While loop initialised and condition provided
print(condition)
condition = condition +1              # The value inside the variable condition is incremented

As it is quite clear in the example given above, the while loop performs the same operation upon a variable until a given condition holds true.  The while keyword is used to initiate a while loop and the “:” tells Python that you are going to start the loop now.

Note:

  • The condition included inside the while statement can be more than one.
  • As described in the video, the statement condition = condition +1 is the same as condition += 1. The choice of statement depends on what the coder is comfortable with.
  • We can also decrement the value of the variable in the loop according to the requirement of the problem and the values desired in the program.

Comments and Infinite loop

The video clearly explained how a user can write comments on Python and more importantly make his/her code more explanatory. There are two ways of writing a comment on Python, the first one is by using the ‘#’ symbol before the text. However; the ‘#’ symbol can only be used for single line comments and for multiple line comments we must include the comment lines inside triple quotes (‘’’) and the same to end the comments.

Infinite loops are things that you should avoid in your code as they do not give the desired results and in codes that are used for running applications; an infinite loop may have catastrophic effects. Knowledge of how an infinite loop occurs is the best possible way of avoiding its occurrence in the code.

Along with its utility in simple Python codes, ‘While loop’ is used extensively in cases where a particular data is to be fetched from a database.  Names of people above a certain age or other related data can be fetched with ease and accuracy from a big database using the while loop. However; unlike other programming languages, using while loop in Python requires the programmer to structure his/her code very precisely and accurately and the possibilities of an error, especially in the case of beginners is quite high. Therefore, if you are just starting with Python, we would recommend you to prefer ‘for loop’ during your initial days, until you are comfortable with the language.

GET YOUR FREE PYTHON EBOOK!

(Visited 33 times, 1 visits today)

Comments