For loops in Python are easy πŸ”

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in this topic I need to explain for Loops a for Loop will execute a block of code a fixed number of times you can iterate over a range a string a sequence anything that is considered iterable I'll have more examples for you in future topics there is a lot of overlap where you could use either a while loop or a for Loop but for Loops tend to be better in situations where you have to do something only a fixed number of times here's an example suppose we need to count to ten if we were to use a for Loop we could write something like this we would type four then we would need some sort of counter typically you see people write X 4 x in then we will use the range function what number would we like to start at I would like to start at one then count to ten but the second number is exclusive so really we're going to write 11 if we want to count to 10. so then colon then hit enter whatever code you would like to repeat a certain number of times you will list underneath the for Loop and make sure the code is indented too I will print whatever our counter X is when I run this code we will begin at 1 then stop once we reach 11. so yeah there we are we have been going at one and we have counted all the way to ten so that's the basic Syntax for a for Loop four some counter really you can name this anything sometimes you'll see people name this as counter and that would work too but let's stick with x in some range where would we like to begin where do we stop okay now let's count backwards let's start at 10 then count down to zero when we escape the for Loop let's print Happy New Year when we print Happy New Year we are outside of the for Loop to count backwards you can enclose your range function within the Reversed function reversed so we begin at 10 count down to one then print happy New Year in this case two count backwards you would enclose the range function within the Reversed function there is an additional parameter too you could add that is the step if you would like to count by twos you would add comma two so I'm going to get rid of Happy New Year let's print the numbers 1 through 10 but we will count by twos and this does begin at one though so one three five seven nine if you were to change the step to three you would count by threes beginning at one four seven ten so the range function isn't the only thing you can iterate over you can iterate over a string let's say we have a credit card number credit card equals I'll make up some credit card number with dashes that is good enough for X in credit card print x x will hold our current position at first it'll be one then two three four dash so on and so forth so here's our credit card number one two three four dash five six seven eight I think you get the idea so you can iterate over a string with the for loop as well we'll have a few projects involving that there are two useful keywords as well these aren't exclusive to for Loops you can use these within while Loops as well they are continue and break suppose we are going to count to 20. 4X in range we will begin at 1 stop at 21. I think this is kind of a dumb example but it gets the point across 13 is considered an unlucky number right what if our counter reaches 13 I would like to skip over it well we can do that with the continue keyword if x is equal to 13 we will continue and skip over that iteration else we will print whatever our counter is so let's take a look yeah we have the numbers 1 through 20 but we have skipped the number 13. to skip over an iteration you can use the continue keyword whereas the break keyword we will break out of this Loop entirely if x is equal to 13 then break so yeah we have only counted to 12. once we reach 13 we have escaped the loop so yeah everybody those are for Loops you can execute a block of code a fixed number of times you can iterate over a range a string a sequence anything that is considered iterable there is a lot of overlap where you could use either a while loop or a for Loop while Loops tend to be better if you need to execute something possibly infinite amount of times such as when you're accepting user input for example but yeah everybody those are for Loops in Python
Info
Channel: Bro Code
Views: 57,196
Rating: undefined out of 5
Keywords: python tutorial for beginners, python course, python
Id: KWgYha0clzw
Channel Id: undefined
Length: 5min 5sec (305 seconds)
Published: Sun Oct 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.