Python for loop example

Filed Under: Python

Python for loop is used to iterate over a list of elements or strings. Today we will look into python for loop and it’s usage with some example programs. For loop is present in almost all programming languages and used a lot for iteration, same is the case with python for loops too. In the previous tutorial, we learned about python if else.

Python for loop

Python for loop is used to iterate over a list, tuple or strings. If you don’t know about list or string, you can learn it from Python Data types. The basic structure of Python for loop is given below.

for itarator_variable in sequence_name:
	. . .

For example, suppose we have a word, we need to print each letter of that word. So we can use python for loop as shown below.

for letter in word:
	print (letter)

python for loop

Again, you can use for loop to access a List data. Following code can help to understand the thing.

words= ["Apple", "Banana", "Car", "Dolphin" ]
for word in words:
	print (word)

python for loop example, python for loop list

Python Nested For Loop

You can also write one for loop in between another for loop. But in this case, you have to maintain the indentation properly. The following code will illustrate python nested for loop.

words= ["Apple", "Banana", "Car", "Dolphin" ]
for word in words:
        #This loop is fetching word from the list
        print ("The following lines will print each letters of "+word)
        for letter in word:
                #This loop is fetching letter for the word
                print (letter)
        print("") #This print is used to print a blank line

python nested for loop example

That’s all about python for loop examples. Hope that you learn well. For any query, feel free to ask in comment section. And must not forget, “Practice makes a man perfect”.


  1. Camel says:

    Thanks for this lesson. I’ve learned a lot.

  2. Madhubanti Jash says:

    Hi Pankaj, problem lies with this line “print letter”. It should be print (letter)

    1. Pankaj says:

      Thanks for pointing out the typo error, I have corrected it now. Appreciate it.

  3. Madhubanti Jash says:

    Thank you Imtiaz Abedin for this very useful tutorial series on python.

  4. Saurabh Suthar says:

    Python tutorials are really very helpful. But there are some errors in codes.Like in python nested loop example in print function.

    1. Pankaj says:

      Can you please point out which method is causing the error and what is the error? Could it be because of white spaces in copy paste? White spaces matter a lot in python programming.

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages