Coding

Python Programming Tutorial #6 – For Loops

  • 00:00:00 hey guys welcome back to another video
  • 00:00:03 this is the sixth video in my Python
  • 00:00:05 programming series and today we're going
  • 00:00:07 to be talking about four loops again if
  • 00:00:10 you miss the other videos in the series
  • 00:00:11 please go back and watch them first as
  • 00:00:13 it's going to be hard to understand if
  • 00:00:15 you haven't seen them already okay so
  • 00:00:17 let's get right into it today we're
  • 00:00:18 going to be talking about four loops and
  • 00:00:21 a loop is a new thing now if you're a
  • 00:00:22 beginner in Python and I'll talk about
  • 00:00:24 how they work we're going to start off
  • 00:00:25 by just writing one out getting right
  • 00:00:28 into it with these syntax so for X in
  • 00:00:31 range and then we're going to put a
  • 00:00:34 number which is going to be our starting
  • 00:00:36 position so in this case I'm going to
  • 00:00:37 put zero and then a stop point so ten
  • 00:00:41 okay and then we're simply going to
  • 00:00:43 print out X now I know we did this
  • 00:00:46 pretty quickly but let's talk about what
  • 00:00:49 this is and the syntax so a for loop is
  • 00:00:53 going to run a certain amount of times
  • 00:00:56 so in this case we're going to start at
  • 00:00:58 zero and we're going to go to ten
  • 00:01:01 this means the for loop is actually
  • 00:01:03 going to run ten times so what's going
  • 00:01:07 to happen is we are going to have our
  • 00:01:09 variable X which is right here and every
  • 00:01:12 time before loop runs X is going to be
  • 00:01:14 increased by one now we can increase it
  • 00:01:18 by more if we'd like to by adding
  • 00:01:20 another comma here but by default it is
  • 00:01:22 set to one like this I've just put it in
  • 00:01:25 so that you can visualize it so really
  • 00:01:27 we have something start a stop oops stop
  • 00:01:32 and an end
  • 00:01:33 start stop step sorry mother those put
  • 00:01:37 commas here to show you how it works so
  • 00:01:39 you can see we have our zero that's our
  • 00:01:41 starting number our stop which is ten in
  • 00:01:44 our step which is going to be one so I
  • 00:01:47 know you don't quite understand yet but
  • 00:01:49 you will after I show you a few examples
  • 00:01:51 so we have X is our variable which is
  • 00:01:54 going to be holding the number of what
  • 00:01:57 iteration we are through now iteration
  • 00:01:59 just means how many times we've already
  • 00:02:02 looped through this loop okay so let's
  • 00:02:06 do an example here all right so we have
  • 00:02:08 X and we've just started running this
  • 00:02:11 for loop now right now X is zero we're
  • 00:02:13 going
  • 00:02:13 print X we're going to print zero to the
  • 00:02:15 screen and then we're going to add 1 to
  • 00:02:18 X now this time we come to the loop we
  • 00:02:21 say well is X less than 10 yes it is so
  • 00:02:24 now we're going to print X X is 1 now
  • 00:02:26 we're adding 2 okay so now X is 2 is 2
  • 00:02:31 less than 10 yes 2 is less than 10 so
  • 00:02:34 let's print two okay and we keep going
  • 00:02:36 until eventually we print 9 then X has
  • 00:02:40 one added to it so it becomes 10 well 10
  • 00:02:43 is not less than 10 so we're not going
  • 00:02:46 to print out 10 all right I know I just
  • 00:02:48 talked a lot there but let's see it in
  • 00:02:50 action all right so just like I said we
  • 00:02:55 print out the numbers 0 through 9 now
  • 00:02:58 this may seem weird because I have the
  • 00:03:01 value 10 here so you'd say well why
  • 00:03:03 doesn't it prints out 10 it's because of
  • 00:03:05 the way the loop works so like I said
  • 00:03:07 really what's happening here is this
  • 00:03:10 plus equals 1 so pretty much this means
  • 00:03:12 we're just adding 1 to the variable X
  • 00:03:15 you can also write it like this x equals
  • 00:03:18 x plus 1 like that alright so we print
  • 00:03:23 out X and then we add 1 to X and we have
  • 00:03:26 to be less than 10 for this loop to run
  • 00:03:28 so on the 9th iteration we have well 9
  • 00:03:31 is equal to 9 plus 1 so X is equal to 9
  • 00:03:33 plus 1 we get the value 10 now again 10
  • 00:03:36 is not less than 10 so where you're not
  • 00:03:38 going to print out 10 yeah so that's
  • 00:03:42 pretty much how a for loop works it's
  • 00:03:46 pretty basic this is the syntax we have
  • 00:03:49 for and then a variable name so this
  • 00:03:51 doesn't have to be X we could use
  • 00:03:53 anything we'd like to use I could use
  • 00:03:55 hello I could use any other word it
  • 00:03:59 could be as long as the 1 assures a lot
  • 00:04:00 doesn't matter then we have the word in
  • 00:04:03 which is highlighted in orange and then
  • 00:04:06 range now the range
  • 00:04:08 takes a different amount of arguments
  • 00:04:10 pretty much an argument is whatever's
  • 00:04:11 inside this brackets so like at the
  • 00:04:13 beginning how I showed you we just did 0
  • 00:04:15 and 10 this means we have a start and we
  • 00:04:18 have a stop so we start at 0 and we go
  • 00:04:22 to 10 now I can also I can actually put
  • 00:04:25 1 our
  • 00:04:26 in here like this 10 and that just means
  • 00:04:29 that we just have a sub so by default
  • 00:04:31 Python will say well we're just going to
  • 00:04:33 start at zero and we're going to step by
  • 00:04:36 one now I haven't showed you the step
  • 00:04:37 yet so we'll do that so again we're
  • 00:04:40 going to start at zero we're going to
  • 00:04:41 stop at 10 and we're going to step this
  • 00:04:43 time by two now what step is is how much
  • 00:04:46 we add to X every time so before I
  • 00:04:48 showed you had x equals x plus 1 in this
  • 00:04:52 case since this step is – it's going to
  • 00:04:54 be by 2 like that ok so let's go ahead
  • 00:04:58 let's run this we have 0 2 4 6 8 and you
  • 00:05:04 notice how we again do not print out 10
  • 00:05:06 because 10 is not less than 10 okay
  • 00:05:10 again we'll do another example with the
  • 00:05:11 step let's put 5 in this time now you
  • 00:05:14 can guess what's going to happen 0 5 it
  • 00:05:19 actually only prints out two things
  • 00:05:20 because we start at zero so we print 0
  • 00:05:23 and then we add 5 we get 5 now 5 plus 5
  • 00:05:26 again is not less than 10 so we are not
  • 00:05:28 going to print out 10 okay so that is
  • 00:05:31 the tutorial on the for loop we're going
  • 00:05:33 to do another type of for loop where
  • 00:05:35 we're going to use something called
  • 00:05:36 lists a little bit later on but right
  • 00:05:39 now we're just using this one this is
  • 00:05:41 the basic one I encourage you to play
  • 00:05:43 around with it yourself and yeah so if
  • 00:05:47 you like the video please go ahead and
  • 00:05:49 like it and subscribe and I will see you
  • 00:05:51 again for another video thanks good bye