Python 3 Programming Tutorial – For loop

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 a for loop’s structure very much like

for eachThing in thisThing:
do this stuff
in this block
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.

Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M

Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/

This python 3 tutorial covers the for loop in python 3.3.3

http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

Comments

Argent 2 says:

You didn’t explain how it works at all, you just said what which part means.

Omnia in numeris says:

Dang it. They’ve changed how the range() works in Python 3. It doesn’t return a list anymore. Apparently you HAVE to use it with for() or while() or something like that. Takes getting used to.

askforkris games says:

do this work on 3.6

Definitely Not Ben says:

you sir, are the reason i am passing university at the moment. thank you

Sarwar Sunjid says:

How to decrement for loop??

Kittens Rule says:

Thanks for the video helped me a lot

Free Pokemon TCG online codes says:

a=int(input(“How old are you?”))
if a >= 16 < 18: b=input("Are you at sixth form, work or at college?") print("You work at",b,".") if a > 18:
c=input(“Have you started taking driving lessons yet?”)

Why does this not work!!

Imran Khan says:

hey sentdex
if we want to end the loop before all iteration , how we set the condition
“within the code block?”

Courtney Girgin says:

Im confused on when will we actually know when to use a loop

Ibrahim Hajee says:

How would I write a program that will keep letting you add a monthly payment for 12 months

Anna Games says:

These videos are what will help me pass my Computer Science Degree!

Vedang Wartikar says:

which ide is he using?

Mian Asbat Ahmad says:

I believe while loop is conditional and for loop is limited. e.g. Get me all songs with hits greater than 10 million, use while loop because you dont know how many songs. In the case of get me the top 10 songs, use for loop because you know the limit i.e. 10.

Tyler-jay Richardson says:

it is ok needs less talk more coding

Orangexboom 18 says:

Can you explain this code that I found (specifically the for loop):

Import random

For i in range(5):
Value = random.radint(1,6)
Print(value)

So I understand the print, variables, import and whatnot however, I’m confused with the use of i. I don’t see i used in the statement, only in the for loop part. Please explain what i is used for

David Amadi says:

Can you do a loop through pivot table?

Josephine says:

why do you capitalise the first letter of the second word in the variable name?

eathenbad says:

Is range(1,11) like 1

Hardik rana says:

at 5:30…. we can do in python 2.7(2.7.13)

Wanda Reed says:

is it common for programmers to make the “x” really specific like you did with “each number”?

Dániel Deli says:

wow man you are a very good teacher!

Robert Baloyan says:

thanks for the videos )), a little question, why in the range it does not include 11? but 1 is included ? thanks in advance

Tech Faeed says:

i want this code that this will look like when u will run

please enter the number till you want to go : 10

1+1+2+3+5+8+13+21+34+55 = answer
1 2 3 4 5 6 7 8 9 10

[please reply fast]

Derik Davis says:

You are a great teacher! I’m currently doing the python programming tech degree on team treehouse and when I need a little more breakdown on a certain subject just swing on over to your videos.

Gaurav Jain says:

In range one for loop we didn’t mention from which variable we are taking the values then how do we know if there are multiples variable stored the values in the list..
Please give proper explanation.

Sage Georgi says:

haha 5:16 for a loop what a coincidence

Jose Martinez says:

Thank you for the video

sahil mishra says:

how to assign the numbers in separate variables???

Anuraag Yarramreddy says:

I do not understand why anyone would dislike this video? It is a good tutorial!!!!!

Extra Bass says:

u said u r going to tech how to make a for loop but

harish poojari says:

Good tutorial man !!!

mikha el says:

master, what IDE u using? haha noob here

Thunder 440v says:

Damn, I am grateful that I came across your tutorials. Your playful way of teaching inspires me 🙂

Tusk Bilasimo says:

Hi Santdex, love your work! I have a question: I have a list of integers that I want to loop through (i use them as a sequence of variables) but when it finishes iterating I want it to automatically loop back and re-iterate through the list again and again until a condition is met. Thanks

FGWPlasma says:

I am trying to use loops to minimize repeated words and I am having trouble with it. I have just started using python and could use some tips.

shiwans vaishya says:

i need to print hello world 100 times. each word in one line.like
hello world
hello world
like this 100 times

Daryl Musser says:

I appreciate your tutorials. What GUI or program are you using in the tutorial for Python? Thanks!

 Write a comment

*

Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!