When looping in Python, one of the most common tools is this range function that we get out of the
box with Python.
And you see here that range well is a range object, a special type of object in Python that returns
an object that produces a sequence of integers from start to stop.
Let's explore that a little bit more.
If I print here a range and I say range 100 and I click Run.
I get range of 0 to 100.
Hmm.
So it looks like a tuple, but it's not.
It's a range object.
It's a special type of object.
And range.
You can give it like this, which gives you a default of zero to whatever you give it, which is 100.
Or you can just simply type zero, 100.
And if I click run, it's the same thing.
But where range becomes really useful and you'll see this in 99% of cases in Python is in for loops,
you see, besides tuples and lists and sets and dictionaries and strings, you can iterate over a range.
So for example, if I do four I in range and let's say item or in our case let's just do number in range.
And in here I do print number.
If I run this.
You'll see that I'll get numbers 1 to 100.
And why is this useful?
Well, using range, I was able to loop 100 times.
As you can see all the way to 99, because our count starts at zero and we're able to loop as many times
as I want so I can tell the loop.
Maybe we want to loop a certain number of times, maybe you want want to loop ten times where I can
tell the loop how many times to run.
It's extremely useful.
Maybe here we're performing some action where it says.
I don't know.
Email.
Email list.
So we want to send tend emails to an email list, for example.
Well, again, I can just click run and I sent that email.
Well, a bunch of times, ten times.
Another neat trick is that, well, you might not even need number, right?
We're not really using it in our code.
And often while you'll find in Python programs is if a programmer doesn't need this variable, you just
do an underscore and this is just a variable.
I mean, you could use it like this.
And you'll still get these numbers.
But it just indicates to other people that, hey, I don't really care what this variable is.
I'm just trying to use range so that we can loop over things, let's say ten times.
Now a range also comes with a third parameter.
And what that is is the step over.
So if I do two and by the way, default is one here, if I do two.
It jumps from 0 to 10 by two.
Which is very, very handy.
So what is range?
Well, range creates a special kind of object that we can iterate over.
Okay.
What if I do minus.
Oh, right here.
What if I do minus one?
Well, if I run this.
Hmm.
That's not going to work because.
Well, let me show you why.
If I change this to zero and ten and I do minus one.
It's going to go in the opposite direction.
But if I did just ten and zero.
That's not going to work.
If we want to do something in reverse, we just have to make sure that we add that minus one.
Or if we want to step back twice, then we click run.
So you have a lot of options to play with the range you can do ascending, descending.
One final thing, another place that you might see ranges.
And like I said, most of the time they're in for loops.
But you can also do something interesting like let's say a range.
Of ten.
And then if we wrap this in a list, remember, a list function allows us to convert something to a
list.
If I run this.
Did you see that?
Let's simplify this so that this range is going to just be two.
If I click Run.
You see that eye loop two times.
And in here, I'm going to print first of all, I'm going to create a range, an iterable object that
has ten items.
And then I'm going to convert that into a list.
So you can see here 0 to 9 and to a list.
So this is a quick way to create a list that, well, has integers.
A neat trick that you'll definitely use along your program or journey.
I'll see you in the next one.