Python Programming For Loop Tutorial with Stone River eLearning

In the end of our last blog post, a brief introduction about the similarity between the ‘While loop’ and the ‘For Loop’ was given. Since we have already discussed about the while loop and how it functions in python, there cannot be a better way to highlight those similarities in detail, than by learning the” for loop” itself.

Both the “while loop” and the “for-loop” are used to execute a given operation for a specified number of time. In most of the cases, while loop and for loop are interchangeable and the choice depends upon how comfortable a user is with each one of them.  As we mentioned in the blog post on ‘while loop’, for a beginner in python, it is better to prefer for loop over while, however; learning both the flow control statements and getting a good grip of them is extremely essential to get better at any programming language, including Python.

How is the “for loop” executed?

If there is a fixed number of times any operation needs to be executed, then in such cases the number of execution cycle of an operation does not depend on any condition and while loop will be of little help. Therefore, embedding the more versatile “FOR loop” is a better option as it gives the number of times of execution of an operational cycle in the loop initialization step itself.

Python follows a structure that is very similar to writing codes in simple English language and therefore, if you have prior experience of working on languages such as C, C++ or Java, you might –loop’ find the Python execution of the ‘for-loop’ to be a little different. However; if your basics are strong and if you are one of those who believe in understanding the logic rather than simply mugging up the rules, then you will find Python’s execution of the ‘for-loop’ to be much easier than the others.

Eg 1:

exampleList = [6,7,8,3,5,9,11,10]

for thing in exampleList:

print(thing)

Eg 2:

For x in range(1,11):

print(range)

As you will notice, from the two examples, writing a Python for loop is very simple and straight forward. You do not have to import packages, mention header files, echo useless statements, write 10-15 lines of code before you could reach the actual function. In many ways, Python does what a function does in all the other programming languages, with the only difference being that Python does not need the build up to the function and the function calls. Further, you will notice that writing a code on Python is as easy and effortless as writing an algorithm on a piece of paper. If you know the logic behind it, python will make sure that keywords and syntax does not create any hindrance for your ideas.

Note:

  • The use of colon (:) after the statement that involves the “for” keyword is absolutely crucial and critical to ensure that the for loop executes exactly how it is supposed to.
  • You will notice that despite giving (1,11) as inputs to range, the console only printed number from 1 to 10. The reason behind that is the range does not include the end of it and therefore it can be compared to the “<” less than symbol in for loops used in other languages.

We have not talked about Python 3.0 and how it is better than the previous versions of Python in the blog posts, even though these points were mentioned quite a few times in the videos.  The use of range in the ‘for loop’ highlighted how Python has evolved and improved from its previous versions. As described in the video, the range in Python 3 is very similar to the xrange in Python 2.7, with the former being a faster and a more efficient way of doing the process.

GET YOUR FREE PYTHON EBOOK!

(Visited 122 times, 1 visits today)