Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

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."

For a more tangible and better look into the Python language, consider reading the following book. It's an excellent read.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments
  • User

    WsfzTKwKE Nov. 28, 2016, 1:48 p.m.

    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?

  • User

    AlanTuring Oct. 9, 2015, 12:51 a.m.

    Not including the last number seems inelegant. There must be a reason its done that way. Anyone know why? (1,3)-> 1,2,3 seems more elegant or logical than (1,2)

  • User

    James_learn_to_code Aug. 27, 2015, 8:10 p.m.

    Enjoy it very much !

  • User

    Langevin April 6, 2015, 11:16 a.m.

    @fadrior The function range(start, stop [,step])

  • User

    BRIAN March 23, 2015, 5:58 a.m.

    lam loving it, just like Macdonalds :)

  • User

    BRIAN March 23, 2015, 5:56 a.m.

    nice staff!!

  • User

    fadrior March 20, 2015, 8:07 a.m.

    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 ?

  • User

    fadrior March 20, 2015, 7:59 a.m.

    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

  • User

    sjhuskey March 2, 2015, 7:09 p.m.

    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 …"

  • User

    Guru the Wizard Feb. 25, 2015, 5:44 p.m.

    KingFreeFall python.org

  • User

    KingFreeFall Jan. 20, 2015, 4:34 a.m.

    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.

  • User

    Barak Dec. 8, 2014, 2:03 p.m.

    Im starting to get this, grate explenations b=1 for a in range(1,6): print(a) if b>9000: print("IT'S OVER 9000!!!") else: print("not yet...") print(b) b*=10

  • User

    Neo Nov. 22, 2014, 4:31 p.m.

    By adding a module statement we can print even numbers.Just do as the following: for a in range(1, 20): if a%2==0: #This is were we add the module print (a)

  • User

    bin4rym4ge Nov. 4, 2014, 5:11 p.m.

    for x in range (1,99): print (x)

  • User

    Naveen July 5, 2014, 2:32 p.m.

    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: &gt;&gt;&gt; code for a in range(1,3): b = a print (b) code &lt;&lt;&lt;&lt; Error message - File &quot;.py&quot;, 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 - &gt;&gt;&gt;code for a in range(1,3): b = a print (b) &lt;&lt;&lt; code error message - ParseError: bad input on line3 ========================= That to me sounded *** WWWhhhaat! LOL

  • User

    EpicYoloTime July 2, 2014, 7:53 p.m.

    for a in range(1,99999): Print (a) Right then was when i had to restart my laptop after about an hour. (Not using the internet python tester)

  • User

    EpicYoloTime July 2, 2014, 7:46 p.m.

    @chris packet: That also counts you.

  • User

    Gen45 June 18, 2014, 8:55 a.m.

    a = 3 b = 5 def sumaEstupida(a,b): print(b-a) for a in range(1,10): sumaEstupida(a,b)

  • User

    Weifeng June 12, 2014, 5:58 a.m.

    Just numbers? How the loop could be used in string?

  • User

    DB_alfa June 9, 2014, 1:05 p.m.

    wow great tutorial i fell like i am in a lona park when i learn here

  • User

    AllAboutMike June 8, 2014, 3:47 p.m.

    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.

  • User

    Arunkumar Ravikumar May 30, 2014, 11:30 p.m.

    Also we can increment the loops , in the below example i am increment the loop by 5 for a in range(10,30,5): print(a)

  • User

    Arunkumar Ravikumar May 30, 2014, 11:11 p.m.

    Happy that we dont have to increment the loop variable a=10 for a in range(10,20): print(a) #a=a+1 print(&quot;--&quot;) print(a)

  • User

    nnaa igwiloh May 22, 2014, 4:47 a.m.

    range are easily understoood i love it heree.check out my own crazy test on def function male = &quot;richard&quot; female= &quot;alicia&quot; def name(): print (male, female) name()

  • User

    Marcus D May 1, 2014, 7:32 p.m.

    why doesnt python show the value of 3 in the example?

  • User

    MaximusBo April 23, 2014, 8:09 p.m.

    And mathkindaguy, did you learn all that programming from the previous tutorials?! Because i didn't even get close to that! :D

  • User

    MaximusBo April 23, 2014, 8:07 p.m.

    agreed. Also, try : for i in range(501) print i print i if you want your computer to lag out.... Choose wisely!

  • User

    javed April 13, 2014, 9:30 p.m.

    the tutorial is very good for learning but it lacks questions for testing

  • User

    Lotfi GHAZOUANI Feb. 26, 2014, 1:47 p.m.

    Great step to know about the for instruction in Python

  • User

    Mouath Ibrahim Dec. 17, 2013, 3:04 a.m.

    for anyone who gonna try this for i in range(1,999999): print (i) you gonna have a BAD TIME :P

  • User

    The Aztek Dec. 9, 2013, 6:51 a.m.

    @Nourequip look at this example for a in range(1,3): print (a) b=a+10 print(b) print (&quot; for loop finished&quot;) Everithing that it'is spaced it'is under the &quot;for&quot; function. Hope this will solve your dubts

  • User

    Ruben Oct. 15, 2013, 3:18 p.m.

    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 &quot;inside loop&quot; print &quot;loop &quot; + str(e) print 2 ** e print 3 ** e print &quot;not inside loop&quot; print 2 ** 10

  • User

    Matt Walke Oct. 3, 2013, 12:58 p.m.

    For me this is is so far the best tutorial ever, I've only done a little bit of programming before and I'm picking this all up so easily!

  • User

    Darren Aug. 28, 2013, 1:44 a.m.

    This has been so helpful i just could'nt grasp the meaning of this before. thanks again.

  • User

    stupidsid July 21, 2013, 9:26 a.m.

    awesome work guys

  • User

    Cacmo July 17, 2013, 11:24 p.m.

    This is great, is awesome!

  • User

    Daniel Wilianto June 5, 2013, 12:06 a.m.

    @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 &quot;inside loop&quot; print &quot;loop &quot; + str(e) print 2 ** e print 3 ** e print &quot;not inside loop&quot; print 2 ** 10

  • User

    R.S.Sai kumar April 26, 2013, 8:47 p.m.

    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

  • User

    Jakfar Shodiq March 26, 2013, 9 p.m.

    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 ..



Advertisement