Skip to main content

For Loop in python 3 : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 7

I am back again with another part in the beginner python programming tutorial series. We will continue with "Loops" but this time we are going to learn how to use for loop in python 3. 

Difference between For loop and While Loop

A "For Loop" and "While Loop" have a common connection that both are looping statement, but major difference is that unlike while loop, a for loop gets executed a specified number of times.

We can break down the For loop into the following components(Refer to the for loop flow chart below).

  • for keyword
  • variable name
  • call to the range function
  • colon operator
  • clause or block of code

Hovering over the flow-chart

The above flow-chart is..well horrible I know, but I make my own drawings because I can't afford an artist. So, bear with me. In the above flowchart is a very basicy, laymanish representation of a for loop. The decision box contains the For statement along-with it's soulmate i.e the Range function. As long as the variable I falls within the range x then the control flows down to the underlying code and it continues in the loop till the time I is less than x.

Digging deeper down

We will formulate a simple code to understand how to implement the concept we learned above

Run the above code and the output shall be
I will draw better flowcharts from next time
I will draw better flowcharts from next time
I will draw better flowcharts from next time

Now how exactly the above code works? It's simple, the control goes to the for statement where it sets an
initial value of 0 to the variable i, then it checks if this value is less than the value within the range function. 
Which in our case resolves to true so the control moves to the print statement and then prints the statement 
within it and then immediately increments the value of i by 1, after which the control again moves to the for 
statement. This process continues till the value of i is less then the value within the range function. Once the 
value is equal to the range function ,the control exists the loop.

Something more from the Range()

Now you must be wanting more from the Range() function rather then just handling the range, maybe some more customisation.  I don't blame you, it's human nature to want more, we are never satisfied with less. Looking into this the good guys at python have imparted some more room for customisation in the Range function and this we shall see in the below code.

In our above code snippet everything is same as the previous example but this time we have introduced a start position 1 which is known as the step-in and an end position 3 which is known as the step-out. What it means is that the control enter the loop when the index is at the count-1 and exists the loop when the index is at count-2 and because of this the statement is printed only 2 times instead of 3.


Now I must say that we have just scratched the surface for loops in python, but if you are in a mood to make a deeper dive into understand the loops in details then you should definitely go for this book Automate the Boring Stuff with Python: Practical Programming for Total Beginners . I started my coding journey with this book and I believe you would get something out of it too.

This was all for today but before signing off I would like to thank you all for the love that you guys have shown me. I am overwhelmed by the daily visits that I get on my blog and my youtube channel.

I work hard to create and share programming tutorials and stuffs on my blog and youtube channel . It's my sole motive to help you learn coding by sharing my knowledge and experiences that I have gained during my own journeys. All my contents are absolutely free but a nice hot cup of coffee from you would be great to get me going. Now it's perfectly fine if you don't want to buy me a coffee, you can just stop by to say hello.

If you like my work and it helps you in learning what you want then you can buy me a coffee by clicking on the below button.

Buy Me a Coffee at
Take me to Python Tutorial - Part 6                                                      To Be Continued ....


Popular posts from this blog

How To Install Tensorflow On Windows

Why I am writing this tutorial ?
Recently I have started learning machine learning specifically DeepLearning for one of my pet projects, and during this quest of mine I wanted to learn TensorFlow library which is developed by Google. Learning is one thing but first I needed to install the library, but there was this problem which I faced during the installation. I am using Anaconda as my package manager and trying to install the Tensorflow library on my work computer which is behind a proxy, installing directly from the anaconda cloud while behind a proxy always displayed a proxy error.

How did I resolve the issue? Well this is exactly what I am going to write down in the below tutorial. The Steps To Follow
Before we get all cosy with this short tutorial on Tensorflow installation, let me tell you that
this tutorial is on the assumption that you have already installed Anaconda on your machine and you know at least a few basic things or two about python.
If you want to learn basic pyt…

What I learned from writing a guest post

Recently I wrote a python tutorial for Datacamp, while writing this tutorial I learned a couple of things which I am going to share with you right here in my blog post. May be you can use my experiences in your own endeavours as a writer.
IdeaThe first step is idea generation. You can either search through any previous topics in your blog post if you have one or you can go through your past learning, experiences or any problem statement that you might have gone through and you will find that while doing that you can get tons of ideas to start your article. If you fall short of ideas then you can ask for help from may be your social media followings your friends or your family, for example I asked the people at Datacamp themselves and they suggested me a couple of topics which got me started on the right note. So, be prompt to ask for help and you will be surprised how easily you can crowd-source ideas and there's nothing wrong in it.

Squeezing in time

Squeezing in time to get into …
Encountering The Python
What is Python? As per Wikipedia “Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here).
What kind of programing language?To give a better idea let's look at the below mentioned summary-
High level language- Python is a high-level language because it provides strong “abstraction”.Interpreted - Python is an Interpreted language because it does not run on a compiler.Dynamically typed language - Python is dynamically typed because in python every variable is bound to an object.Why should I learn Python?Python is a very powerful programming language, secondly it's easier to learn and since it's interpreted language the code is easier to understand. Lastly there is a huge community supporting Python so if you get stuck anywhere lot's of help are a…