For Loop Python Tutorial




for eachThing in thisThing:
    do this stuff
    in this block

The next loop is the For loop. The idea of the for loop is to "iterate" through something. For each thing in that something, it will do a block of code. Most often, you will see a for loop's structure very much like this.


So, again, why would someone use each type of loop?

Typically, you will see the while loop being used for finite tasks that have predetermined length, and the for loop being used for tasks that have uncertain and variable time-frames.

That said, the for loop can be used for the exact same tasks as the while loop.

For this reason, I prefer the for loop myself, but again, it comes down to personal preference.

Here's an example of a for loop:

exampleList = [1,5,6,6,2,1,5,2,1,4]

for x in exampleList:
    print(x)

This code will print out each item in that list. Usually, people will choose to actually do something with the item in the list, more than just printing it out, but this is just a basic example.

Another example of a for loop is in the case of something like:

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

This code is actually what is known as a generator function, and is highly efficient. The above works very much like the "counter" function we made with a while loop. The only difference is this one is much faster and more efficient in many cases.


There exists 2 challenge(s) for this tutorial. for access to these, video downloads, and no ads.


There exists 2 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial