Home » Tutorials » Python » For Loop

For Loop

It’s time for an awesome part of Python. Python’s for loops are pretty amazing compared to some other languages because of how versatile and simple they are. The idea of a for loop is rather simple, you will just loop through some code for a certain number of times. I won’t get a chance to show you the flexibility of the for loops until we get into lists, but sure enough its time will come. Onto an example:

Example
for a in range(1,3):
    print (a)
Result 1
2

First, print is indented for a reason. Remember that Python is picky about spacing. It is somewhat complicated to understand exactly what a is in the example. In this instance, a is a variable the increments itself every time we run through the loop. Next, we use the range keyword to set the starting point and the point right after we would finish. This is precisely why the number 3 didn’t print. Python is quite fond of this idea of all the way up to a number, but not including it.

Also, notice the in keyword. This is actually part of the for loop and you will understand it a bit better after we deal with lists and dictionaries. So, basically the above for loop says, “For variable a, which will be incremented at the end of every loop, in the range of 1 up to 3.”



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "For Loop". After Hours Programming. Accessed on April 23, 2024. https://www.afterhoursprogramming.com/tutorial/python/for-loop-py/.

  • Stewart, Suzy. "For Loop". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/for-loop-py/. Accessed 23 April, 2024.

  • Stewart, Suzy. For Loop. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/for-loop-py/.



38 thoughts on “For Loop”

  1. Perhaps at least half of all readers may be wondering why after each lesson there’s a plug for a book, but no mention of it’s name, no link… just a mystery. Perhaps Author can explain?

  2. how to get every other element? i.e. skipping even index elements in a list.
    for example in the list [0,1,2,3,4,5,6,7,8], i only want to work on the 0,2,4,6,8 ?

  3. print(str(range(1,9)));

    to know the element of the “range(x,y)”, without going thru a loop. Coming from C/C++, it’s NOT something you can do in a 1 liner !! well done python

  4. There is a typo in the third sentence of the second paragraph: “In this instance, a is a variable the increments itself every time we run through the loop.” It should be “? a variable *that* increments ?”

  5. I apologise for the fibonacci test on your server

    def a(b):
    if n < 2: return n return a(b-2) + a(b-1)print map(a, range(startNumber, endNumber))I didn't mean to cause a problem. I plugged in 1 and then a few 9's, it was an experiment. where can i download a python writing program please.

  6. Just wanted to share couple of things about for loops, what happens if you intentionally ( or not ) put an error.

    if you do not include the colon at the end of if statement in that same line – then you get a parse error as follows-

    ParseError: bad input on line1

    Also, I included in my code an intentional indentation error: and the message you get is:

    >>> code

    for a in range(1,3):
    b = a
    print (b)

    code <<<<

    Error message –
    File ".py", line 3 print (b) ^ IndentationError: unindent does not match any outer indentation level

    ******************** Now the interesting error. Should the last line have an intentional error after indentation spaces — the error changes –

    >>>code
    for a in range(1,3):
    b = a
    print (b)
    <<< code

    error message –

    ParseError: bad input on line3

    ========================= That to me sounded *** WWWhhhaat! LOL

  7. For loop doesn’t really work like you described here. Try this:
    print(range(1,3))
    You get:
    [1,2]
    So:
    for a in range(1,3):
    print(a)
    Means:
    for each value (we’ll call it ‘a’) in range(1,3) (which is the list [1,2]):
    print the variable ‘a’

    This would look better done in colours, but I hope you see what I’m trying to say.

  8. range are easily understoood i love it heree.check out my own crazy test on def function

    male = "richard"
    female= "alicia"
    def name():
    print (male, female)
    name()

  9. @Nourequip

    look at this example

    for a in range(1,3):
    print (a)
    b=a+10
    print(b)
    print (" for loop finished")

    Everithing that it’is spaced it’is under the "for" function.
    Hope this will solve your dubts

  10. Thanks Daniel! Tried out your suggestion!
    @kumar : for-loop will execute all indented lines after it, until the indentation changes.
    Try out this example:

    for e in range(1, 5):
    print "inside loop"
    print "loop " + str(e)
    print 2 ** e
    print 3 ** e
    print "not inside loop"
    print 2 ** 10

  11. @kumar : for-loop will execute all indented lines after it, until the indentation changes.
    Try out this example:

    for e in range(1, 5):
    print "inside loop"
    print "loop " + str(e)
    print 2 ** e
    print 3 ** e
    print "not inside loop"
    print 2 ** 10

  12. After print statement does the for loop ends? What if the multiple statements have to be written inside the loop?? Some one please give some perfect example

  13. In python version 3.3 not work. Just info 🙂
    Hmm, or maybe is another way ..

    But we have using,
    example :

    word = [1,2,3]

    for x in word:
    print(x)

    result :

    1
    2
    3

    Thanks alot for your tutorial, i amvery happy with your article about Python Programming ..

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.